Update query criteria code using variables

ot070707

New Member
Joined
Nov 12, 2008
Messages
6
My spreadsheet contains a query from an Access database.
I recorded a macro to edit the query's criteria.

The code is copied below.
This works fine whilst the criteria are hard coded into the macro.
However, I want the criteria to come from variables (via inputboxes or userform) so that the user can choose what to report on (in this case 'Employee' or 'Contractor').
I can't work out how to work the variable name into the criteria line of the macro without getting an error.
Can anyone help out.
Thank you.


Sub QueryUpdate()

Range("B4").Select
With Selection.QueryTable
.Connection = _
"ODBC;DSN=MS Access Database;DBQ=F:\TR MI.mdb;DefaultDir=F:\;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"
.CommandText = Array( _
"SELECT `Bookings Query for Recharge MI Report`.`Period Name`, `Bookings Query for Recharge MI Report`.SumOfQuantity, `Bookings Query for Recharge MI Report`.SGRate, `Bookings Query for Recharge MI Rep" _
, _
"ort`.Recovery, `Bookings Query for Recharge MI Report`.`Employee Cost Centre`, `Bookings Query for Recharge MI Report`.EmployeeContractor" & Chr(13) & "" & Chr(10) & "FROM `F:\TR MI`.`Bookings Query for Recharge MI Report` `Book" _
, _
"ings Query for Recharge MI Report`" & Chr(13) & "" & Chr(10) & "WHERE (`Bookings Query for Recharge MI Report`.`Cost Centre Recharge` Is Not Null) AND (`Bookings Query for Recharge MI Report`.EmployeeContractor='Contractor')" _
)
.Refresh BackgroundQuery:=False
End With
End Sub
<!-- / message -->
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Just a guess but wouldn't it be something like:

EmployeeContractor='Contractor')" _

changed to

EmployeeContractor='" & MyVariable & "')" _
 
Upvote 0
Thanks - I thought I'd tried something like that but I must have missed an apostrophe or space or something.
Anyway, it works just as I want.
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

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