Private Sub CmdPharmacy_Click()
Dim myM(11) As String
Set mydb = CurrentDb
If PMNM = "" Then
PMNM = UCase(Left(cboMonth.Value, 3))
End If
sQ = "SELECT mN FROM PharDate WHERE mT = '" & PMNM & "';"
Set myrec = mydb.OpenRecordset(sQ, dbOpenDynaset)
myrec.MoveFirst
myC = myrec.Fields("mN")
For i = 0 To myC - 1
myM(i) = "M" & i + 1
Next i
For i = myC To 11
myM(i) = "NDATA"
Next i
'mydb.TableDefs.Delete ("PHAR_REPORT")
sQ = "SELECT HistoryData2.PR, HistoryData2.CC, " & _
"HistoryData2." & myM(0) & " as U1, HistoryData2." & myM(1) & " as U2, HistoryData2." & myM(2) & " as U3, HistoryData2." & myM(3) & " as U4, " & _
"HistoryData2." & myM(4) & " as U5, HistoryData2." & myM(5) & " as U6, HistoryData2." & myM(6) & " as U7, HistoryData2." & myM(7) & " as U8, " & _
"HistoryData2." & myM(8) & " as U9, HistoryData2." & myM(9) & " as U10, HistoryData2." & myM(10) & " as U11, HistoryData2." & myM(11) & " as U12," & _
"Revenue." & myM(0) & " as R1, Revenue." & myM(1) & " as R2, Revenue." & myM(2) & " as R3, Revenue." & myM(3) & " as R4, Revenue." & myM(4) & " as R5, " & _
"Revenue." & myM(5) & " as R6, Revenue." & myM(6) & " as R7, Revenue." & myM(7) & " as R8, Revenue." & myM(8) & " as R9, Revenue." & myM(9) & " as R10, " & _
"Revenue." & myM(10) & " as R11, Revenue." & myM(11) & " as R12, " & _
"INTO PHAR_REPORT FROM HistoryData2, Revenue WHERE (((HistoryData2.PR) = Revenue.PR) And " & _
"((Revenue.PR)>=76700000 And (Revenue.PR)<=76900000)) ORDER BY HistoryData2.PR;"
当我尝试执行sQ时,我收到运行时错误..... SELECT语句包含拼写错误或缺失的保留字或参数名称,或者标点符号不正确。
SELECT HistoryData2.PR, HistoryData2.CC, HistoryData2.M1 as U1, HistoryData2.M2 as U2, HistoryData2.M3 as U3, HistoryData2.M4 as U4, HistoryData2.M5 as U5, HistoryData2.M6 as U6, HistoryData2.M7 as U7, HistoryData2.M8 as U8, HistoryData2.NDATA as U9, HistoryData2.NDATA as U10, HistoryData2.NDATA as U11, HistoryData2.NDATA as U12 Revenue.M1 as R1, Revenue.M2 as R2, Revenue.M3 as R3, Revenue.M4 as R4, Revenue.M5 as R5, Revenue.M6 as R6, Revenue.M7 as R7, Revenue.M8 as R8, Revenue.NDATA as R9, Revenue.NDATA as R10, Revenue.NDATA as R11, Revenue.NDATA as R12
INTO PHAR_REPORT
FROM HistoryData2, Revenue
WHERE (((HistoryData2.PR) = Revenue.PR) And ((Revenue.PR)>=76700000 And (Revenue.PR)<=76900000))
ORDER BY HistoryData2.PR;
答案 0 :(得分:2)
您需要查看正在尝试执行的已完成的SQL语句。在mydb.Execute之前添加一行,如下所示:
Debug.Print sQ
mydb.Execute sQ
Debug.Print将在立即窗口中打印语句。 (您可以使用CTRL + g到达那里。)复制语句,然后打开一个新查询,切换到SQL视图并将其粘贴到那里,然后尝试。希望这项工作能帮助您找出问题所在。如果您无法解决问题,请编辑您的问题以包含该声明,以便我们也能看到它。
更新:您问题的最新版本包含字段列表中最后一个字段后面的逗号:
"Revenue." & myM(10) & " as R11, Revenue." & myM(11) & " as R12, " & _
在R12之后删除该逗号。
Update2 :David Fenton在您收录的SQL语句中发现了缺少的逗号。我希望丢失的逗号导致错误3075,“查询表达式中的语法错误(缺少运算符)...”。但是,您的上一个报告指出您收到的错误消息是“条件表达式中的数据类型不匹配”。我怀疑我们一直在处理移动目标。您已经报告了至少3种不同的错误消息。您发布的SQL语句与您向我们展示的VBA代码不一致。
如果您的错误消息仍然是“条件表达式中的数据类型不匹配”,请尝试此查询并告诉我们它给您的内容:
SELECT "HistoryData2" AS table_name, TypeName(PR) AS pr_data_type
FROM HistoryData2
UNION ALL
SELECT "Revenue" AS table_name, TypeName(PR) AS pr_data_type
FROM Revenue;
答案 1 :(得分:1)
在SQL语句的SELECT子句中,您有:
HistoryData2.NDATA as U12 Revenue.M1 as R1
应该是这样的:
HistoryData2.NDATA as U12, Revenue.M1 as R1
即缺少逗号。
但是,我在代码中看到连接字符串,尾随的逗号就在那里。我无法解释这种差异,除非您更改了代码,或者没有从正确的源派生SQL。