Results 1 to 8 of 8

Thread: Passing forms to classes...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88

    Passing forms to classes...

    Hello, I'm just getting into Object based Programming (with VB6, which I have to use at work for now - .NET later, hopefully soon).

    Here's the situation: I'm trying to encapsulate code for populating combo boxes throughout in a multiform project. I have made a class called, surprisingly, "populateCombo". Here is the code I have so far in that class module (just one method):

    Public Sub populateCombo(ByVal frmWhich As Integer, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
    Dim objRS As New ADODB.Recordset
    Dim strSQL As String
    strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
    objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly

    If frmWhich = 0 Then
    Do While Not objRS.EOF
    frm1.Combo(cboNum).AddItem objRS.Fields(fieldName)
    objRS.MoveNext
    Loop

    Set objRS = Nothing

    ElseIf frmWhich = 1 Then
    Do While Not objRS.EOF
    frm2.Combo1(0).AddItem objRS.Fields(fieldName)
    objRS.MoveNext
    Loop

    Set objRS = Nothing

    End If
    End Sub

    It's pretty self-explanatory, but the if-then structures sort of defeat the purpose of reusable code. Is there a way to pass in form names when I call populateCombo rather than having the frmWhich and repeating the code for each form? The current configuration works fine, but I'd like to make it slicker, for example, when I call the procedure I'd like to do something like this: Call populateCombo("frm1", 1, "<table name>", "<field name>"). Is this even possible or am I stuck with the above situation?

    Thank you! This forum has been a big help to me!

    Ed Womack

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    VB Code:
    1. Public Sub populateCombo(ByRef frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
    2. Dim objRS As New ADODB.Recordset
    3. Dim strSQL As String
    4.     strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
    5.     objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
    6.  
    7.     Do While Not objRS.EOF
    8.     frmWhich.Combo1(0).AddItem objRS.Fields(fieldName)
    9.     objRS.MoveNext
    10.     Loop
    11.     Set objRS = Nothing
    12.  
    13. End Sub
    Just pass "Me" as a parameter.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88
    I put in the code as in above (this is from my screen):

    Public Sub populateCombo(ByVal frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
    Dim objRS As New ADODB.Recordset
    Dim strSQL As String
    strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
    objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly

    Do While Not objRS.EOF
    frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
    objRS.MoveNext
    Loop

    Set objRS = Nothing

    End Sub



    I also changed the class invocation to:

    Dim cboTest as New populateCombo
    Call cboTest.populateCombo(Me, 0, "<table name>", "<field name>")

    When I run the program I get a "Run-Time error '438': Object does not support this property or method." The debug button takes me back to this line:

    frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)

    When I try to type in the line from scratch above, intellisense will not let me enter "Combo1." It gives me a list of form properties, but nothing about the forms controls. There is a "Controls" property on the list, however.

    I have a feeling I'm very close...

    Thank you,

    Ed Womack

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Is the name of the combo box combo1? Unless you need the form for something else then it'd be better to pass the combo instead of the whole form.

    VB Code:
    1. Public Sub populateCombo(ByVal cbo As ComboBox, ByVal tblName As String, fieldName As String)
    2. Dim objRS As New ADODB.Recordset
    3. Dim strSQL As String
    4. strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
    5. objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
    6.  
    7. Do While Not objRS.EOF
    8. cboAddItem objRS.Fields(fieldName)
    9. objRS.MoveNext
    10. Loop
    11.  
    12. Set objRS = Nothing
    13.  
    14. End Sub
    15.  
    16. 'syntax
    17. Call cboTest.populateCombo(Me.Combo1(1), "<table name>", "<field name>")

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88
    Wow! That did it! Now I can pass the entire Combo box to the class function from anywhere in my program. Incredibly cool.

    One thing; on the code in the last message the line:

    cboAddItem objRS.Fields(fieldName)

    should read:

    cbo.AddItem objRS.Fields(fieldName)

    I'm sure it's just a typo.

    Thank you for the help! I appreciate it!!

    Ed Womack

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Originally posted by ewomack
    I put in the code as in above (this is from my screen):

    Public Sub populateCombo(ByVal frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)****
    Dim objRS As New ADODB.Recordset
    Dim strSQL As String
    strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
    objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly

    Do While Not objRS.EOF
    frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
    objRS.MoveNext
    Loop

    Set objRS = Nothing

    End Sub



    I also changed the class invocation to:

    Dim cboTest as New populateCombo
    Call cboTest.populateCombo(Me, 0, "<table name>", "<field name>")

    When I run the program I get a "Run-Time error '438': Object does not support this property or method." The debug button takes me back to this line:

    frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)

    When I try to type in the line from scratch above, intellisense will not let me enter "Combo1." It gives me a list of form properties, but nothing about the forms controls. There is a "Controls" property on the list, however.

    I have a feeling I'm very close...

    Thank you,

    Ed Womack
    You are using ByVal instead of ByRef in the form parameter.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88
    Whoop! That one slipped by - I don't want to pass copies of the combo box - thanks, I 'll make that change!

    Ed Womack

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Passing a control ByVal does not pass a copy of the control. If I recall correctly it simply passes a copy of the pointer to the control.

    You should also be aware that in a Public class you cannot pass controls in this manner. You must declare them as Object and typically you would include code to make sure the correct kind of control was passed

    Code:
    Public Sub DoSomething(ComboBoxControl as Object)
        If Typeof ComboBoxControl is VB.ComboBox Then
             ... processing
        Else
             Err.Raise ...
        End if
    End Sub
    or you could set a local variable = to the combobox parameter (you will get a type mismatch error if someone tries to pass a textbox)

    Code:
    Public Sub DoSomething(ComboBoxControl as Object)
        Dim objCombo as VB.ComboBox
        Set objCombo = ComboBoxControl
       
    End Sub
    Have fun.
    Bruce

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