Article ID: 146651
Article Last Modified on 6/29/2004
i% = MyDb.ExecuteSQL("sp_name")
This executes the stored procedure sp_name and returns the affected
number of rows in i%. The ExecuteSQL method is strictly for action
queries such as:
Delete Authors where name like "fred%"
The ExecuteSQL() method is valid only for SQL statements that do not
return records (or rows). An SQL statement that uses "SELECT..." returns
records, while an SQL statement that uses "DELETE..." does not. Neither
Execute() nor ExecuteSQL() return a recordset, so using ExecuteSQL() on
a query that selects records produces an error.
Data1.Options = dbSQLPassThrough
Data1.Recordsource = "sp_name" ' name of the stored procedure.
Data1.Refresh ' Refresh the data control.
When you use the SqlPassThrough bit, Visual Basic's Microsoft Access
database engine ignores the syntax used and passes the command
through to the SQL server.
Dim Ds as Recordset
' Open your desired database here.
Set MyDB = DBEngine.Workspaces(0).OpenDatabase(...
Set Ds = MyDB.OpenRecordset("sp_name", dbOpenDynaset, _
dbSQLPassThrough)
You can also use 'dbOpenSnapshot' in place of 'dbOpenDynaset' above.
' String specifying SQL.
command.SQLx = "My_StorProc parm1, parm2, parm3"
...
' For stored procedure that doesn't return records.
i = MyDB.ExecuteSQL(SQLx)
...
'For stored procedure that returns records.
set Ds = MyDB.OpenRecordset(SQLx, dbOpenDynaset, dbSQLPassThrough)
The object variable (Ds) contains the first set of results from the
stored procedure (My_StorProc).
Dim db as Database
Dim l as Long
Dim Ss as Recordset
Set Db = DBEngine.Workspaces(0).OpenDatabase _
("", False, False, "ODBC;dsn=yourdsn;uid=youruid;pwd=yourpwd:")
' For SPs that don't return rows.
l=Db.ExecuteSQL("YourSP_Name")
' For SPs that return rows.
Set Ss = Db.OpenRecordset("YourSP_Name", dbOpenSnapshot, _
dbSQLPassThrough)
Col1.text = Ss(0) ' Column one.
Col2.text = Ss!ColumnName
Col3.Text=Ss("ColumnName")
Additional query words: kbVBp400 kbdse kbDSupport kbVBp
Keywords: kbhowto KB146651