Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

Error 5 with SQL Query in VBA with Long "In" Clause

I am building a macro that includes a SQL statement. The SQL statement references a cell for its "in" clause (cell G5 in the code below). If I reference two or three item numbers in this cell, the query executes as expected, but when I put a lot of item numbers (around ten or so), the query stops working and I get error 5: invalid procedure call or argument. Does anybody know how to work around this?

Here's the SQL query I'm trying to run... please let me know if you want the full VBA code for troubleshooting, but I'm cutting down to just the SQL and cell reference definition for brevity.

Dim myval As String
myval = Range("g5").Value
"select c.item_num, avg(s.ext_net_sls_amt/s.ordr_qty) as ASP" & Chr(13) & "" & Chr(10) & _
"from sls_tbl s" & Chr(13) & "" & Chr(10) & _
"inner join cust_tbl c on s.item_ID = c.item_ID" & Chr(13) & "" & Chr(10) & _
 "where c.item_num in (" & myval & ") and s.ordr_qty <> 0" & _
 "and s.invoice_dt between 20200723 and 20201023 group by c.item_num")
question from:https://stackoverflow.com/questions/65941137/error-5-with-sql-query-in-vba-with-long-in-clause

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...