OK I thought I had this down, but I guess it's not working...
I'm trying to run an access query from excel (the access query queries an Oracle ODBC). I know, I should query that ODBC directly, and that may be an upgrade in the future.
Anyway, I have a UserForm with 3 text boxes prompting the user for Project Number, Fiscal Year, and Accounting Period.
I then set those text boxes equal to 3 variables:
Dim ProjectNo As String
Dim FiscalYr As Long
Dim AcctPd As Long
ProjectNo = Me.txtPN.Value
FiscalYr = Me.txtFY.Value
AcctPd = Me.txtAP.Value
And then I recorded the code for the MS Access query and inserted the variables where I need them to be (VARIABLES IN BOLD):
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=F:\Program Finance Folder\Labor Report db1.mdb;DefaultDir=F:\Program Finance Folder;DriverId=25;FIL=" _
), Array("MS Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range( _
"A4"))
.CommandText = Array( _
"SELECT `Labor Report`.E_ORIG_WK_END_DT, `Labor Report`.REG_HRS, `Labor Report`.OT_HRS, `Labor Report`.E_FPH, `Labor Report`.Q_RF_PROJECT, `Labor Report`.Q_RF_ACTIVITY, `Labor Report`.NAME, `Labor Repo" _
, _
"rt`.DESCR, `Labor Report`.Q_SUPV_NAME, `Labor Report`.FISCAL_YEAR, `Labor Report`.ACCOUNTING_PERIOD" & Chr(13) & "" & Chr(10) & "FROM `F:\Program Finance Folder\Labor Report db1`.`Labor Report` `Labor Report`" & Chr(13) & "" & Chr(10) & "WHERE (`Labor Repo" _
, _
"rt`.Q_RF_PROJECT Like ProjectNo) AND (`Labor Report`.FISCAL_YEAR=FiscalYr) AND (`Labor Report`.ACCOUNTING_PERIOD=AcctPd)" & Chr(13) & "" & Chr(10) & "ORDER BY `Labor Report`.E_ORIG_WK_END_DT" _
)
.Name = "Query from MS Access Database_12"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Result:
When I run this query with numerical information in place of the variables, it works great... but when I run it with the variables in the code, it gives me a "general ODBC error" and highlights the " .Refresh BackgroundQuery:=False" line... that is telling me that something is wrong with my query, but I don't understand why variables wouldn't work for parameters in the query.
Any idea how to get the user-defined variables to work as parameters in that query?
Many thanks in advance.
Bookmarks