【问题标题】:Adding Filename to a column with your textfile Import使用您的文本文件导入将文件名添加到列中
【发布时间】:2017-06-08 08:27:23
【问题描述】:

我已经构建了一个表单,用户可以在其中选择一个或多个文件并将它们导入到一个表中。当用户选择文件,或者多个文件时,一旦导入完成,我希望在每一行添加文件名,当然,与正确的文件相关。

我可以设置一个查询来手动添加文件名,但我怎样才能以更自动化的方式做到这一点。例如,如果用户选择了一个文件,我如何编写 SQL 查询来自动检测文件名并添加它?如果用户选择了多个文件,查询如何为每一行写入正确的文件名?

这是我的表单代码:

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

模块代码:

Function InsertCMS_Reports_2ndSave(FileName As Variant)
    '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"
    CurrentDb.Execute "UPDATE CopyOfCOMPRPT_CE SET FileName = 'HLTH_COMPRPT_1701011028174_h0062.txt' WHERE FileName is NULL", dbFailOnError
End Function

【问题讨论】:

    标签: ms-access import vba ms-access-2010


    【解决方案1】:

    如果您提供的代码已经可以工作,那么这应该适合您。

    CurrentDb.Execute "UPDATE CopyOfCOMPRPT_CE SET FileName = '" & FileName & "' WHERE FileName is NULL", dbFailOnError
    

    如果您遇到问题,很可能是 sql 字符串的语法问题,而引号最有可能是罪魁祸首。如果遇到问题,请在代码中添加调试语句,以便查看生成的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多