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.
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