2013-10-23 64 views
0

一个微软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 

我不确定问题是否必须做用它前面的行的语法。任何人都可以展示如何解决这个问题?并解释发生了什么?

回答

3

你缺少DAORecordset之间的句号(句号) - 它应该是

Dim rs As DAO.Recordset 

除此之外,你也将有一个运行时错误阅读的字段值,因为DAO字段可替换地

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

,通过其名称引用字段:收集从0,而不是1。因此索引,倒数第二行改变到这个

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

不,那不是我的建议 - 读一遍...使用*或者*'rs.Fields(0)'*或* rs!maxClientNumber' - 你的'rs!Fields(0)'是一个混乱。 –

+0

如果有人想知道这是对现在已被删除的评论的回复,虽然这个观点仍然存在(你不能使用砰的操作员按位置进行索引)... –

+0

这不是问题所在。 – CodeMed

0

你在你的SQL语句的结束mising分号