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

时间:2017-01-20 22:14:32

标签: vba ms-access access-vba

我最初想要设置一个文本框,用户可以在其中键入文本文件的日期并单击导入(我创建了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)导入这些文件时,如何将文件名添加到最后一列,并为每一行显示文件名。

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

1 个答案:

答案 0 :(得分:0)

这应该做你想要的。我也在那里写了一些有用的评论。

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。

http://www.digitalcitizen.life/how-create-task-basic-task-wizard

最后,如果我是你,我会把这作为一夜之间的工作。

    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.