1 event procedure for multiple controls? (1 Viewer)

G37Sam

Registered User.
Local time
Today, 20:20
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
 

boblarson

Smeghead
Local time
Today, 09:20
Joined
Jan 12, 2001
Messages
32,059
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
 

ChrisO

Registered User.
Local time
Tomorrow, 02:20
Joined
Apr 30, 2003
Messages
3,202
Maybe
=gm()

Congratulations on the 10,000 posts Bob. :)

Regards,
Chris.
 

G37Sam

Registered User.
Local time
Today, 20:20
Joined
Apr 23, 2008
Messages
454
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?
 

boblarson

Smeghead
Local time
Today, 09:20
Joined
Jan 12, 2001
Messages
32,059
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.
 

ChrisO

Registered User.
Local time
Tomorrow, 02:20
Joined
Apr 30, 2003
Messages
3,202
' 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
 

boblarson

Smeghead
Local time
Today, 09:20
Joined
Jan 12, 2001
Messages
32,059
' 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.
 

ChrisO

Registered User.
Local time
Tomorrow, 02:20
Joined
Apr 30, 2003
Messages
3,202
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

Top Bottom