Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same VBA code lines for different textbox in a form

Status
Not open for further replies.

bydiaz

Programmer
Sep 6, 2004
1
US
I have several textbox in my form (Cab01, Cab02, Cab03..Cab99) and i'd like to execute the same code for all

Private Sub Cab01_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then PX = X: PY = Y

How could i pass and return the values X,Y without writing 99 times the code?

Something like:
When i click left mouse button on a textbox it will execute a function that will find out what control has the focus so i can use a code like

for x=1 to 99
MsgBox "TextBox: " & Cab(x).Name
next x
 
Not positive, I understand the question, but, to loop through each control, on a specific form, is relatively simple.

Dim ctl As Control

For Each ctl in Me.Controls 'this alone, will give ALL controls, ie; labels, listboxes, cmmdbuttons etc. so...
If ctl.ControlType = acTextBox And Left(ctl.Name,3) = "Cab" Then

...execute code...

End If
Next

...what is this... Cab(x).Name ?
what are you trying to extract, the value for that control, or the name of the control, with current(previous) focus?

The minute you cick on a textbox, THAT textbox now, has the focus. Do you want the control, with the focus, prior to left clicking a textbox. Or, you simply want the name, of the textbox, that you click on?

From any event, from any control use either...
MsgBox Screen.ActiveControl.Name
or...
MsgBox Screen.PreviousControl.Name

Not sure if I have the gist yet?
Public procedure, is very easy also...

 
Can you be more specific on what are you trying to do with this code? I'm not sure I'm understanding.

Assuming you want the same code in all the mouseDown events of each textbox you have, you can use a separate SUB routine and call it from each of the mouseDown events you want it to run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top