Solved Run Same Code on OnEnter Event of Multiple Textboxes (1 Viewer)

Pac-Man

Active member
Local time
Today, 20:52
Joined
Apr 14, 2020
Messages
408
Hello,

I'm trying to trap OnEnter event of multiple textboxes on a form and run the same code. The same will be applied to number of other forms. So I tried to write my own class to achieve the purpose and tried few other methods but none was successful.

While searching across the internet, I found out article by @MajP about Simulating a Control Array and tried it too. It also didn't work for my case. I have been trying to do this for many days but failed. Can you please help me where I'm doing wrong in implementation of the technique. Attached is a DB in which I tried to implement it.

Best Regards,
Abdullah
 

Attachments

  • CodeOnMultipleControls.zip
    302.2 KB · Views: 325

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:52
Joined
Feb 28, 2001
Messages
26,999
The usual way to do what you want is to first build a subroutine that takes the control object as a formal argument, and then in a routine in a general module, do what checking you need to do - remembering that EVERYTHING that would reference the control has to now qualify the reference with one of the routine's arguments.

Code:
Public Sub DoCommonThing( ctl As Access.Control, other args...)
...
    If ctl.Visible Then....
...
    ctl.BorderColor = vbBlack
....
End Sub

...

Private Sub MyControl_Enter()

    DoCommonThing MyControl, ....other args

End Sub
 

Ranman256

Well-known member
Local time
Today, 11:52
Joined
Apr 9, 2015
Messages
4,339
turn on the form KEYPREVIEW = yes.
then run events thru the form and you wont need to code EVERY textbox.

 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:52
Joined
May 21, 2018
Messages
8,463
The common way to do this is a single function.
It has to be a function and not a Sub. Then you simply select all the controls, and update the OnEnter property.
Instead of [Event Procedure] you put the name of the function as follows
=SomeFunction()

Now that function will trap every on enter event.
See discussion here

When I posted that "pseudo control array", it was really to demonstrate trapping events in a custom class and building a custom collection, but that is not how I would do it. It is very simple to do with a single Function.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:52
Joined
May 21, 2018
Messages
8,463
In this thread that I referenced
If you download the database there is a form that shows how a single function can trap multiple events, or see demo.
 

Attachments

  • MajP_CodeOnMultipleControls.accdb
    620 KB · Views: 290
Last edited:

Pac-Man

Active member
Local time
Today, 20:52
Joined
Apr 14, 2020
Messages
408
The usual way to do what you want is to first build a subroutine that takes the control object as a formal argument, and then in a routine in a general module, do what checking you need to do - remembering that EVERYTHING that would reference the control has to now qualify the reference with one of the routine's arguments.
Thanks for reply @The_Doc_Man. I couldn't, due to my little knowledge of programming, understand how this will handle/run same code or function on multiple controls on the form.
turn on the form KEYPREVIEW = yes.
then run events thru the form and you wont need to code EVERY textbox.

Thanks for reply @Ranman256. Right now I am away from PC, I will check this method. I don't have to apply the specific function on all the textboxes on the form. The code is to run on about 80% of the textboxes which have certain tag. Purpose of adopting class module was to apply the same code for other textboxes with same tags on other forms (when those are opened/loaded).

The common way to do this is a single function.
It has to be a function and not a Sub. Then you simply select all the controls, and update the OnEnter property.
Instead of [Event Procedure] you put the name of the function as follows
=SomeFunction()
Thanks for suggestion @MajP, it never occurred to me to do like this. I will try to do this tommorow as I'm currently away from PC. I tried to use this method of applying coffee for one textbox but my function input variables have one private const which couldn't be added through this method. My function was something like (when I call it in vba) call MyFunction ( PvtConst, True). When I wrote it in OnEnter property, it was converted to =MyFunction ([PvtConst], True).

TO ALL: one other reason I adopted for class module was this way I might learn class modules which seem to me like another world and also to prevent putting for all controls one by one in all forms.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:52
Joined
May 7, 2009
Messages
19,169
if you are just changing the Background of the textbox, you can do a simple Conditional format.
an overkill class that only changes the background color of the textbox "on Focus"
 

Attachments

  • FormClass.accdb
    416 KB · Views: 318

Pac-Man

Active member
Local time
Today, 20:52
Joined
Apr 14, 2020
Messages
408
if you are just changing the Background of the textbox, you can do a simple Conditional format.
an overkill class that only changes the background color of the textbox "on Focus"
Thanks for reply @arnelgp. I want to switch keyboard layout while entering specific textboxes. As per @MajP suggestion, I used =function () in OnEnter Event property.

Thanks again for everyone who put their input for helping. I'm obliged. My issue is solved now.

Best Regards
Abdullah
 

Users who are viewing this thread

Top Bottom