我构建了一个表单,用户可以在其中选择一个或多个文件并将其导入到单个表中。当用户选择文件或多个文件时,一旦导入完成,我希望在每一行添加文件名,当然,与正确的文件相关。
我可以设置一个查询来手动添加文件名,但我怎样才能以更自动化的方式执行此操作。例如,如果用户选择文件,我如何编写SQL查询以自动检测文件名并添加它?如果用户选择多个文件,查询如何为每行写入正确的文件名?
这是我的表单代码:
f(a_2,a_3)+f(a_2,a_4)+f(a_3,a_4)
模块代码:
Option Compare Database
'Private Sub Command0_Click()
Private Sub cmdFileDialog_Click()
'Requires reference to Microsoft Office 12.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
'Clear listbox contents.
'Me.FileList.RowSource = ""
'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box.
.AllowMultiSelect = True
'Set the title of the dialog box.
.Title = "Please select one or more files"
.InitialFileName = "C:\Users\ABCCCCC\Desktop\January CMS reports for CCCCC"
'Clear out the current filters, and add our own.
.Filters.Clear
'.Filters.Add "Access Databases", "*.MDB; *.ACCDB"
.Filters.Add "Access Projects", "*.txt"
'.Filters.Add "All Files", "*.*"
'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to the list box.
For Each varFile In .SelectedItems
' Me.FileList.AddItem varFile
Call InsertCMS_Reports_2ndSave(varFile)
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
答案 0 :(得分:0)
如果您提供的代码已经有效,那么这应该适合您。
CurrentDb.Execute "UPDATE CopyOfCOMPRPT_CE SET FileName = '" & FileName & "' WHERE FileName is NULL", dbFailOnError
如果您遇到问题,最有可能是sql字符串和引号的语法问题将是最可能的罪魁祸首。如果遇到问题,请在代码中放置一个debug语句,以便查看生成的sql语句。例如:
Function InsertCMS_Reports_2ndSave(FileName As Variant)
Dim strSQL as String
'DoCmd.DeleteObject CopyOfCOMPRPT_CE, "CMS_Reports_2ndSave"
DoCmd.TransferText acImportFixed, "CMS_Reports_Import", _
"CMS_Reports_Import", "C:\Users\ABCCCCC\Desktop\January CMS reports for CCCCC\FileName"
strSQL = "UPDATE CopyOfCOMPRPT_CE SET FileName = '" & FileName & "' WHERE FileName is NULL", dbFailOnError"
debug.print strSQL
CurrentDb.Execute strSQL, dbFailOnError
End Function