I would like to have a user enter a parameter into a cell in a worksheet, then run a saved query on that worksheet that uses that (cell reference) parameter. Has anyone ever done this that could help me?
Try turning on the macro recorder, then step through the edit query process (this will write a VB SQL version of your query), complete all the steps including reurn data to excel, then stop the recorder. then alt+f11 to view the code that you have recorded. it will look something like this. Ive added things you wont see so look for the line "With Selection.QueryTable", in that section you'll see where I substituted " + Trim(Str$(date_start)) + " for the parameter that was origionaly recorded, the SQL statement is a string so you have to concatenate your cell reference accordingly!
such as: my_string=sheets("sheet1".range("a1"
then code the variable my_string into the sql statement
-----------------------------------------------------
Private Sub Worksheet_Activate()
End
Cells(1, 1).Select 'ensures that reserved range in the spreadsheet is valid
rq$ = InputBox("Do you want to Update the Test Hours data?", "Re-Query the Data source", "N"
If UCase(rq$) <> "Y" Then End
get_dates 'branches to sub to get users date range via inputbox
With Selection.QueryTable
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=N:\ODBC_Databases\General Sample Queries.mdb;DefaultDir=N:\ODBC_Databases;DriverId=25;FIL=MS Access;MaxBuf" _
), Array("ferSize=2048;PageTimeout=15;")
.CommandText = Array( _
"SELECT `hours worked CH`.LABAREAS, `hours worked CH`.REFRNUMB, `hours worked CH`.MTRLCODE, `hours worked CH`.MTLCLASN, `hours worked CH`.TESTCODE, `hours worked CH`.DTARDLAB, `hours worked CH`.DTESETU" _
, _
"P, `hours worked CH`.DTCOMAPP, `hours worked CH`.SMPLSTAT, `hours worked CH`.RSLTSEQN, `hours worked CH`.AVLBLHRS, `hours worked CH`.TESTDESR, `hours worked CH`.MTLCODED" & Chr(13) & "" & Chr(10) & "FROM `c:\mcgon_stuff\General " _
, _
"Sample Queries`.`hours worked CH` `hours worked CH`" & Chr(13) & "" & Chr(10) & "WHERE (`hours worked CH`.DTCOMAPP>=" + Trim(Str$(date_start)) + " And `hours worked CH`.DTCOMAPP<=" + Trim(Str$(date_stop)) + "" _
)
.Refresh BackgroundQuery:=False
End With
End Sub
---------------------------------
Hope this is some help!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.