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]
 
Looping through the Controls collection is fairly straight forward as it is a collection, and items can be referenced by Index number, or by key, with the key being the name of the control. This allows you to loop through the control collection in the following manner, only looking at controls named Ctl1, Ctl2, ... Ctl140
Code:
For Idx = 1 to 140
   Set Ctl = Me.Controls("Ctl" & Trim(Idx))
   With Ctl 
       ...
   End With
Next Idx
which is all fine and good, but I don't think it helps with the fundamental issue behind this application. In seems to me that the key element in this is knowing if and when the focus has changed from one control to another, without having specific event handlers for each and every control. One approach that might work, but does have some downsides, is the following:

Create another textbox on the form and call it "txtActiveControl", and set its visible property to false.
In the Form_Load event, place the following code:
[/code]
Private Sub Form_Load()
Me.TimerInterval = 500
txtActiveControl.Tag = vbNullString
End Sub
Code:
Then construct the Timer event as follows:
[code]
Private Sub Form_Timer()
   If (Screen.ActiveControl.ControlType = acTextBox) Then
      txtActiveControl = Screen.ActiveControl.Name
      If (txtActiveControl <> txtActiveControl.Tag) Then
         MsgBox &quot;Textbox has changed to: &quot; & txtActiveControl
         txtActiveControl.Tag = txtActiveControl
      End If
   End If
End Sub
In place of the MsgBox, you will place your routine to extract the current textbox's name and do with it as you must. txtActiveControl will always contain the name of the textbox which last had, or currently has the focus. As it is, if you click on a button, or even a combo box, the value of txtActiveControl will not change. It will still contain the name of the last text box which had the focus. If you want to bring other control into the mix, you can adjust the
Code:
If (Screen.ActiveControl.ControlType = acTextBox) Then
to meet your needs.

The downsides: It is possible that you could skip over a control by clicking on one control and then another between Timer interval events. You will miss the intervening click. Secondly, become of the rapidity of the Timer interval, you will be chewing up lots of processer cycles. But in the meantime, until we can come up with something a little more robust and efficient, you might be able to use this to get the job done.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I had thought about using a timer but it seems pretty klooogy, especially on a screen with 140 controls.
We've already established that you can't form trap mouse clicks the way you can trap keyboard events using key preview, so there is no event to trap that kicks off the code he wants to run. That means the only event to trap is the focus movements on the controls. In an event driven model, that's where the code is going to have to go. The only alternative I can see is to write some type of mouse trap, if you will, in C, or find some API call that would allow you to do the same. Personnally, adding a function call to each a control focus event just isn't that big a deal, even if you have 140 controls.


 
I agree vbajock, it is a kludge, and I don't particularly like it, and I also agree that it doesn't take that much time to copy and paste 140 event routines that are the same.

But, suppose that the 140 controls are being dynamicaly created at run-time on an as needed basis? You can't add the event handlers at run-time. Would be easy with a control array to be sure.

I wonder how to derive a form level &quot;Form_FocusChanged (strFromControl as String, strToControl as String)&quot; event.

One approach that I'm looking into is using a Class WithEvents that &quot;controls&quot; the textbox, by attaching an instance of the class to the textbox. Then the code is only in one place, with the class can raising the appropriate event at the right time. This may also, if I can get it to work, provide a way to mimick a control array.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Brilliant, Brilliant!!!! [thumbsups] Okay so here is the plan so far:

In the got focus event, there is going to be code that calls the function GetControlName. GetControlName is going to use the ActiveControl.name(kudos to vbajock) to get the name of the control. GetControlName will pass that value to a public variable that the sub procedure can use. Paste that into all 140 sub procedures (kudos to Cajun), and I think that may do it. I think this may very well work. It kinda mimics a control array, by just one section of code being in a single function. I don't mind copying and pasting as long as I dont have to go through and change any numbers.

If I take a peek in your Windows, to fix a problem, does that make me a &quot;Peeping Tom&quot;? Hmmmmmmmmmmv [pc1][shocked]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top