Hi,
My question is similar to the post: Filtering in VB report.
I tested a Excel macro that tries to run a MS Query to imort data from MS Access. It worked fine until I wanted to put a parameter in the Sub. The error that I have is 'Runtime error 13: type mismatch'. When debugging, it highlights ".CommandText...)" part of the code. It must be a quoting problem.
Thanks in advance!
Sub rawquery(mkt As String)
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\jqz\cadi\aetna\aetna_state_May08.mdb;DefaultDir=C:\jqz\cadi\aetna;DriverId=25;FIL=MS Acce" _
), Array("ss;MaxBufferSize=4096;PageTimeout=5;")), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT rebt_period, state, formularydesign, PLAN_NAME, mkt, product, mail_retail_cd, med_ind, " & _
"mb_flag, rx_count, total_prod_units, wac_sales, rebates, mkt_rx, mkt_total_prod_units, mkt_shr, unit_shr " & _
"FROM `C:\jqz\cadi\aetna\aetna_state_May08`.aetna_state " & _
"where aetna_state.mkt=" & mkt & """")
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub runit()
rawquery "ARB"
End Sub
My question is similar to the post: Filtering in VB report.
I tested a Excel macro that tries to run a MS Query to imort data from MS Access. It worked fine until I wanted to put a parameter in the Sub. The error that I have is 'Runtime error 13: type mismatch'. When debugging, it highlights ".CommandText...)" part of the code. It must be a quoting problem.
Thanks in advance!
Sub rawquery(mkt As String)
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\jqz\cadi\aetna\aetna_state_May08.mdb;DefaultDir=C:\jqz\cadi\aetna;DriverId=25;FIL=MS Acce" _
), Array("ss;MaxBufferSize=4096;PageTimeout=5;")), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT rebt_period, state, formularydesign, PLAN_NAME, mkt, product, mail_retail_cd, med_ind, " & _
"mb_flag, rx_count, total_prod_units, wac_sales, rebates, mkt_rx, mkt_total_prod_units, mkt_shr, unit_shr " & _
"FROM `C:\jqz\cadi\aetna\aetna_state_May08`.aetna_state " & _
"where aetna_state.mkt=" & mkt & """")
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub runit()
rawquery "ARB"
End Sub