Re: Run Access Query From Excel With Parameter?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Indeed, the word exec (or execute, at long) is not required either, so just:

? CurrentProject.Connection.Execute("query13 7").Fields(0).Value

works too, with ADO, and it is not limited to Jet as targeted db. Note that if you have more than one parameter, you may have to be sure of the right order of the parameters, using an explicit PARAMETERS clause in your Jet query (something we have to do for crosstabs).


Building, explicitly, an ADO parameters collection is required when your stored proc return a value in one of the parameters, but that is not something Jet does with its stored procedures... or queries with parameters, if you prefer.



Vanderghast, Access MVP



"EricG" <EricG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:6A695F8E-FD86-428D-9A0C-E694579F9C34@xxxxxxxxxxxxxxxx
It's that simple? Wow! I added the parameter value in-line with the
procedure name, and it worked perfectly. Thanks for the help.

Eric

"vanderghast" wrote:

Assuming the database has a saved parameter query, query13 as example:


SELECT MAX(Ds.d)
FROM Ds
WHERE (((Ds.d)<=[limite]));



You can then EXEC it like this:


? CurrentProject.Connection.Execute("exec query13 7").Fields(0).Value


or use an open connection to the database. Note that (here the single)
argument is simply passed after the stored procedure... ooops, the saved
parameter query, name. Also, Execute produce a read only forward only
recordset, so here, I chose to read the value from the first field of the
first record it produced.



Vanderghast, Access MVP


"EricG" <EricG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:FADC4A3C-4250-41CB-A6D7-46BFF9488899@xxxxxxxxxxxxxxxx
> I'm using code like the following to run a stored procedure from Excel.
> Is
> there a similar method to run a stored procedure that requires a
> parameter?
> I can always hard code the SQL, but I would rather just be able to call
> the
> procedure and provide the parameter it needs.
>
> Thanks,
>
> Eric
>
> Sub RunAccessQueries_ADO()
>
> Dim cn As ADODB.Connection
> Dim cm As ADODB.Command
>
> dbPath = "d:\data\mypath\"
> dbName = "mydb.mdb"
>
> Set cn = New ADODB.Connection
> Set cm = New ADODB.Command
>
> With cn
> .CommandTimeout = 0
> .Provider = "Microsoft.Jet.OLEDB.4.0;"
> .ConnectionString = "Data Source=" & dbPath & dbName
> .Open
> End With
>
> With cm
> .CommandText = "qryMakeTable" ' Something like "qryMakeTable " &
> myParam?
> .CommandType = adCmdStoredProc
> .ActiveConnection = cn
> .Execute
> End With
> '
> cn.Close
> '
> End Sub
>


.



Relevant Pages

  • Re: Emptying Table in ADO
    ... Execute this TADOCommand, do not use TADODataSet or TADOQuery. ... http://www.oledbdirect.com - The fastest way to access MS SQL Server, ... MS Jet and Interbase ... >>> Anyone can suggest the best way of emptying table in ado? ...
    (borland.public.delphi.database.ado)
  • Re: [Access2003, VBA] Use DAO or ADO?
    ... One of the reasons my clients preferred Access / Jet / ODBC was that they ... Autonumber or its server equivalent and could have been shown to the users ... real world of development, Dot Net", ADO has been superceded by ADO.NET ...
    (comp.databases.ms-access)
  • Re: How to enforce subtypes/supertypes in Access 2000?
    ... DAO is the native object model for Jet databases and, as such, is the ... ever need ADO. ... Private Sub SetContactType() ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Use DAO or ADO?
    ... tables to a jet backend to open a ado recordset. ... will ALSO work for mysql, oracle and sql server if I take that sql ... engine that they don't understand who want to avoid Jet at all ...
    (comp.databases.ms-access)
  • Re: [Access2003, VBA] Use DAO or ADO?
    ... to a jet backend to open a ado recordset. ... Because one might have chosen to use ado in place dao in their application. ... You gain sql neutral code. ...
    (comp.databases.ms-access)