BobNoobGuy
BobNoobGuy

Reputation: 1645

VB6 module to pass recordset to form

lets call it modUser. In this modUser I have a ADODB record set Now from this modUser, I would like to open a form. Lets call it frmUser2. when this frmUser2 initialize. I would like to use the recordset that I already have from modUser. How do I pass this recordset from modUser to frmUser2?

I tried creating a public sub under frmUser2. But I get an error that says "Run time error 13 type mismatch"

here is a snippet

sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount > 1 Then
    frmUser2.PopulateList(rs)
End if

in frmUser2 I have the public function ( i tried sub too)

Public Function PopulateList(rs As ADODB.Recordset)
    For Count = 0 To rs.RecordCount - 1
        LstModels.AddItem rs(0)
        rs.MoveNext
    Next
    rs.close
End Function

I tried show , and I can get the form to appear, but I have no way to pass the record set.

frmUser2.Show

Please help. Thank you

Upvotes: 1

Views: 566

Answers (1)

jac
jac

Reputation: 9726

I'm not a fan of how you're trying to do this, but working with what you want to do, first create a public Recordset property in your form and assign the recordset to it from your module before showing the form.

Module code:

Dim objForm As frmUser2

sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount.EOF = False Then
    Set objForm = New frmUser2
    frmUser2.Recordset = rs
    frmUser2.Show
End if

Form Code:

Private Sub Form_Load()

    If Not Recordset Is Nothing Then
        PopulateList
    End If
End Sub

Public Function PopulateList()

    Recordset.MoveFirst 'defensive, make sure we're on the first record
    LstModels.Clear
    Do While Recordset.EOF = False
        LstModels.AddItem Recordset(0)
        Recordset.MoveNext
    Next
    Recordset.Close

End Function

I think it would be preferable for the module to have a public method that returns an employee recordset. Your form would call that method when it needs the data. Set rsEmployees = modUser.GetEmployees()

Upvotes: 1

Related Questions