1

我已经整理了一个过程来循环遍历包含文本文件路径的表并将它们导入数据库。

程序的原因: 这样做的原因是我正在为许多依赖于每晚更新的文本文件的报告数据库构建后端。最近他们更改了这些文件的服务器名称和文件名,所以我正在尝试构建更可靠的东西,这样我就不必通过链接表向导来确保所有数据类型都与以前完全相同。

问题: 我遇到的问题是 With .edit .update 的行为不像我认为的那样,并将表中的“更新”字段更新为今天的日期。

这是代码。我还是编程新手,所以很抱歉。

Private Sub ImportAll()
' Loops through table containing paths to text files on network and imports
Dim ID As Integer
Dim netPath As String
Dim netDir As String
Dim netFile As String
Dim localTable As String
Dim activeTable As Boolean
Dim updatedTable As Date
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("Tables")

Do Until rst.EOF
    ID = rst.Fields("Table ID").Value
    netDir = rst.Fields("Network Location").Value
    netFile = rst.Fields("File Name").Value
    localTable = rst.Fields("Local Table Name").Value
    activeTable = rst.Fields("Active").Value
    updatedTable = rst.Fields("Updated").Value

        If activeTable = True And updatedTable <> Date Then
            If ifTableExists(localTable) Then
                On Error GoTo ImportData_Err
                        CurrentDb.Execute "DELETE * FROM " & localTable, dbFailOnError
                        netPath = netDir & netFile
                        DoCmd.TransferText acImportDelim, , localTable, netPath, True, ""
                            rst.Edit
                            updatedTable = Date
                            rst.Update
                    Else
                        netPath = netDir & netFile
                        DoCmd.TransferText acImportDelim, , localTable, netPath, True, ""
                            With rs
                                .Edit
                                  .Fields("Updated") = Date
                                .Update
                            End With
            End If
        End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
ImportData_Exit:
            Exit Sub
ImportData_Err:
            MsgBox Error$
            Resume ImportData_Exit
End Sub

谢谢你。

4

1 回答 1

0

你在哪里

With rs

你的意思是

With rst

通过打开 可以发现诸如此类的错误Option Explicit。Option Explicit 意味着必须声明所有变量。

请参见此处:如何强制 VBA/Access 要求定义变量?

于 2013-06-12T00:12:40.210 回答