Solved How do I convert these lines into a function?

561414

Active member
Local time
Yesterday, 23:27
Joined
May 28, 2021
Messages
279
Hello everyone.

I've been requerying a bunch of ComboBoxes in my forms using the after update event of the ComboBox they depend on, it looks like this:

Code:
Private Sub cboMain_AfterUpdate()

Me.Combo1.Requery
If Trim(Me.Combo1& "") = "" Then
    Me.Combo1= Me.Combo1.ItemData(0)
End If

Me.ComboTwo.Requery
If Trim(Me.ComboTwo& "") = "" Then
    Me.ComboTwo= Me.ComboTwo.ItemData(0)
End If

End Sub

I have this pattern everywhere, is there a way to wrap these lines like CustomRequery(combo)? or something like that?

I was trying with
Code:
Public function CustomRequery(combo as Access.ComboBox) as ?
'the code
End function

First of all, I don't know what the output type should be. Second, I don't know how to refer to the form "me".

Also, if none of this makes sense, I'd like to know.

Thank you all.
 
you can do that, add this code in a Module:
Code:
Public Function fnRequeryCombo(ByRef Combo As Access.ComboBox)
Combo.Requery
If Combo.ListIndex < 0 Then
    Combo = Combo.ItemData(0)
End If
End Function

on your code:
Code:
Private Sub cboMain_AfterUpdate()
Call fnRequeryCombo(Me.Combo1)
Call fnRequeryCombo(Me.ComboTwo)
End Sub
 
arnelgp, that works so good, thank you so much
 

Users who are viewing this thread

Back
Top Bottom