哇,盡管多年來一直使用答案,但我的第一個堆疊問題。非常令人興奮。
我對 VBA 和 Excel 還很陌生,對 Access 完全陌生,完全公開。因此,我嘗試創建一個實驗室報告的核心資料庫,并且我有一個用于輸入有關新報告的資訊的表單,該表單將有關該報告的資訊添加到所有報告的主表中,包括為其分配一個唯一標簽。輸入資訊后,我有一個按鈕,允許用戶選擇報告隨附的 Excel .csv 檔案并將其作為新表匯入資料庫。它回傳成功或錯誤訊息。它有效!(代碼來自這里的某個地方)
問題是我想在新表中添加一個欄位,將分配給新報告的標簽添加到所有記錄,以便查詢可以通過使用該標簽來參考它。如果可能的話,我還想在新表中添加一個索引欄位,因為將 .csv 作為表匯入似乎不會創建索引。我想我將創建另一個子組件,將新報告名稱作為新欄位的名稱(這也將是所有記錄中欄位的值)以及將其附加到的表。
如果我剛剛匯入它,如何將這個子表傳遞給新匯入的表?我需要這一切從按鈕開始作業,因為它主要是我的經理使用此表單/按鈕匯入新檔案,他們將無法在創建表時手動進入表并添加欄位(是的,我知道這是顯而易見的解決方案,但相信我......這一定是一個按鈕)
這是我正在使用的代碼(是的,我知道很多可以用不同的方式完成,但它可以作業!)
Public Function ImportDocument() As String
On Error GoTo ErrProc
Const msoFileDIalogFilePicker As Long = 3
Dim fd As Object
Set fd = Application.FileDialog(msoFileDIalogFilePicker)
With fd
.InitialFileName = "Data Folder"
.Title = "Enthalpy EDD Import"
With .Filters
.Clear
.Add "Excel documents", "*.xlsx; *.csv", 1
End With
.ButtonName = " Import Selected "
.AllowMultiSelect = False 'Manual naming currently requires one file at a time be imported
'If aborted, the Function will return the default value of Aborted
If .Show = 0 Then GoTo Leave 'fb.show returns 0 if 'cancel' is pressed
End With
Dim selectedItem As Variant
Dim NewTableName As String
NewTableName = InputBox(Prompt:="Enter the Report Name", _
Title:="Report Name")
For Each selectedItem In fd.SelectedItems 'could later be adapted for multiple imports
DoCmd.TransferText acImportDelim, , NewTableName, selectedItem, True 'Imports csv file selected, true is 'has headers'
Next selectedItem
'Return Success
ImportDocument = "Success"
'Append report label and index
AppendReportLabelField(NewTableName, #What to put here as the table to append to?)
'error handling
Leave:
Set fd = Nothing
On Error GoTo 0
Exit Function
ErrProc:
MsgBox Err.Description, vbCritical
ImportDocument = "Failure" 'Return Failure if error
Resume Leave
End Function
AppendReportLabelField 將傳遞欄位的名稱(和值)和(新匯入的)表的名稱。我如何通過它的桌子?NewTableName 目前只是一個字串。如果我可以通過新的子表,我相信其余的會很簡單。
在此先感謝您的幫助!
uj5u.com熱心網友回復:
考慮將所有用戶輸入資料存盤在包含所有可能欄位的單個主表中,并使用臨時臨時表(主表的副本)將 CSV 表遷移到此主表。在暫存期間,您可以使用所需欄位更新表。
SQL (另存為存盤查詢)
(引數化更新查詢)
PARAMETERS [ParamReportNameField] TEXT;
UPDATE temptable
SET ReportNameField = [ParamReportNameField]
(明確參考所有列)
INSERT INTO mastertable (Col1, Col2, Col3, ...)
SELECT Col1, Col2, Col3
FROM temptable
VBA
...
' PROCESS EACH CSV IN SUBSEQUENT SUBROUTINE
For Each selectedItem In fd.SelectedItems
Call upload_process(selectedItem, report_name)
Next selectedItem
Sub upload_process(csv_file, report_name)
' CLEAN OUT TEMP TABLE
CurrentDb.Execute "DELETE FROM myTempStagingTable"
' IMPORT CSV INTO TEMP TABLE
DoCmd.TransferText acImportDelim, , "myTempStagingTable", csv_file, True
' RUN UPDATES ON TEMP TABLE
With CurrentDb.QueryDefs("myParameterizedUpdateQuery")
.Parameters("ParamReportNameField").Value = report_name
.Execute dbFailOnError
End With
' RUNS APPEND QUERY (TEMP -> MASTER)
CurrentDb.Execute "myAppendQuery"
End Sub
如果 CSV 上傳的資料結構差異很大,則合并一個 Excel 清理步驟以標準化所有輸入。或者,強制用戶使用標準化模板。暫存可用于驗證上傳。資料庫不應該是許多不同表的存盤庫,而是預先設計的設定中關系模型的一部分。運行開放式流程(例如由用戶動態創建新表)可能會導致維護問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454693.html
