Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedures and ADO

Status
Not open for further replies.

abombss

Programmer
Apr 13, 2000
51
US
I would like to know if anyone has attempted to use stored queries and ADO w/ Access 97.&nbsp;&nbsp;I read a KB article on MSDN discussing a bug with ADO and how to use stored procedures with Access 2000.&nbsp;&nbsp;I have not yet been able try it with Access 97 and I was wondering if anyone has.<br><br>qb18
 
Try something like the following:<br><br>==========<br>Public Const ADOConnect As String = &quot;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Reporting;Data Source=<font color=red>SERVERNAMEHERE</font>&quot;<br><br>Private Sub GetStoredProcedure()<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim rs As ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim cmd As ADODB.Command<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim adoCN As ADODB.Connection<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set adoCN = New ADODB.Connection&nbsp;&nbsp;&nbsp;&nbsp;' Normally on initial open of application<br>&nbsp;&nbsp;&nbsp;&nbsp;adoCN.Open (ADOConnect)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Normally on initial open of application<br>&nbsp;&nbsp;&nbsp;&nbsp;Set cmd = New ADODB.Command<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;With cmd<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set .ActiveConnection = adoCN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CommandType = adCmdStoredProc<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CommandText = &quot;<font color=red>sp_SPNameHere</font>&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Use the following two lines if you have a parameter to pass<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Set paritem = .CreateParameter(&quot;Keyword&quot;, adVarChar, adParamInput, 255, <font color=red>Me.txtValue</font>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.Parameters.Append paritem<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = New ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;rs.Open cmd, , adOpenStatic<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;' Do whatever you want here .......<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Set cmd = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Set adoCN = Nothing<br>End Sub<br>==========<br><br>I am not positive I have all of the stuff right, but it's something close to this. All of the ADO I have done is in VB, and it should be close to the same. Unfortunately, I do not have SQL Server available at my present job to test this with, so good luck. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
ADO from access 97 does work you will need mdac 2.1 or above 2.5 is latest and to goto to preferences and select ms active data objects ,<br><br>The first section makes the connection , change the vars to your info and the fnc call is to run the stored proc.<br><br><br>Dim cn As New ADODB.Connection<br>Dim rs As ADODB.Recordset<br>Dim sys4 As Variant, sys5 As Variant<br><br>Public Function fncADOConnect()<br>'Use&nbsp;&nbsp;This To transfer data to SQL Box - Check tblparameters for connection information<br>'No Parameters<br>' Connect to SQL server through SQL Server OLE DB Provider.<br>'Temp Variables<br>Dim conSQL As String, strDate As String<br>Dim conDB As Database<br>Dim conRS As Recordset<br><br><br>On Error GoTo Err_Attach_SQL<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Set conDB = CurrentDb()<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;' Get Paths<br>&nbsp;&nbsp;&nbsp;&nbsp;conSQL = &quot;select * from tblParameters where fileno=1&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set conRS = conDB.OpenRecordset(conSQL, DB_OPEN_SNAPSHOT)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Set connection properties.<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.ConnectionTimeout = 25<br>&nbsp;&nbsp;&nbsp;&nbsp;'cn.ConnectionTimeout = 600<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Provider = &quot;sqloledb&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Properties(&quot;Data Source&quot;).Value = conRS!SQLServerAddress<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Properties(&quot;Initial Catalog&quot;).Value = conRS!SQLDatabaseName<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Properties(&quot;User ID&quot;).Value = conRS!SQLLogonName<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Properties(&quot;Password&quot;).Value = conRS!SQLPassword<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;' Open the database.<br>&nbsp;&nbsp;&nbsp;&nbsp;cn.Open<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;conRS.close<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Truncate Data From All User Tables The user must be a db_owner<br>&nbsp;&nbsp;&nbsp;&nbsp;'fncExecSQL (&quot;xp_AddRole&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;fncExecSQL (&quot;xp_TruncateAll&quot;)<br><br><br>Public Function fncExecSQL(strSQL As String)<br><br>On Error GoTo Err_Attach_SQL<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = CreateObject(&quot;ADODB.Recordset&quot;)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Open recordset.<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = cn.Execute(strSQL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;fncExecSQL = True<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>Exit_Attach_SQL:<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Function<br><br>Err_Attach_SQL:<br>&nbsp;&nbsp;&nbsp;&nbsp;fncExecSQL = False<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Encountered error:&nbsp;&nbsp;&quot; & str$(Err) & Chr(10) & Error$, , &quot;Attach_SQL Error&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Resume Exit_Attach_SQL<br><br>End Function <p>Dave<br><a href=mailto:dab@completebs.com>dab@completebs.com</a><br><a href= Bsuiness Systems</a><br>See website for more info but we can develop most things for most people.
 
Stored Procedures are not supported by the Jet engine which you would probably use to communicate with your Access database. ADO works very well however, with both Access97 and 2000. I have written extensively for both including ADOX and it all works fine.<br><br>With the limitations of Jet it is best to pass the sql as adCmdText. If you want to use command procedures then you need to be running Sql Server as your engine.<br><br>A good book is ADO Programming in VB6 by Holzner which covers everything.<br><br>Watch out that you always set the cursor mode to client with jet when working with ADODB recordsets as otherwise the cursor content is unavailable.<br><br>good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top