Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How would you return the values when calling a Sybase stored procedure call ?

Posted on 2008-10-15
7
Medium Priority
?
354 Views
Last Modified: 2013-11-27
I am developing an Access mdb application using Access as the front end and I call a sybased stored procedure named sp3D_GetListOfDocuments. My code is the Attach Code Snippet.

How would you modify this code to execute the stored procedure that returns
a result set which is comprised of the following 3 columns:
1) MailID
2) NoOfPages
3) NoOfAttributes
This stored procedure does not require any parameters.

My goal is to load the result set records by APPENDING THEM into the table tblNameAddressMod.  

In the following Attach Code Snippet, the table tblNameAddressMod has the same 3 fields:

1) MailID
2) NoOfPages
3) NoOfAttributes

For Example, the result set could have the following values:
------------------------------------------------------------------------

MailID                               NoOfPages                          NoOfAttributes
-----------------                 ----------------                       -------------------
200707091208                1                                           3
200707091235                1                                           3
200707091356                1                                           3

Private Sub Command9_Click()
Dim com As ADODB.Command
Dim recNameAdress As ADODB.Recordset
Dim cmd As ADODB.Command
Dim oConn
Dim strMailID As String
Dim strNoOfPages As String
Dim strNoOfAttributes As String
 
DoCmd.Hourglass True
On Error Resume Next
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "DSN=N_DIG_MAIL;" & _
 "Uid=PC;" & _
 "Pwd=po"
 
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
 
rst.Open "SELECT MailID FROM tblNameAddressMod", CurrentProject.Connection
 
Set cmd = New ADODB.Command
With cmd
    .ActiveConnection = oConn
    .CommandType = adCmdText
    .CommandText = "exec sp3D_GetListOfDocuments"
    Set recNameAdress = .Execute
End With
 
strMailID = recNameAdress("MailID")
strNoOfPages = Nz(recNameAdress("NoOfPages"), " ")
strNoOfAttributes = Nz(recNameAdress("NoOfAttributes"), " ")
 
CurrentProject.Connection.Execute "INSERT INTO tblNameAddress Set MailID ='" & Replace(strMailID, "'", "''") & "', " & _
"NoOfPages ='" & strNoOfPages & "', NoOfAttributes ='" & strNoOfAttributes & "    "
 
recNameAdress.Close

Open in new window

0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:borki
ID: 22728261
Hi Zimmer9

I think you were almost there!

You can directly execute a stored procedure (SP) using ADO with MS SQL (I don't have Sybase).

You did not specify the data types of the columns returned. I would have thought that the 2nd and 3rd one were numeric. You need to watch out for conversion problems.

While your approach to build a SQL Insert statement is valid, I prefer to use a recordset.

Hope that helps

Felix Burkhard


Private Sub test()
   Dim oConn As New ADODB.Connection
   Dim oCmd As New ADODB.Command
   Dim recSP As New ADODB.Recordset
   Dim recMod As New ADODB.Recordset
    
   DoCmd.Hourglass True
   On Error Resume Next
   
   'Prepare command object with SP
   oConn.ConnectionString = "DSN=N_DIG_MAIL;Uid=PC;Pwd=po"
   With oCmd
       .ActiveConnection = oConn
       .CommandType = adCmdStoredProc
       .CommandText = "sp3D_GetListOfDocuments"
   End With
       
   'Open command object - executes SP
   recSP.Open oCmd
   If recSP.EOF Then
      'Handle error...
      Exit Sub
   End If
   
   'Open local dataset
   recMod.Open "tblNameAddressMod", CurrentProject.Connection, adOpenForwardOnly, adLockOptimistic, adCmdTable
   
   'Loop through records retrieved from SP
   Do While Not recSP.EOF
      'Copy data from SP output into new local record
      recMod.AddNew
      recMod!MailID = recSP!MailID
      recMod!NoOfPages = Nz(recSP!NoOfPages, " ")
      recMod!NoOfAttributes = Nz(recSP!NoOfAttributes, " ")
      recMod.Update
      'Advance cursor to next row
      recSP.MoveNext
   Loop
   recSP.Close
   recMod.Close
   Set recSP = Nothing
   Set recMod = Nothing
   DoCmd.Hourglass False
End Sub

Open in new window

0
 

Author Comment

by:zimmer9
ID: 22729939
I am a user who needs to access a Sybase database and call a stored procedures exposed to me to get information as a Sybase result set. I don't have any choice in the matter.

0
 

Author Comment

by:zimmer9
ID: 22730040
Can anyone confirm whether or not the solution above proposed by Felix should work as is  
using ADO with Sybase ? I have tried implementing it and as I step throught the execution I get to the following line in which the recordset is opened and the EOF is reached immediatately and the subroutine exits.

'Open command object - executes SP
   recSP.Open oCmd
   If recSP.EOF Then
      'Handle error...
      Exit Sub
   End If


thanks,
zimmer9
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 9

Expert Comment

by:borki
ID: 22735156
Hi Zimmer9

I just re-checked my code, and I think I forgot to open the connection. Try to add these 2 lines

10a   oConn.Provider = "ASEOLEDB"
11a   oConn.Open

Let me know how you go

0
 

Author Comment

by:zimmer9
ID: 22742242
Where is the linkage between the result set from the execution of stored procedure sp3D_GetListOfDocuments             and the record set recSP ?

By the way, I use ODBC with DataDirect 4.0 Sybase Wire Protocol driver.
0
 

Author Comment

by:zimmer9
ID: 22742274
Don't I need a .Execute line in the following With/End With ?

With oCmd
       .ActiveConnection = oConn
       .CommandType = adCmdStoredProc
       .CommandText = "sp3D_GetListOfDocuments"
End With
0
 
LVL 9

Accepted Solution

by:
borki earned 2000 total points
ID: 22751574
Hi Zimmer9

There are two types of commands you can have with ADO,
1) use Execute to run an Action query (Insert/Update/Delete)
2) use Recordset.Open to run a Select query that returns rows of data

Did you try the code with the modification I sent?

I am not sure what DataDirect 4 for Sybase is and does... I gound some references on the Sybase site that uses "ASEOLEDB" - what appears to be a specific OLEDB provider for Sybase. Not sure where ODBC fits in, but if you are using it, I would think that your remote data is already accessible via a linked table.

Good luck



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question