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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Click Which Control 2

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU
I have a form with a large number of text boxes on it
approx 200...

Trying to avoid repeating the on_click code 200 times eg..

Private Sub Text_1_1_Click()
Private Sub Text_1_2_Click()
etc............

I have tried the Form....on_click to detect a click and then work out which control has focus, but the event click does not seem to work..

What i tried was ......

Private Sub Form_Click()
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name

MsgBox (strControlName)

When i click on a text box, the above does not trigger...

Would appreciate any help with this idea....

Hope i have explained it clearly !!!!

Regards Kennedymr2



 
The is a property for the form called, as far as I recall, Key Preview, which allows the form to receive key events first.
 
Thanks Remou ,

Really appreciate the help...

I have altered the form key Preview to Yes...

If i enter a character in the text box, this works fine...
BUT... If i just click in the text box, this event does not trigger.
Have tried activating all the events i can see... but nothing seems to get a click to trigger the event...

Appreciate any ideas on how to get a click on the text box to work...

Regards Kennedymr2




 
How are ya kennedymr2 . . .

Just setup a common routine and make a simple call from each control. This can be done with a [blue]Class Module[/blue], but I see at least twice the number code lines involved.

So . . . in a module in the modules window copy/paste the following:
Code:
[blue]Public Sub comClick()
   Dim curControl As Control, ctlName As String
   
   Set curControl = Screen.ActiveControl
   ctlName = curControl.Name
   
   MsgBox (strControlName)

End Sub[/blue]
Then in each of the [blue]On Click[/blue] events of interest, copy/paste the following line:
Code:
[blue]   Call comClick[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 

Thanks TheAceMan1 for your reply...

What i am trying to do is have "no onclick" in any of the textboxes... ie i am trying to detect which textbox has been clicked and then use the routine below to determine which textbox was clicked.ie use following on form.keypressed

Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name

MsgBox (strControlName)

Remou sujjested turning on the form.keypreview... this worked fine....but only if eg a character is entered or space bar hit....it does not seem to get triggered by a click..

???Any ideas ????


Regards Kennedymr2


 
Would you accept a line against the click event? It is possible to set the On Click property to the name of a function or macro. So:

Code:
OnClick: =DoThis()

Function DoThis()
   MsgBox "You clicked " & Screen.ActiveControl.Name 
End Function
 
... and it is simple enough to update the property for every control via code.
 
Remou and Aceman1.....

Did not get the thrust of what you were sujjesting in the 1st instance.... sorry..!!!!

Thanks for the tip..... i did not think of just puting a function on each textbox click... i have wrtten a small program to loop through the properties and put this code in the click...it certainly avoids having all the vba code for each text box... thanks... will proceed with this idea...

Regards Kennedymr2
 
Have you tried to select all the relevant controls in design view and then set the OnClick property ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PVH....

Yes... a lot easier than writing code to alter it...
Many thanks...


Regards Kennedymr2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top