Access 2007,VBA:编译错误:预期的结束语句

时间:2016-06-24 12:12:45

标签: vba access

在Access 2007中工作。努力让数据库根据货件/收据提供实时库存更新量。我被推荐以下代码,但它抛出了编译错误。

Private Sub Command62_Click()
'Arguments: ContID = Container to be reported on
' AsOfDate = the date at which quantity is to be calculated
'If missing, all transactions are included
' Return: Quantity on hand. Zero on error.

Dim db As DAO.Database   'current database
Dim rs As DAO.Recordset    ' various recordsets

Dim lngContID As Long       ' ContID as long
Dim strAsOf As String       ' AsOfDate as string
Dim strLasShipRec As String   ' Last ship date as string
Dim strDateClause As String    'Date clause to be used in SQL statement
Dim strSQL As String            ' SQL statement
Dim lngOnHand As Long     'IOnHand inventory

If Not IsNull(CONTID) Then
'Initialize: Validate and Convert parameters

Set db = CurrentDb()
lngContID = CONTID
If IsDate(AsOfDate) Then
strAsOf = "#" & Format$(AsOfDate, "mm\/dd\/yyyy") & "#"

'Get the LastShipRec date and quantity fo this container

If Len(strAsOf) > 0 Then
strDateClause = "AND (LastShipRec<="&strAsOf&")"
End If

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")"&strDateClause)&";ORDER BY LastShipRec DESC;"

它正在给出一个预期的结束语&#39;编译:

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")"&strDateClause)&";ORDER BY `enter`LastShipRec DESC;"

AND

If Len(strAsOf) > 0 Then

strDateClause = "AND (LastShipRec<="&strAsOf&")"
End If

有人能够指出我正确的方向吗?如何更正此错误?

2 个答案:

答案 0 :(得分:1)

strSQL = ...的行正在尝试构建一个长字符串。通过这个冗长的陈述,它看起来像一个括号不合适,并在第一行有一个额外的分号。尝试

strSQL = "Select Top 1 LastShipRec, Quantity FROM tblContInventory"& "WHERE ((ContID = "&lngContID&")" & strDateClause & ") ORDER BY LastShipRec DESC;"

在构建sql字符串时,我建议创建字符串,然后执行debug.print进行调试,以查看创建的字符串是否是有效的SQL语句。

答案 1 :(得分:0)

从strDateClause之后删除括号,看起来它需要进入“”,你的SQL也有终结符;在结束之前。看一下,你不要使用strDateClause