If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > VBA code in Access to get rs from Stored Procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-11, 01:35
hollis hollis is offline
Registered User
 
Join Date: Sep 2011
Posts: 12
Question VBA code in Access to get rs from Stored Procedure

I have a stored procedure in MS SQL 2005 and want to create a recordset in Access 2007 VBA from a stored procedure that accepts input for data range for records to be included in the recordset.

The following line works in Management Studio Query window when executed:

EXEC [dbo].[EZ-2000] '1/15/2011','1/30/2011'

I run the code below and it seems to spend some time running the SP but the recordset does not return any records.

Any help/ideas?


-- ealier code to define SP input dates ---

Dim StartDate As Date
Dim EndDate As Date
-- Start and End dates are taken from Access form --
StartDate = CDate(Me.Date_Billed_From_Date)
EndDate = CDate(Me.Date_Billed_To_Date)
-- When I debug, these dates show up as same dates I use in Query above --

--- main code ----

Dim Cmd1 As New ADODB.Command
Dim rs As New ADODB.Recordset


' On Error GoTo Err_CompcareExcel_Click
Set Cmd1 = New ADODB.Command

Cmd1.ActiveConnection = "Provider=SQLOLEDB;Data Source=xxx;Initial Catalog=xxx;Integrated Security=SSPI;"
Cmd1.CommandText = "[dbo].[EZ-2000]"
Cmd1.CommandType = adCmdStoredProc

-- I tried this line without the above 2 lines and next 3 and same results --
'Cmd1.CommandText = "EXEC [dbo].[EZ-2000] '1/15/2011','1/30/2011'"
-- I used the next line to verify that connection to server is fine and it did return a recordset ---
'Cmd1.CommandText = "SELECT * FROM Known_Table"

Cmd1.Parameters.Refresh
Cmd1.Parameters("@BegDate").Value = StartDate
Cmd1.Parameters("@EndDate").Value = EndDate

Set rs = Cmd1.Execute()
rs.Open Cmd1

--- above code runs for a while and any later code that tries to extract data from rs results in error message that rs is not open ---
Reply With Quote
  #2 (permalink)  
Old 09-01-11, 19:49
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
It's been a while since I used VBA but isn't there a Command.Parameters.Add method that you should be using for setting up your parameters?

Also, can't you create a Connection object and then assign it to the active connection rather than using the connection string approach? From memory there are some differences with how these work and it might be a connection issue not a query execution issue.
Reply With Quote
  #3 (permalink)  
Old 09-02-11, 02:34
hollis hollis is offline
Registered User
 
Join Date: Sep 2011
Posts: 12
Thanks, rok, for the input, but I recently posted that I know it is not a connection issue since I get it to work on simple stored procedure.

You are right about the parameter comment and the append is apparently more efficient but more variables involved so I'm using this method first to get it to work and then I will optimize.

Thanks!
Reply With Quote
  #4 (permalink)  
Old 09-05-11, 17:47
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
Dates can sometimes be problematic when you are passing them through.

Have you checked to see exactly what is being run on the server? Can you run a profile and see what the parameters are being set to?

When you say the code runs for a while do you know what line it actually "stalls" on? Is it the refresh or the open??
Reply With Quote
  #5 (permalink)  
Old 09-05-11, 18:01
hollis hollis is offline
Registered User
 
Join Date: Sep 2011
Posts: 12
Thanks, again. You are right about dates being tricky, but I created a simple SP with dates and have that part working fine. I believe the problem might be that it takes too long and that I need to change the timeout time. Do you know how to increase timeout parameter?

Last edited by hollis; 09-05-11 at 18:05.
Reply With Quote
  #6 (permalink)  
Old 09-06-11, 00:12
hollis hollis is offline
Registered User
 
Join Date: Sep 2011
Posts: 12
Problem is fixed

I was able to fix this. The problem was the name of the SP. It used a hyphen. I removed the hyphen (only after hours and hours of work and pulling out my hair) and all works fine now.

I had similar thread in VB section but wanted to close this one up in case someone has same problem I had and comes across this thread for help.
Reply With Quote
Reply

Tags
access 2003 recordset vba, stored procedure, vba

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On