Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing ComboBox Object to a Function

Status
Not open for further replies.

AWilliams

Programmer
Mar 20, 2002
10
CA
The code below (Code Example 1) is used to populate a combo box with institution names from an institution table. This works fine.

What I was trying to do was throw this code in a function in the module and pass this function the query statement, combo box name, institution name and institution primary key value. Figured this would be more efficient coding style. The problem that I'm having is that unable to pass the combo box object name into the function from the calling form. I figured you can pass an object as a parameter to a function. Any ideas if this can be done. See Code Example 2 which is the code used in the calling form and in the main module.

Code:
' Code Example 1 - all code contained in the same form, this work fine

Private Sub Form_Load()

 Call PopulateInstitutionCombo
 
End Sub


Private Sub PopulateInstitutionCombo()

 On Error GoTo PopulateInstitution_Error

        Set rsPopulate_Combo = New ADODB.Recordset
    
        cboInstitutionList.Clear
    
        strSQL = "SELECT * FROM tbInstitution ORDER BY IT_Name"
        rsPopulate_Combo.Open strSQL, cnCSMS
      
        Select Case rsPopulate_Combo.BOF
        
            Case False
                
                rsPopulate_Combo.MoveFirst
    
                Do Until rsPopulate_Combo.EOF
                    cboDistrict.AddItem (rsPopulate_Combo!IT_Name)
                    cboDistrict.ItemData(cboDistrict.NewIndex) = rsPopulate_Combo!IT_Institution_PK
                    rsPopulate_Combo.MoveNext
                Loop
        
        End Select
              
        rsPopulate_Combo.Close
            
Exit_PopulateInstitution:
    Exit Sub
    
PopulateInstitution_Error:

    Error_Response Err.Number, Err.Description
    
    Resume Exit_PopulateInstitution

End Sub

Code:
'Code Example 2 

'Calling form

Private Sub Form_Load()

  Call LoadComboBox  

End Sub


Private Sub LoadComboBox()

 'strSQL = "SELECT * FROM tbDistOff ORDER BY DO_District_Name"
 Call PopulateCombo(strSQL, cboInstitution, IT_Name, IT_Institution_PK)

End Sub

'Module

Public Function PopulateCombo(sqlStmt As String, cboName As Object, fieldName As String, pkField As String)

On Error GoTo PopulateCombo_Error

        Set rsPopulate_Combo = New ADODB.Recordset
    
        cboName.Clear
    
        rsPopulate_Combo.Open strSQL, cnCSMS
      
        Select Case rsPopulate_Combo.BOF
        
            Case False
                
                rsPopulate_Combo.MoveFirst
    
                Do Until rsPopulate_Combo.EOF
                    cboDistrict.AddItem (rsPopulate_Combo!fieldName)
                    cboDistrict.ItemData(cboDistrict.NewIndex) = rsPopulate_Combo!pkField                    
                    rsPopulate_Combo.MoveNext
                Loop
        
        End Select
              
        rsPopulate_Combo.Close
            
Exit_PopulateCombo:
    Exit Sub
    
PopulateCombo_Error:

    Error_Response Err.Number, Err.Description
    
    Resume Exit_PopulateCombo

End Sub
 

here is an example on how to pass a control (in this case combo1) as an arguement

'in a form put
[tt]
Option Explicit

Private Sub Form_Load()
LoadCombo Combo1
End Sub

'In mod put
Public Sub LoadCombo(C As ComboBox)
C.AddItem "A"
C.AddItem "B"
C.AddItem "C"
End Sub

[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top