Results 1 to 7 of 7

Thread: Problem returning Recordset

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    14

    Problem returning Recordset

    Hi

    I am trying to return a recordset from a function in a class back to my form in order to display the data.

    Code is as follows:

    In my Form -

    Dim bs as New Bookstore
    Public rsResult = New ADODB.Recordset

    Private Sub mnuBook_Click()

    Dim strIsbn As String

    strIsbn = InputBox("Enter the ISBN", "Search by ISBN")
    If strIsbn = "" Then
    MsgBox "No ISBN entered", vbOKOnly, "Search"
    Else
    Showdata(bs.findbyIsbn(strIsbn))
    End If

    End Sub

    Private Sub Showdata(rsResult As ADODB.Recordset)
    txtIsbn.Text = rsResult.Fields("ISBN")
    (...display into all my other text boxes....)

    End Sub

    and in my Bookstore Class:

    Public Function findbyIsbn(strIsbn As String) As ADODB.Recordset
    Dim rsResult As New ADODB.Recordset
    Dim strSQL As String

    strSQL = "SELECT Author.*, Books.* FROM Author INNER JOIN Books ON Author.[Author Code] = Books.[Author Code] WHERE (Books.ISBN)='" & strIsbn & "';"

    rsResult.Open strSQL, mcon1, adOpenDynamic, adLockOptimistic
    Set findbyIsbn = rsResult
    End Function


    The problem is I get a Type Mismatch error on the - Showdata(bs.findbyIsbn(strIsbn)) - line of code in my form

    Its probably something really simply, but I fairly new to VB and any advice would be appreciated

    Cheers

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    This is how i do it.


    in the DLL/Class Module

    VB Code:
    1. Function GetAllEmployees() As Object 'Set The returned Recordset as an Object
    2. Dim CN As New ADODB.Connection
    3. Dim RS As New ADODB.Recordset
    4. CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind"
    5. CN.CursorLocation = adUseClient
    6. RS.Open "SELECT * FROM Employees", CN, adOpenStatic, adLockOptimistic
    7. Set GetAllEmployees = RS 'This Retunrs the REcordset to the Function
    8. Set RS = Nothing
    9. Set CN = Nothing
    10. End Function



    Then in Your Form something like this.


    VB Code:
    1. Private Sub Command1_Click()
    2. Dim Rs As New ADODB.Recordset
    3. Dim MyRS As New SQLServerDLL.Interface ' Set refrences to ADO and your DLL
    4. Set Rs = MyRS.GetAllEmployees ' Set you Recordset = The recor returned by your Function
    5. MSFlexGrid1.Cols = Rs.Fields.Count
    6. MSFlexGrid1.Rows = Rs.RecordCount + 1
    7.  
    8. Do Until Rs.EOF ' Fill in a FlexGrid with the info, or do whatever you would like.
    9.    DoEvents
    10.     For i = 0 To Rs.Fields.Count - 1
    11.       MSFlexGrid1.TextMatrix(0, i) = Rs(i).Name
    12.       MSFlexGrid1.TextMatrix(lRow, i) = "" & Rs(i).Value
    13.     Next
    14.  lRow = lRow + 1
    15.  Rs.MoveNext
    16. Loop
    17. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    14
    Thanks for that reply

    But even when I change the return of the function as an Object, I still get the same error when I call the Showdata method in my form

    Surely there is a simple way to display the returned recordset to text boxes on a form (as in my case)???

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Yah there is a simple way... and i just showed it to you.. It doesnt get more simple than my example.



    Let me change it so its adding the items to textboxes instead of a FlexGrid.

    This is in your DLL
    VB Code:
    1. Function GetAllEmployees() As Object 'Set The returned Recordset as an Object
    2. Dim CN As New ADODB.Connection
    3. Dim RS As New ADODB.Recordset
    4. CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind"
    5. CN.CursorLocation = adUseClient
    6. RS.Open "SELECT * FROM Employees", CN, adOpenStatic, adLockOptimistic
    7. Set GetAllEmployees = RS 'This Retunrs the REcordset to the Function
    8. Set RS = Nothing
    9. Set CN = Nothing
    10. End Function





    This is in your Form..
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim Rs As New ADODB.Recordset
    3. Dim MyRS As New SQLServerDLL.Interface ' Set refrences to ADO and your DLL
    4. Set Rs = MyRS.GetAllEmployees ' Set you Recordset = The recor returned by your Function
    5.  
    6. Text1=Rs!LastName
    7. Text2=Rs!FirstName
    8. etc....
    9. End Sub
    Last edited by Arc; Jun 15th, 2002 at 01:29 PM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Problem returning Recordset

    Originally posted by mickysavage
    Hi

    I am trying to return a recordset from a function in a class back to my form in order to display the data.

    Code is as follows:

    In my Form -

    Dim bs as New Bookstore
    Public rsResult = New ADODB.Recordset

    Private Sub mnuBook_Click()

    Dim strIsbn As String

    strIsbn = InputBox("Enter the ISBN", "Search by ISBN")
    If strIsbn = "" Then
    MsgBox "No ISBN entered", vbOKOnly, "Search"
    Else
    Showdata(bs.findbyIsbn(strIsbn))
    End If

    End Sub

    Private Sub Showdata(rsResult As ADODB.Recordset)
    txtIsbn.Text = rsResult.Fields("ISBN")
    (...display into all my other text boxes....)

    End Sub

    and in my Bookstore Class:

    Public Function findbyIsbn(strIsbn As String) As ADODB.Recordset
    Dim rsResult As New ADODB.Recordset
    Dim strSQL As String

    strSQL = "SELECT Author.*, Books.* FROM Author INNER JOIN Books ON Author.[Author Code] = Books.[Author Code] WHERE (Books.ISBN)='" & strIsbn & "';"

    rsResult.Open strSQL, mcon1, adOpenDynamic, adLockOptimistic
    Set findbyIsbn = rsResult
    End Function


    The problem is I get a Type Mismatch error on the - Showdata(bs.findbyIsbn(strIsbn)) - line of code in my form

    Its probably something really simply, but I fairly new to VB and any advice would be appreciated

    Cheers
    First of al... get rid of the parenthesis:

    VB Code:
    1. Showdata bs.findbyIsbn(strIsbn)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    A tip... for everybody. Try avoidin using
    VB Code:
    1. Dim variable As New VariableType
    Use this instead:
    VB Code:
    1. Dim variable As VariableType
    2. Set variable = New VariableType
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Mc Brain
    A tip... for everybody. Try avoidin using
    VB Code:
    1. Dim variable As New VariableType
    Use this instead:
    VB Code:
    1. Dim variable As VariableType
    2. Set variable = New VariableType
    Declaring variables As a new instance of an object, forces the compiler to control in each operation if the object has been previously created. So... this is what your "compiled code" would look like (Everyone know that it won't look like that... but it will be coded as if this is the source of your program)
    Attached Images Attached Images  
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width