1 event procedure for multiple controls?

G37Sam

Registered User.
Local time
Today, 23:46
Joined
Apr 23, 2008
Messages
454
Hello,

I have a form with like 30 text boxes, what im trying to do is (for simplicity's sake) is to run msgbox "hi" on lost focus for all 30 text boxes without having to them manually 1 by 1? can that be done?

regards,
Samer
 
Not totally but sort of. You can create a function in either the form module or standard module (if in standard module it can be called from any form) and to make it short you could use:
Code:
Public Function gm()
   MsgBox "Hi"
End Function

and then in the events of the text boxes you can just type or copy and paste:

gm
 
Maybe
=gm()

Congratulations on the 10,000 posts Bob. :)

Regards,
Chris.
 
thx for the suggestion mate, but i was wondering if theres one piece of code you'd write somewhere and it'll all work out?
 
thx for the suggestion mate, but i was wondering if theres one piece of code you'd write somewhere and it'll all work out?

Nope you still gotta tell Access to use the event. So, I worked it down to two letters, but you still gotta go there.
 
' For four text boxes named Text1, Text3, Text5 and Text7
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim intIndex As Integer
    
    For intIndex = 1 To 7 Step 2
        Me("Text" & CStr(intIndex)).OnLostFocus = "=gm()"
    Next intIndex

End Sub
 
' For four text boxes named Text1, Text3, Text5 and Text7
Code:
Private Sub Form_Open(Cancel As Integer)
    Dim intIndex As Integer
    
    For intIndex = 1 To 7 Step 2
        Me("Text" & CStr(intIndex)).OnLostFocus = "=gm()"
    Next intIndex

End Sub

Ah, interesting.
 
Sure is.

The event handlers we see in the properties are simply strings.

We can write anything we want in there…and sometimes it works. :D

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom