首页 文章

编译错误:预期的结束语句

提问于
浏览
0

Microsoft Access 2010数据库给我以下错误消息:

Compile Error: Expected End Of Statement

这是抛出错误消息的方法:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Provide the user with the option to save/undo
    'changes made to the record in the form
    If MsgBox("Changes have been made to this record." _
        & vbCrLf & vbCrLf & "Do you want to save these changes?" _
        , vbYesNo, "Changes Made...") = vbYes Then
            DoCmd.Save
        Else
            DoCmd.RunCommand acCmdUndo
    End If

    Dim sSQL As String
    sSQL = "SELECT max(Clients.ClientNumber) AS maxClientNumber FROM Clients"
    Dim rs As DAO Recordset
    Set rs = CurrentDb.OpenRecordset(sSQL)
    MsgBox ("Max client number is: " & rs.Fields(1))
End Sub

抛出错误消息的代码行是:

Dim rs As DAO Recordset

我不确定问题是否与前面的行的语法有关 . 任何人都可以展示如何解决这个问题?并解释发生了什么?

2 回答

  • 0

    你在Sql语句的末尾正在修改分号

  • 3

    你错过了 DAORecordset 之间的句号(句点) - 它应该是

    Dim rs As DAO.Recordset
    

    除此之外,读取字段值时也会遇到运行时错误,因为DAO Fields集合的索引是从0而不是1.因此,将倒数第二行更改为:

    MsgBox ("Max client number is: " & rs.Fields(0))
    

    或者,通过名称引用该字段:

    MsgBox ("Max client number is: " & rs!maxClientNumber)
    

相关问题