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

I don't follow.

Are you getting a syntax error when you try and execute that query in the database?

Or are you getting it somewhere else?

PS There's a missing # after the first date.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Effectively I am asking how do I build that above code into my VBA and before that, what is the problem in the syntax?
 
Upvote 0
Thanks for that, of course, what an idiot, I am being.

I want to test if this SQL runs, but whilst doing so....it wants me to enter a parameter value for Ref, but my understanding was that I had denoted the table and the .then gave the extension, the extension being ref

Yep, the error is from the database
 
Upvote 0
What 'extension'?

The syntax to refer to a field name in SQL is [tablename].[fieldname].
 
Upvote 0
Okay, this is my SQL, I just need to put this in terms which vba understands

SELECT tblCPILogged.Ref, tblCPILogged.[Logged By], tblCPILogged.[Logged Date]
FROM TblStaffList INNER JOIN TblCPILogged ON TblStaffList.RESPONDID = TblCPILogged.[Logged By]
WHERE TblCPILogged.[Logged Date] >=#6/23/2014# AND TblCPILogged.[Logged Date] <=#6/27/2014#
 
Upvote 0
Well I'd construct that SQL statement in VBA like this.
Code:
strSQL = " SELECT tblCPILogged.Ref, tblCPILogged.[Logged By], tblCPILogged.[Logged Date] "
strSQL = strSQL & " FROM TblStaffList INNER JOIN TblCPILogged ON TblStaffList.RESPONDID = TblCPILogged.[Logged By] "
strSQL = strSQL & " WHERE tblCPILogged.[Logged Date] >= #6/23/2014# And tblCPILogged.[Logged Date] <= #6/27/2014#"
 
Upvote 0
Okay, so I did what you sugggested, I now have this error appearing

'NCR CPI Logging
Set rs = db.OpenRecordset(qry_CPI_Logged_productivity(wc), dbOpenDynaset)
Call print_recordset(rs, Sheet4, "CT5", "CT5:CV5005", 2)

It says sub or function not defined
 
Upvote 0
Do you have a sub/function called print_recordset?

An array/sub/function named qry_CPI_Logged_productivity?

Where are you referring to the SQL statement?
 
Upvote 0
Sub print_recordset(ByVal rs As Recordset, ByRef sht As Worksheet, ByVal start_cell As String, ByVal range_cell As String, ByVal fld As Integer)

I believe this is what you are referring to?
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,167
Members
448,870
Latest member
max_pedreira

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