Solved How do I convert these lines into a function? (1 Viewer)

561414

Active member
Local time
Today, 02:13
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.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:13
Joined
May 7, 2009
Messages
19,246
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
 

561414

Active member
Local time
Today, 02:13
Joined
May 28, 2021
Messages
279
arnelgp, that works so good, thank you so much
 

Users who are viewing this thread

Top Bottom