MS Access VBA:更新语句中的语法错误

时间:2018-07-13 16:02:45

标签: ms-access syntax access-vba

我在MS Access中编译VBA代码方面相对较新。我正在尝试编译一个脚本,该脚本执行增量行更新,以便向基础表添加行号。

我当前的VBA脚本如下:

Function HOP_Macro()
On Error GoTo HOP_Macro_Err

    Dim db As DAO.Database
    Dim Tbl As DAO.Recordset
    Dim sql As String
    Dim Row_Counter As Integer
    Dim Total_Row As Integer

    Set db = CurrentDb

    'Initialising Row Counter variable
    Row_Counter = 1

    DoCmd.SetWarnings False

    ' Query to extract initial loan population
    'DoCmd.OpenQuery "Qry - HOP loan data by month", acViewNormal, acEdit

    ' Query to determine the population size
    'DoCmd.OpenQuery "Qry - Row Counter", acViewNormal, acEdit

    ' Sets Total_Row variable to the population count
    Total_Row = DLookup("[Row Count]", "[Tbl - Record Counter]")
    'MsgBox ("Total Row: " & Total_Row)
    'MsgBox ("Row Counter: " & Row_Counter)

    sql = "SELECT [Tbl - HOP Loan Data].[Row ID] FROM [Tbl - HOP Loan Data];"
    Set Tbl = db.OpenRecordset(sql, dbOpenDynaset)
    'MsgBox ("SQL 1: " & sql)

    With Tbl
        Do Until Tbl.EOF
            If Row_Counter <= Total_Row Then
                'MsgBox ("Hello")
                sql = "UPDATE [Tbl - HOP Loan Data.Row ID] = " & Row_Counter & _
                " Where [Tbl - HOP Loan Data.Row ID] < " & Row_Counter & " ;"
                'MsgBox ("SQL 2: " & sql)
                db.Execute (sql)
                Row_Counter = Row_Counter + 1
            End If
            .MoveNext
        Loop
    End With

    MsgBox ("Row Counter: " & Row_Counter)

    DoCmd.SetWarnings True
    Beep
    MsgBox "Data extract complete", vbInformation, "Completed"

End Function

运行上述脚本时,出现“ UPDATE语句中的语法错误”消息。我找不到错误。有人可以帮忙吗?

谢谢

2 个答案:

答案 0 :(得分:0)

您需要在更新查询中使用SET,您可能只是错过了将其添加到查询中所需的所有VBA内容。

我的建议(通常)是通过Access正常编写查询,然后修改该查询以在VBA中使用。

例如,像这样首先编写此查询(在Access SQL编辑器中,出于测试目的):

UPDATE [Tbl - HOP Loan Data]
SET [Tbl - HOP Loan Data].[Row ID] = 1
WHERE [Tbl - HOP Loan Data].[Row ID] < 1

无论如何,请尝试以下操作:

sql = "UPDATE [Tbl - HOP Loan Data] " & _
    "SET [Tbl - HOP Loan Data].[Row ID] = " & Row_Counter & _
    "WHERE [Tbl - HOP Loan Data].[Row ID] < " & Row_Counter & " ;"

注意:根据您的表结构,WHERE语句很有可能是不必要的。

答案 1 :(得分:0)

找到解决方案。

Function Set_Row_ID()

    Dim db As DAO.Database
    Dim Tbl As DAO.Recordset
    Dim sql As String
    Dim Row_Counter As Integer
    Dim Total_Row As Integer

    Set db = CurrentDb

    'Initialising Row Counter variable
    Row_Counter = 1

    sql = "SELECT [Tbl - HOP Loan Data].[Row ID] FROM [Tbl - HOP Loan Data];"
    Set Tbl = db.OpenRecordset(sql, dbOpenDynaset)

    With Tbl
        Do Until Tbl.EOF
            If Row_Counter <= Total_Row Then
                sql = "UPDATE [Tbl - HOP Loan Data " & _
                    "SET [Row ID] = " & Row_Counter & _
                    "Where [Row ID] < " & Row_Counter & " ;"
                db.Execute (sql)
                Row_Counter = Row_Counter + 1
            End If
            .MoveNext
        Loop
    End With

End Function