Error when running a parameter query from access 2007 in excel vba -


i'm trying run query in access 2007 database excel 2007 vba script. access query has parameters called "year" , "month".

i'm trying following code work:

sub runmyquery()      dim cn new adodb.connection     dim rs new adodb.recordset     dim dbpath string     dim stqry string     dim stcon string     dim cmd new adodb.command     dim prmyear new adodb.parameter     dim prmmonth new adodb.parameter      dbpath = "<path_to_my_db>"      stcon = "provider=microsoft.ace.oledb.12.0;" _          & "data source=" & dbpath & ";"      cn.open (stcon)     cn.cursorlocation = aduseclient      set cmd.activeconnection = cn      set prmyear = cmd.createparameter("year", adnumeric, adparaminput, , 2011)     set prmmonth = cmd.createparameter("month", adnumeric, adparaminput, , 5)      cmd.parameters.append prmyear     cmd.parameters.append prmmonth      cmd.commandtext = "select * [month_totals]"     cmd.commandtype = adcmdtable      set rs = cmd.execute      sheets("sheet1").range("a1").copyfromrecordset rs      rs.close     set rs = nothing     cn.close     set cn = nothing  end sub 

when run this, code stops on "cmd.execute" with

run-time error '-214217900 (80040e14)':

syntax error in clause.

what getting wrong?

  • the command text seems simple enough me. missing there?

  • am misusing parameters functionality of adodb.command? don't think that's problem here, because i've tried running same script non-parametrized query substituted month_totals, , gotten same error.

i believe parameters use when using saved query in access. solve problem moving parameters sql statment.

change

"select * [month_totals]" 

to

"select * [month_totals] year = 2011 , month = 5" 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -