Ms access 导入包含文件名的文本文件

Ms access 导入包含文件名的文本文件,ms-access,vba,Ms Access,Vba,我最初想设置一个文本框,用户可以在其中键入文本文件的日期,然后单击导入(我创建了from并进行了一些编码,但失败了,无法找到s) 支持我的问题)。这将获取文本文件并将其导入到表中 这些是需要导入到表中的每周报告。 我的最佳选择是通过研究、跟踪和错误得出一个宏观的结论 这是我的密码: Function InsertCMS_Reports_2ndSave() 'DoCmd.DeleteObject Table, "CCS_Reports_2ndSave" DoCmd.Transfer

我最初想设置一个文本框,用户可以在其中键入文本文件的日期,然后单击导入(我创建了from并进行了一些编码,但失败了,无法找到s) 支持我的问题)。这将获取文本文件并将其导入到表中

这些是需要导入到表中的每周报告。 我的最佳选择是通过研究、跟踪和错误得出一个宏观的结论 这是我的密码:

Function InsertCMS_Reports_2ndSave()
    'DoCmd.DeleteObject Table, "CCS_Reports_2ndSave"
    DoCmd.TransferText acImportFixed, "CCS_Reports_Import", _
    "CCS_Reports_Import", "C:\Users\ABCDEF2\Desktop\January CCS reports for Centene\ABC_COMPRPT_1701011028174_h0062.txt"
End Function
它所做的是,当我启动数据库时,它会自动执行宏,因为我构建了一个宏并为其指定了名称autoexec。宏所做的是根据我创建的导入规范将文本文件数据添加到表中。令人惊讶的是,它完美地导入了它,但我正在尝试做更多的事情。我可能需要创建多个线程/问题,所以现在,我只在这里包括一个问题

1) 导入这些文件时,如何将文件名添加到最后一列,并使每一行都显示文件名


因此,在这一点上,我必须在宏打开后不断禁用它,或者将文件名更改为新的每周文件,保存它,关闭它,然后重新打开它。这不是最有效的方法,但似乎可能有效。

这应该可以满足您的需要。我也在里面放了一些有用的评论

Private Sub Command1_Click()

        Dim strPathFile As String, strFile As String, strPath As String
        Dim strTable As String
        Dim blnHasFieldNames As Boolean

        ' Change this next line to True if the first row in EXCEL worksheet
        ' has field names
        blnHasFieldNames = False

        ' Replace C:\Documents\ with the real path to the folder that
        ' contains the EXCEL files
        strPath = "C:\Users\Excel\Desktop\test\"

        ' Replace tablename with the real name of the table into which
        ' the data are to be imported
        strTable = "tablename"

        strFile = Dir(strPath & "*.txt")
        Do While Len(strFile) > 0
              strPathFile = strPath & strFile
              DoCmd.TransferText acImportDelim, , strTable, strPathFile, True

              On Error Resume Next
              CurrentDb.Execute "ALTER TABLE tablename ADD FileName CHAR(25)"

              CurrentDb.Execute "UPDATE tablename SET FileName = '" & _
              strFile & "' WHERE FileName IS NULL", dbFailOnError

        strFile = Dir()
        Loop

End Sub
至于装货。你什么时候拿到文件?我想每天都会收到一次文件。这现实吗?您可以根据下面的教程运行Windows TaskScheduler

最后,如果我是你,我会把这当作一份通宵工作

    Create an AutoExec macro

    If you have already created a macro that contains the actions that you want to occur when the database starts, just rename the macro AutoExec, and it will run the next time that you open the database. Otherwise, follow these steps to create a macro:

        On the Create tab, in the Other group, click Macro. If this command is unavailable, click the arrow beneath either the Module or the Class Module button, and then click Macro.

        In the Macro Builder, in the first empty Action cell, select the action that you want to perform. If applicable, under Action Arguments, type the appropriate values in

 the argument boxes.

    If you cannot find the action you want, on the Design tab, in the Show/Hide group, make sure Show All Actions is selected. This expands the list of actions that you can use, but the list will include some actions that will only run if the database is granted trusted status. For more information, see the articles Decide whether to trust a database or How database objects behave when trusted and untrusted.

    Repeat step 2 for each additional action you want to occur.

    Click Save, and in the Save As dialog box, type AutoExec.

    Click OK and then close the Macro Builder. The new macro will run the next time that you open the database.

如果我只是运行一个更新查询来更新字段,那么我到底在哪里执行该查询呢?我最终使用了一个更新查询,这就解决了我的问题。如果有人知道一个更有效的方法,请让我知道。有人知道我如何执行这个,而不必总是启动数据库,因为它是宏?我也可以选择时让它运行吗?在VBA中:
Docmd.OpenQuery“QueryName”
Docmd.RunSql“querysql”