如何修复此编译错误:“预期:表达式”

时间:2011-02-17 15:57:44

标签: sql ms-access vba

当用户更新个案的状态时,需要在帐号下的所有案例中标记该更改。在VB中,我试图使用以下SQL来更新表,其中包含用户放入表单的信息。

DoCmd.RunSQL "UPDATE [Main Details] " & _

"SET [Main Details].[Status] = '" & Status & "' " & _

"AND [Main Details].[On Hold] = '" & On Hold & "' " & _

"WHERE   [Main Details].[Account] = '" & Account & "';"

但是,我收到了编译错误,我怀疑它与“暂停”有关。我怎么能纠正这个?任何有关这方面的帮助将不胜感激。

对于上下文,我在一个名为“主要详细信息”的表单上查看名为“ReportSelection”的组合框。在“After Update”事件过程中,有以下代码:

Private Sub
 ReportSelection_AfterUpdate()

     If ReportSelection = "Enforcement Letter" Or ReportSelection = "Fees
 Letter" Or ReportSelection = "Follow
 On Letter" Or ReportSelection =
 "Reminder Letter BO" Or
 ReportSelection = "Reminder Letter CR"
 Or ReportSelection = "Reminder Letter
 CT" Or ReportSelection = "Reminder
 Letter NNDR" Or ReportSelection =
 "Reminder Letter RTD" Or
 ReportSelection = "Reminder Letter SD"
 Then
                 CmbStatus = "HOLD Until"
                 [On Hold] = Date + 5
             End If

     DoCmd.RunSQL "UPDATE [Main Details] " & _
       "SET [Main Details].[Status] = '" & Status & "' " & _
       "AND [Main Details].[On Hold] = '" & On Hold & "' " & _
       "WHERE   [Main Details].[Account] = '" & Account &
 "';"

     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
 acMenuVer70

     Select Case Me!ReportSelection

     Case "Write Email"

         DoCmd.OpenForm "CaseEmail", acNormal, , , acFormEdit,
 acWindowNormal

         Exit Sub

     Case "Arrangement Letter"

         Set dbs = CurrentDb

        DoCmd.RunSQL "SELECT * FROM [Arrangements] " & _
             "WHERE [Client]  = '" & Me!Client & "' " & _
             "AND   [Account] = '" & Me!Account & "' " & _
             "AND   [Status]  = 'Made';"

         Set rst = dbs.OpenRecordset(SQL)

         On Error GoTo ArrangementNotFound

         rst.MoveFirst

         ' rst!PaymentCode

         rst.Close
         Set dbs = Nothing

         GoTo RunReport
          ArrangementNotFound:

         rst.Close
         Set dbs = Nothing

         MsgBox "No arrangement has been made for this account"

         Exit Sub

     Case "Reminder Letter", _
         "Reminder Letter BO", _
         "Reminder Letter CR", _
         "Reminder Letter CT", _
         "Reminder Letter NNDR", _
         "Reminder Letter RTD", _
         "Reminder Letter SD", _
         "Enforcement Letter", "Commital Letter"

         If [Status] = "HOLD" Then
             MsgBox "Order is on HOLD", vbExclamation
             Exit Sub
         End If

         If Me![Bailiff Name] <> "" Then
             MsgBox "Order is with " & Me![Bailiff Name], vbExclamation
             Exit Sub
         End If

         If [First Letter] <> 0 Then
             GoTo RunReport
         Else
             MsgBox "Order has not yet been 1st Noticed", vbExclamation
             Exit Sub
         End If

     End Select
      RunReport:

     Select Case Me!ReportSelection

     Case "Details - Account", "Nulla Bona - All Cases", "Arrangement
 Letter"

         WhereCondition = "[Client]='" & Me!Client & "' AND [Account]='" &
 Me!Account & "'"

     Case Else

         WhereCondition = "[Reference]=" & Forms![Main
 Details]!Reference

     End Select

     On Error GoTo InvalidReport

     DoCmd.OpenReport Me![ReportSelection], acViewPreview, ,
 WhereCondition, acWindowNormal

     Select Case Me!ReportSelection

     Case "Council Tax Seizure"

         '*** DO NOTHING ***

     Case "Details"

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) & "_" &
 Trim(Me!Summons) & ".pdf{ENTER}", True

             Sleep 500

             DoCmd.Close acReport, Me!ReportSelection, acSaveNo

         End If

     Case "Details - Account"

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) &
 ".pdf{ENTER}", True

             Sleep 500

             DoCmd.Close acReport, Me!ReportSelection, acSaveNo

         End If

     Case "Nulla Bona"

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) & "_" &
 Trim(Me!Summons) & "NB.pdf{ENTER}",
 True

             Sleep 500

             DoCmd.Close acReport, Me!ReportSelection, acSaveNo

         End If

         DoCmd.OpenReport "Details", acViewPreview, , "[Reference]=" &
 Forms![Main Details]!Reference,
 acWindowNormal

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) & "_" &
 Trim(Me!Summons) & ".pdf{ENTER}", True

             Sleep 500

             DoCmd.Close acReport, "Details", acSaveNo

         End If

     Case "Nulla Bona - All Cases"

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) &
 "NB.pdf{ENTER}", True

             Sleep 500

             DoCmd.Close acReport, Me!ReportSelection, acSaveNo

         End If

         DoCmd.OpenReport "Details - Account", acViewPreview, ,
 "[Client]='" & Me!Client & "' AND
 [Account]='" & Me!Account & "'",
 acWindowNormal

         If Me!Return Then

             Sleep 1000

             SendKeys "%(fp)v{ENTER}Z:\Returns\" & Me!Client
 & "\" & Trim(Me!Account) &
 ".pdf{ENTER}", True

             Sleep 500

             DoCmd.Close acReport, "Details - Account", acSaveNo

         End If

     Case Else

         '------------------------------------------+
         ' STAMP EACH CASE WITH TYPE OF LETTER SENT |
         '------------------------------------------+

         Set con = Application.CurrentProject.Connection

         SQL = "INSERT INTO [Free Type] ( Reference, [Text], Username ) " & _
             "SELECT DISTINCTROW Reference, '" & _
             ReportSelection & " Sent', '" & _
             [Forms]![Current User]![Initials] & "' " & _
             "FROM [Main Details] " & _
             "WHERE Client  = '" & [Forms]![Main Details]![Client] & "' "
 & _
             "AND   Account = '" & [Forms]![Main Details]![Account] &
 "';"

         con.Execute SQL

     End Select

     Exit Sub

 InvalidReport:

     MsgBox "This report is currently unavailable, please try again later."

End Sub

此外,值得一提的是,类似的代码片段适用于不同的组合框,代码中没有任何相关的声明。

抱歉之前没有提供太多的上下文,这是我第一次涉足VB,SQL和Access。

1 个答案:

答案 0 :(得分:3)

简短回答是:不要在字段或表名中使用空格。

很明显,您迫切需要更好地理解Access基础知识,但是现在我只会集中精力让您超越当前的障碍。

当您使用字段名称中的空格向访问中的表单添加字段时,Access会以静默方式替换带下划线的空格。因此,以下代码应该与您当前设计的数据库一起使用:

CurrentDb.Execute "UPDATE [Main Details] " & _
"SET [Main Details].[Status] = '" & Status & "' " & _
", [Main Details].[On Hold] = '" & On_Hold & "' " & _
"WHERE   [Main Details].[Account] = '" & Account & "';", dbFailOnError

我还在您的UPDATE语句中将AND更改为逗号,将DoCmd.RunSQL替换为CurrentDb.Execute并使用dbFailOnError选项,以便您的查询不会抛出对话框盒子或静默失败(如果你关闭警告,就像大多数人使用DoCmd.RunSQL一样)。