我正在尝试使用VBA将SQL语句用于报表的记录源。我知道SQL有效,因为我从有效查询的SQL视图复制了SQL。
此处是在报表的打开事件时触发的SQL记录源。我对SQL语句进行了一项更改,其中添加了一个变量strInput
,您可以在WHERE子句中看到该变量。
Private Sub Report_Open(Cancel As Integer)
Dim sql As String
Dim strInput As String
strInput = Me.OpenArgs
sql = "SELECT DISTINCTROW INV.NUM, INV.SUB, [INVD]![QTY]*([INVD]![VDR]+[INVD]![MATQ]) AS Expr1, [INVD]![QTY]*[INVD]![LPR] AS Expr2, [Expr1]+[Expr2] AS Expr3, [INVD]![MATQ]+[INVD]![VDR] AS Expr4, ([INVD]![MATQ]+[INVD]![VDR])*[INVD]![QTY]*0.8 AS Expr5" & _
" FROM (JOBS INNER JOIN UN ON JOBS.UNID = UN.JOBS) INNER JOIN ((CONT INNER JOIN INV ON CONT.CTNO = INV.CTNO) INNER JOIN INVD ON (INV.CTNO = INVD.CTNO) AND (INV.INV = INVD.INV)) ON (UN.UNID = INVD.UNID) AND (UN.CTNO = INVD.CTNO)" & _
" WHERE (((INV.JOB) = [Forms]![02- UNIT JOB INVOICE ENTRY]![JOB])) AND InvNum = ' " & strInput & "'" & _
" ORDER BY INV.CTNO, INV.JOB, INV.INV, INVD.ITEM;"
If Me.OpenArgs <> "" Then
Me.RecordSource = sql
End If
End Sub
我得到的错误是
此表达式的输入错误或过于复杂而无法 评估
我知道有类似的线程在打开,但是这些解决方案对我不起作用。谢谢。
编辑1:
根据您的回复,我删除了打开过程,并尝试使用您所说的where子句
MsgBox strInput
DoCmd.OpenReport "PRINT REPORT", acPreview, , "INVD.InvNum =' " & strInput & "'"
现在这给了我同样的错误
此表达式的输入错误或过于复杂而无法 评估
答案 0 :(得分:0)
解决方案是从WHERE子句中删除字符串引号,因为InvNum是一个数字。最终的Where子句看起来像这样
WHERE (((INV.JOB)=[Forms]![02- UNIT JOB INVOICE ENTRY]![JOB])) AND INVD.[InvNum] = " & strInput