VB6 - Query/StoredProc works in Access but not in VB6 - please help!

Thread Solved

Join Date: Nov 2007
Posts: 143
Reputation: jonifen is an unknown quantity at this point 
Solved Threads: 16
jonifen jonifen is offline Offline
Junior Poster

VB6 - Query/StoredProc works in Access but not in VB6 - please help!

 
0
  #1
Nov 19th, 2009
Hi,

I've got a bit of a problem with an application I am writing at the moment. I know it has to be something small which I'm missing, but I can't figure out what it is.

Basically, I have a Query (or Stored Procedure if you prefer) in an Access2000 database called qryApptList which has one parameter (DateOfAppt). This parameter is Text (as is the Date field in the database table).

If I run the query in Access, it prompts for the Date. I enter it in and it returns my results as it should. If I then do the same thing in VB6 using ADODB, it brings back nothing.

My VB6 code...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private m_oConnection As ADODB.Connection
  2. Private m_oCmdGetAppointments As ADODB.Command
  3.  
  4. 'Open the database connection
  5. Public Sub OpenDatabaseConnection()
  6. On Error GoTo ErrorHandler
  7.  
  8. If Len(m_sDatabasePath) = 0 Then
  9. m_sDatabasePath = m_oParentForm.FileSelection
  10. End If
  11.  
  12. Set m_oConnection = New ADODB.Connection
  13.  
  14. m_oConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  15. "Data Source=" & m_sDatabasePath
  16.  
  17. If m_oConnection.State <> adStateOpen Then
  18. Set m_oConnection = Nothing
  19. MsgBox "Could not open connection to database", vbCritical + vbOKOnly, App.Title
  20. Exit Sub
  21. End If
  22.  
  23. Exit Sub
  24. ErrorHandler:
  25. HandleError "clsDatabaseHandler:OpenDatabaseConnection Sub"
  26. End Sub
  27.  
  28.  
  29. 'Method to get the appointments from the database
  30. Public Function GetAppointments(ByVal sDate As String, ByRef oRsAppts As ADODB.Recordset)
  31. On Error GoTo ErrorHandler
  32.  
  33. If m_oCmdGetAppointments Is Nothing Then
  34. Set m_oCmdGetAppointments = New ADODB.Command
  35.  
  36. With m_oCmdGetAppointments
  37. Set .ActiveConnection = m_oConnection
  38.  
  39. .CommandText = "qryApptList"
  40. .CommandType = adCmdStoredProc
  41.  
  42. .Parameters.Append .CreateParameter("DateOfAppt", adVarChar, adParamInput, 11)
  43. End With
  44. End If
  45.  
  46. m_oCmdGetAppointments.Parameters("DateOfAppt") = sDate
  47.  
  48. Set oRsAppts = New ADODB.Recordset
  49.  
  50. oRsAppts.Open m_oCmdGetAppointments, , adOpenStatic, adLockReadOnly
  51.  
  52. Exit Function
  53. ErrorHandler:
  54. HandleError "clsDatabaseHandler:GetAppointments Function"
  55. End Function

All this code runs within a separate class module which is called from a form.

And if it helps... my SQL query.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. PARAMETERS DateOfAppt Text ( 255 );
  2. SELECT tblAppointments.ApptId, tblAppointments.User, tblAppointments.ApptDate, tblAppointments.ApptTime, tblAppointments.ClientId, tblAppointments.Comments, tblClients.Title, tblClients.FirstName, tblClients.Surname, tblClients.ContactNumber1
  3. FROM tblClients INNER JOIN tblAppointments ON tblClients.ClientId=tblAppointments.ClientId
  4. WHERE tblAppointments.ApptDate=[DateOfAppt];

I have had it working once and I had a recordcount of 1 in the recordset (I only have one record in the table by the way). I haven't knowingly changed anything but now I get 0 records every time.

Any help would be massively appreciated as I'm tearing out what little hair I have left!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 143
Reputation: jonifen is an unknown quantity at this point 
Solved Threads: 16
jonifen jonifen is offline Offline
Junior Poster
 
0
  #2
Nov 21st, 2009
For info, I have also tried it without the ADODB.Command object and hard-coded the SQL statement before opening the recordset against it but I still get nothing back from the database. As said before, the SQL query works perfectly in Access so I'm at a real loss here... has anyone got any ideas?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 143
Reputation: jonifen is an unknown quantity at this point 
Solved Threads: 16
jonifen jonifen is offline Offline
Junior Poster
 
0
  #3
Nov 21st, 2009
After much hair pulling and head scratching, I've got it working and believe it or not, it was a complete schoolboy error as I didn't clean up afterwards and destroy the command object so it was holding the original parameter value.

So I've moved the command from being module based to just method based (don't know why I had it in the module to start with!) and the amended code is as follows:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function GetAppointments(ByVal sDate As String, ByRef oRsAppts As ADODB.Recordset)
  2. On Error GoTo ErrorHandler
  3.  
  4. Dim oCommand As ADODB.Command
  5.  
  6. If oCommand Is Nothing Then
  7. Set oCommand = New ADODB.Command
  8.  
  9. With oCommand
  10. Set .ActiveConnection = m_oConnection
  11.  
  12. .CommandText = "qryApptList"
  13. .CommandType = adCmdStoredProc
  14.  
  15. .Parameters.Append .CreateParameter("DateOfAppt", adVarChar, adParamInput, 20)
  16. End With
  17. End If
  18.  
  19. oCommand.Parameters("DateOfAppt") = sDate
  20.  
  21. Set oRsAppts = New ADODB.Recordset
  22.  
  23. oRsAppts.CursorLocation = adUseClient
  24.  
  25. oRsAppts.Open oCommand, , adOpenStatic, adLockReadOnly
  26.  
  27. Set oCommand = Nothing
  28.  
  29. Exit Function
  30. ErrorHandler:
  31. HandleError "clsDatabaseHandler:GetAppointments Function"
  32. End Function

Thanks to anyone who looked in.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: mallak alrooh is an unknown quantity at this point 
Solved Threads: 0
mallak alrooh mallak alrooh is offline Offline
Newbie Poster

I have a problem >>> I need solutions

 
0
  #4
Nov 21st, 2009
Hi :
I have this problem, and I'm trying to solve it, I need your help#####@@@

write aprogramto generate a business travel express attaachment for an income tax return. The program should request ias input the name of the organization visited, the date and location of the visit,and the expenses for meals and entertainment,airplane fare,loding,and taxi fares.(Only 50% of the expenses for meals and entertainment are deductible) . Sub procedures should be used for the input and output .
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 143
Reputation: jonifen is an unknown quantity at this point 
Solved Threads: 16
jonifen jonifen is offline Offline
Junior Poster
 
0
  #5
Nov 22nd, 2009
Originally Posted by mallak alrooh View Post
Hi :
I have this problem, and I'm trying to solve it, I need your help#####@@@

write aprogramto generate a business travel express attaachment for an income tax return. The program should request ias input the name of the organization visited, the date and location of the visit,and the expenses for meals and entertainment,airplane fare,loding,and taxi fares.(Only 50% of the expenses for meals and entertainment are deductible) . Sub procedures should be used for the input and output .
It's not really that similar to my original query and therefore your question would be better placed in its own thread.
However, I'd recommend doing some research and finding the points that you think you'll have problems with. If you have code that you have written already that is causing you problems then even better, you could post it up and people can then see if they can spot the error or improve it in any way.

As it stands, it appears (from your post) that you are hoping for someone to provide you with the code to work with.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum


Views: 740 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC