vba 编译错误:预期的语句结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19531039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compile error: expected end of statement
提问by CodeMed
A Microsoft Access 2010 database is giving me the following error message:
Microsoft Access 2010 数据库给我以下错误消息:
Compile Error: Expected End Of Statement
Here is the method that is throwing the error message:
这是抛出错误消息的方法:
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
The line of code that is throwing the error message is:
抛出错误消息的代码行是:
Dim rs As DAO Recordset
I am not sure if the problem has to do with the syntax of what is on the line preceding it. Can anyone show how to fix this problem? And explain what is going on?
我不确定问题是否与它前面一行的语法有关。任何人都可以展示如何解决这个问题?并解释发生了什么?
回答by Chris Rolliston
You are missing a full stop (period) between the DAO
and the Recordset
- it should be
您错过了DAO
和之间的句号(句点)Recordset
- 应该是
Dim rs As DAO.Recordset
Beyond that, you will also have a runtime error on reading the field value, since a DAO Fields collection is indexed from 0, not 1. Hence, change the penultimate line to this:
除此之外,您还会在读取字段值时遇到运行时错误,因为 DAO 字段集合是从 0 而非 1 索引的。因此,将倒数第二行更改为:
MsgBox ("Max client number is: " & rs.Fields(0))
Alternatively, reference the field by its name:
或者,通过名称引用该字段:
MsgBox ("Max client number is: " & rs!maxClientNumber)
回答by Cayucodies
You're mising the semicolon at the end of your Sql statement
您错过了 Sql 语句末尾的分号