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 -->
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 -->