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

creating a function that returns a control's name and.... 2

Status
Not open for further replies.

docmeizie

Programmer
Aug 5, 2003
326
US
What I want to do is create a public function that when you click on a textbox control (the controls are named 1 through 140 but when you create a sub procedure the controls are named Ctl1 through Ctl140) the function trims off the "Ctl" of the control just clicked and returns just the numerical part of the control (i.e., 1, 2, ..., 140). I will need number stored in a variable that will be used locally in a private sub procedure. Does anyone have any coding solutions to do this.

If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
As long as they ALL FIT THE FORMAT, you can create a function called

Function DoIt(strCtlName as String)
msgbox Me.Controls(strCtlName).Name
'etc, whatever else you need to do
End Sub

Then, for each event for EACH control, you can set the OnClick event to something like

=DoIt("Ctl1")


If there are too many to manually update, you can try messing with opening the form as a document, then programmatically changing all the controls on the open "document". But that I leave to you.

--
Find common answers using Google Groups:

Corrupt MDBs FAQ
 
Do you know of a single event that can be used to detect when any control has been clicked on, or when any control has received the focus?

Otherwise, you'd have to place the function call in the Click event of each control, or better yet the OnEnter event since you can tab to a control without clicking it, and there would be no need to call a function because once you're in an event for a specific control, you can extract the numeric portion of the name with a simple assignment statement.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Huh, there's something new.

I suppose this is one of those reasons I should get around to finishing that Access book ...
 
I guess the question I have is why anyone would want to do it.
 
Good point -- to get back to the original post -- your problems may be solved by using a continuous form instead of a cascading lump of hundreds of fixed controls.

But there may be a good reason.
 
If the object is just to discover what control was just accessed:

Dim ctlCurrentControl As Control

Set ctlCurrentControl = Screen.ActiveControl
msgbox ctlCurrentControl.Name

 
Answer to the question about having to put it in all the controls, put the above code in the key down and mousedown events, that ought to cover it
 
Using the KeyDown will work, you just have to be careful to remember that the Tab key and Return keys will give you the name of the control that you are leaving, not the ones that you're entering, but you can work your way around that.

But I'm not too sure that MouseDown can be trapped by the form, or even the Section (Header, Detail) because those events, I'm believe, go to the individual controls.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
There is a mousedown event for the form object. Check the help file, Form, Events

Keydown would require some work, because it's also going to fire whenever a letter is typed. Like I said, you'd need a pretty good reason to do it.


 
Yes I know there is a mousedown event for the form, but the form only gets the mouse down events when certain areas of the form are clicked. If you click in a control, the control gets the event. If you click in the detail section, the detail section gets the event.

Do you know of a way to hook the mouse events so that they all go to the form?


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Your right, I just tested it. I figured it would fire on any click. There is no keypress type property to redirect the mouse clicks to form level either. I guess he is SOL in finding a single function to do it all.
 
Sorry for not getting back to you yesterday. The reason why I did this that way is because I have one form based on a crosstab query that creates, at most, 140 controls on the form. This is for user ease so they may see which slips are currently open without having to scroll through 140 records and memorizing which slips are empty. So basically for each record, the information will be the location in the marina(Large Boat Basin, Small Boat Basin, and Mooring) and the slips that belong in each location. I got the functionality of the form to work to show all 140 slips in one form (alot of resizing and moving around). The owners of the slips can also be identified by name and a color code. That part was simple(after racking my brain for 3 days on how to show it the right way). Now with what little brain juice I have reserved for one week, I need to be able to code a procedure that will get the name of the control that I just clicked so that I may use this code in all 140 controls with out having to hard code all 140 procedures in by control name. The idea candidate code it seems would be a public procedure or function that gets the name of the control (the controls are named 1 - 140, oops on my part). This would have all been way easy if VBA had the ability to create control arrays, then I could just select all the controls and code once. One thing that I have notice though, is in the code section the controls 1 - 140 appear as Ctl1 - Ctl140.

If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
Darn, I was hoping that you had some trick up your sleeve that we could all learn from.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I'm still thinking. I got some ideas so far but nothing solid. I do know for a fact that you can iterate through all the controls on a form. Look up Control in Help. I doth declareth this day of October 31, 2003, that thou, docmeizie shall find a way to do this. I'll keep posting what I come up with. I solved my random number generator using the index number of a list of teams so by goolie gee whiz i can do this too. LOL

If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
Ahhh see I have learned that this code:

Sub SetTextBoxProperties(frm As Form)
Dim ctl As Control

' Enumerate Controls collection.
For Each ctl In frm.Controls
' Check to see if control is text box.
If ctl.ControlType = acTextBox Then
' Set control properties.
With ctl
.SetFocus
.Enabled = True
.Height = 400
.SpecialEffect = 0
End With
End If
Next ctl
End Sub

Actually checks what type of control it is and then changes the properties of the control through code. Now with this being true, it would seem viable to check to see if a textbox has the focus and then get the name of the textbox into a variable so that you open a certain form and have information about slip (location + number) filled in the form already.


If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
U you can also refer to controls by the index in this format:

Me.Controls(IndexNumber)

If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
Maybe I'm missing somethinf here. Why don't you just create a single function to handle the job you need done and then put the function call in the event you are trying to trap?

For example, on click you can pass the control to a function:

Function SomeFunction(somectl as Control)

'do stuff


End function
 
You know vba that is exactly what I am trying to do. The problem lies with in the intialization phase in my mind. Right now I am trying to think of code that would return the name of the control that has the focus. Doth such a refined gentleman as thineself have sucheth an answer?

If I take a peek in your Windows, to fix a problem, does that make me a "Peeping Tom"? Hmmmmmmmmmmv [pc1][shocked]
 
ActiveControl returns the name of the control that currently has the focus.

The only way to solve your initialization problem is to put a call to a generic function in the gotfocus event of each control. In the event driven model, you have to have an event to initiate things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top