How do I paste in an SQL code into my excel vba code?

In the code you posted you have defined the SQL, in a string variable.

You might have defined a function with the same name as the variable elsewhere in your code, I don't know.

As for replacing + with &, that's a general thing and has nothing to do with the problem.

You can keep using + if you want, it'll work most of the time but like I said it can cause problems.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
qry_CPI_Logged is a function that requires two arguments to be supplied - you aren't supplying any.
 
Upvote 0
Thanks for the input, I have sorted out the SQL string now, but in terms of arguments, presumably because the SQL string contains a date from and to is the reason for the need to enter an argument?
 
Upvote 0
A string doesn't require arguments, but you will need to give values to any variables you want to be part of the string.
 
Upvote 0
The way your last posted version is written, it only uses the first parameter so you could remove the table one.
 
Upvote 0
Thank you, as you said, tbl not necessary

but now I have another issue, the macro runs (yay)! but it's displaying hundreds more results than it should it do

I have even got rid of the inner join, which I know, will make zero difference

The sql string is this....qry_CPI_Logged = _
"SELECT tblCPILogged.Ref, tblCPILogged.[Logged By], tblCPILogged.[Logged Date] " _
+ "FROM tblCPILOgged " _
+ "WHERE (((tblCPILogged.[Logged Date])>=#" + CStr(convert_date(wc)) + "# And (tblCPILogged.[Logged Date])<#" + CStr(convert_date(wc + 7)) + "#));"

The sql in db is this SELECT tblCPILogged.Ref, tblCPILogged.[Logged By], tblCPILogged.[Logged Date]
FROM tblCPILogged
WHERE TblCPILogged.[Logged Date] >=#6/23/2014# AND TblCPILogged.[Logged Date] <#6/29/2014#;
 
Upvote 0
Without the data it's pretty difficult to say what's going on. Are any of the dates returned outside your date range?
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top