简体   繁体   English

运行查询时出现运行时错误 3075 MS Access VBA

[英]Runtime Error 3075 MS Access VBA when running query

I'm trying to run this query via VBA code and I'm getting "Run -Time 3075 error".我正在尝试通过 VBA 代码运行此查询,但出现“Run -Time 3075 错误”。 I tried to reorganize in diferent ways but its not working out.我试图以不同的方式重组,但没有奏效。 It runs fine as a query.它作为查询运行良好。

Here is the original query:这是原始查询:

SELECT DISTINCT TB_Vendor.Vend_name, TB_POLine.ord_no, IIf(IsNull([cus_name]),StrConv([bill_to_name],3),StrConv([cus_name],3)) AS Customer, TB_POLine.user_def_fld_2, IIf(IsNumeric([CS_CLIENTOENTRY].[request_dt]) And IsNull([CS_CLIENTOENTRY1].[ord_dt]),DateSerial(Left$([CS_CLIENTOENTRY].[request_dt],4),Mid$([CS_CLIENTOENTRY].[request_dt],5,2),Right$([CS_CLIENTOENTRY].[request_dt],2)),IIf(IsNumeric([CS_CLIENTOENTRY1].[ord_dt]) And IsNull([CS_CLIENTOENTRY].[request_dt]),DateSerial(Left$([CS_CLIENTOENTRY1].[ord_dt],4),Mid$([CS_CLIENTOENTRY1].[ord_dt],5,2),Right$([CS_CLIENTOENTRY1].[ord_dt],2)),Null)) AS [Request Date], DateSerial(Left$([TB_POHeader].[ord_dt],4),Mid$([TB_POHeader].[ord_dt],5,2),Right$([TB_POHeader].[ord_dt],2)) AS [Order Date], IIf([TB_POLine].[promise_dt]<>0,DateSerial(Left$([TB_POLine].[promise_dt],4),Mid$([TB_POLine].[promise_dt],5,2),Right$([TB_POLine].[promise_dt],2)),Null) AS [Promise Date], TB_POHeader.user_name, TB_POHeader.ord_status, TB_POLine.move_to_loc, IIf([vchr_no]=0,"Received",IIf([vchr_no]>0,"Vouchered",IIf(DateDiff("d",Date(),[TB_POLine Query].[Pro Date])<=0,"Late","On Time"))) AS Status, TB_POHeader.cmt_2, TB_POHeader.cmt_3, TB_POHeader.cmt_1
FROM [TB_POLine Query] RIGHT JOIN (CS_Receivings RIGHT JOIN (CS_CLIENTOENTRY1 RIGHT JOIN (CS_CLIENTOENTRY RIGHT JOIN (TB_POHeader RIGHT JOIN (TB_Vendor RIGHT JOIN TB_POLine ON TB_Vendor.vend_no = TB_POLine.vend_no) ON TB_POHeader.ord_no = TB_POLine.ord_no) ON CS_CLIENTOENTRY.[Ticket #] = TB_POLine.user_def_fld_2) ON CS_CLIENTOENTRY1.[Ticket #] = TB_POLine.user_def_fld_2) ON CS_Receivings.ord_no = TB_POLine.ord_no) ON [TB_POLine Query].ord_no = TB_POLine.ord_no
GROUP BY TB_Vendor.Vend_name, TB_POLine.ord_no, IIf(IsNull([cus_name]),StrConv([bill_to_name],3),StrConv([cus_name],3)), TB_POLine.user_def_fld_2, IIf(IsNumeric([CS_CLIENTOENTRY].[request_dt]) And IsNull([CS_CLIENTOENTRY1].[ord_dt]),DateSerial(Left$([CS_CLIENTOENTRY].[request_dt],4),Mid$([CS_CLIENTOENTRY].[request_dt],5,2),Right$([CS_CLIENTOENTRY].[request_dt],2)),IIf(IsNumeric([CS_CLIENTOENTRY1].[ord_dt]) And IsNull([CS_CLIENTOENTRY].[request_dt]),DateSerial(Left$([CS_CLIENTOENTRY1].[ord_dt],4),Mid$([CS_CLIENTOENTRY1].[ord_dt],5,2),Right$([CS_CLIENTOENTRY1].[ord_dt],2)),Null)), DateSerial(Left$([TB_POHeader].[ord_dt],4),Mid$([TB_POHeader].[ord_dt],5,2),Right$([TB_POHeader].[ord_dt],2)), IIf([TB_POLine].[promise_dt]<>0,DateSerial(Left$([TB_POLine].[promise_dt],4),Mid$([TB_POLine].[promise_dt],5,2),Right$([TB_POLine].[promise_dt],2)),Null), TB_POHeader.user_name, TB_POHeader.ord_status, TB_POLine.move_to_loc, IIf([vchr_no]=0,"Received",IIf([vchr_no]>0,"Vouchered",IIf(DateDiff("d",Date(),[TB_POLine Query].[Pro Date])<=0,"Late","On Time"))), TB_POHeader.cmt_2, TB_POHeader.cmt_3, TB_POHeader.cmt_1
HAVING (((DateSerial(Left$([TB_POHeader].[ord_dt],4),Mid$([TB_POHeader].[ord_dt],5,2),Right$([TB_POHeader].[ord_dt],2)))>#1/1/2014#) AND ((TB_POHeader.ord_status)<>"C"))
ORDER BY TB_POLine.ord_no DESC;

And VBA Code I'm using和我正在使用的 VBA 代码

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim StrSql As String
Dim cns As QueryDef
Dim teste As String
Dim teste2 As String

Dim RstSql As QueryDef

'Set RstSql = Application.CurrentDb.QueryDefs("Consulta1")
Set db = CurrentDb()

StrSql = "SELECT DISTINCT TB_Vendor.Vend_name, TB_POLine.ord_no, IIf(IsNull([cus_name]),StrConv([bill_to_name],3),StrConv([cus_name],3)) AS Customer, TB_POLine.user_def_fld_2, IIf(IsNumeric([CS_CLIENTOENTRY].[request_dt]) And, " & _
"IsNull([CS_CLIENTOENTRY1].[ord_dt]),DateSerial(Left$([CS_CLIENTOENTRY].[request_dt],4),Mid$([CS_CLIENTOENTRY].[request_dt],5,2),Right$([CS_CLIENTOENTRY].[request_dt],2)),IIf(IsNumeric([CS_CLIENTOENTRY1].[ord_dt]) And, " & _
"IsNull([CS_CLIENTOENTRY].[request_dt]),DateSerial(Left$([CS_CLIENTOENTRY1].[ord_dt],4),Mid$([CS_CLIENTOENTRY1].[ord_dt],5,2),Right$([CS_CLIENTOENTRY1].[ord_dt],2)),Null)), " & _
"AS [Request Date], DateSerial(Left$([TB_POHeader].[ord_dt],4),Mid$([TB_POHeader].[ord_dt],5,2),Right$([TB_POHeader].[ord_dt],2)) AS [Order Date], " & _
"IIf([TB_POLine].[promise_dt]<>0,DateSerial(Left$([TB_POLine].[promise_dt],4),Mid$([TB_POLine].[promise_dt],5,2),Right$([TB_POLine].[promise_dt],2)),Null) AS [Promise Date], TB_POHeader.user_name, TB_POHeader.ord_status, TB_POLine.move_to_loc, TB_POHeader.cmt_2, TB_POHeader.cmt_3, TB_POHeader.cmt_1, " & _
"FROM TB_POLine Query] RIGHT JOIN (CS_Receivings RIGHT JOIN (CS_CLIENTOENTRY1 RIGHT JOIN (CS_CLIENTOENTRY RIGHT JOIN (TB_POHeader RIGHT JOIN (TB_Vendor RIGHT JOIN TB_POLine ON TB_Vendor.vend_no = TB_POLine.vend_no) ON TB_POHeader.ord_no = TB_POLine.ord_no) ON CS_CLIENTOENTRY.[Ticket #] = TB_POLine.user_def_fld_2) ON CS_CLIENTOENTRY1.[Ticket #] = TB_POLine.user_def_fld_2) ON CS_Receivings.ord_no = TB_POLine.ord_no) ON [TB_POLine Query].ord_no = TB_POLine.ord_no "

If Not IsNull(Comb1.Value) Then
    teste = teste & " [CS_POStatus].[user_name] = '" & Comb1.Value & "'"
End If

If teste = " WHERE " Then
    teste = " WHERE TB_CS_POStatus.[ser_name]='Null';"
Else
    teste = teste '& ";"

End If

Set rs = db.OpenRecordset(StrSql & teste)

Set RstSql = Application.CurrentDb.QueryDefs("CS_POStatus")
RstSql.SQL = StrSql & teste & " ORDER BY CS_POStatus.ord_no;"

If teste = ";" Then
    MsgBox ("Please select at least one search criteria.")
Else
    If rs.RecordCount = 0 Then
        MsgBox ("There are no results for this search")
    End If

    Lista8.Requery

    Me.Refresh

End If
End Sub

I rarely comment on code but I'm sorry to have to say that this code it truly horrendous and you shouldn't be surprised that you're getting errors you can't fix.我很少对代码发表评论,但我很抱歉不得不说这段代码真的很可怕,你不应该对你遇到无法修复的错误感到惊讶。

There are many kinds of wrong here, the biggest one being that this SQL query is impossible to read.这里有很多错误,最大的错误是这个 SQL 查询无法阅读。

Having said that:话说回来:

  • Your first line of SQL VBA end with And, , that , doesn't make sense.您的 SQL VBA 的第一行以And, , that 结尾,没有意义。

  • Same issue with the second line.第二行也有同样的问题。

  • Your third line also ends with an extra , which causes the AS [Request Date] on the next line to be on its own, which will also generate an error.您的第三行也以 extra 结尾,这会导致下一行的AS [Request Date]独立,这也会产生错误。

  • Your fourth line also ends with an extra , .您的第四行也以一个额外的,结尾。 Since ti's followed by FROM on the next line, you will also get an error message.由于 ti 在下一行跟在FROM之后,您还将收到一条错误消息。

  • Your sixth line misses a [ : FROM TB_POLine Query] should be FROM [TB_POLine Query] .你的第六行错过了[ : FROM TB_POLine Query]应该是FROM [TB_POLine Query]

You said that the queries were the same, but they are not.你说查询是一样的,但它们不是。 The query that you say works has a GROUP BY and a HAVING clause that the VBA query doesn't have...你说的查询有一个GROUP BY和一个 VBA 查询没有的HAVING子句......

You have to seriously consider being more meticulous:你要认真考虑更细致:

  • Format your SQL, even in VBA.格式化您的 SQL,即使是在 VBA 中。 Use something like SQL Pretty Printer or some other free software or online service to help you.使用SQL Pretty Printer或其他一些免费软件或在线服务来帮助您。

  • clean up your code: your code for building teste is probably not going to work in all the various cases you are trying to check.清理您的代码:您用于构建teste的代码可能无法在您尝试检查的所有各种情况下工作。

  • When testing VBA-build SQL code, put a breakpoint at the point where the SQL is passed to OpenRecordset , print it to the immediate window and copy it into a new SQL Query to check where the Query editor complains.在测试 VBA 构建的 SQL 代码时,在 SQL 传递给OpenRecordset的位置放置一个断点,将其打印到即时窗口并将其复制到新的 SQL 查询中,以检查查询编辑器抱怨的位置。 You would have found your errors if you had at least tried to debug the resulting VBA SQL into the Query editor.如果您至少尝试将生成的 VBA SQL 调试到查询编辑器中,您就会发现错误。

All I can predict, is that if you continue writing code like that, you're going to be in a lot of pain for a long time, and I wouldn't be surprised if some of what seems to work, actually doesn't work properly.我只能预测,如果你继续写这样的代码,你会在很长一段时间内痛苦不堪,如果某些看起来有效但实际上不起作用,我不会感到惊讶好好工作。

Write code that anyone can read.编写任何人都可以阅读的代码。
At least write code that you can read...至少写出能读懂的代码……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM