VB3 How to Call SQL Stored Procedures from Visual BasicID: Q106492
|
This article describes how to call Microsoft SQL stored procedures from Visual Basic. A stored procedure is a precompiled collection of SQL statements, often including control-of-flow language.
The method of calling depends on whether the SQL stored procedure returns
records or not:
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.
DB_SQLPassThrough = 64
Data1.Options = DB_SQLPassThrough
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 will ignore the syntax used and will pass the command
through to the SQL server.
Dim Ds as Dynaset
Set MyDB = OpenDatabase(... ' Open your desired database here.
Set Ds = MyDB.CreateDynaset("sp_name",Db_SQLPassThrough)
' You can also Dim as Snapshot and use MyDb.CreateSnapshot above.
SQLx = "My_StorProc parm1, parm2, parm3" ' String specifying SQL
' command.
...
i = MyDB.ExecuteSQL(SQLx) ' For stored procedure that
' doesn't return records.
...
set Ds = MyDB.CreateDynaset(SQLx,64) ' For stored procedure that
' returns records.
Dim db as Database; l as long; Ss as Snapshot
' Enter the following two lines as one, single line:
Set Db = OpenDatabase
("",false,false, "ODBC;dsn=yourdsn;uid=youruid;pwd=yourpwd")
l=ExecuteSQL("YourSP_Name") ' for SPs that don't return rows
Set Ss = Db.CreateSnapshot("YourSP_Name", 64) ' for SPs that return rows
Col1.text = Ss(0) ' Column one
Col2.text = Ss!ColumnName
Col3.Text=Ss("ColumnName")
More information about calling stored procedures is documented in the
following Microsoft SQL manual which covers the Visual Basic Library
for SQL Server:
Additional query words: 3.00
Keywords :
Version : 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 16, 1999