By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,705 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,705 IT Pros & Developers. It's quick & easy.

Auto-Generate Control Events on Form Load

Expert 100+
P: 378
Hey guys, my turn to ask a question =D

Ok here's my situation.. I have a super simple form, which is bound to a table with a single record. All the controls (mostly text boxes, some true/false checkboxes and combos) are bound straight to each field.

Now, in the table itself, each field has it's Description property filled out, instructing the user as to what data belongs in the field. I can pull this data on the form by using each control's .StatusBarText property. What I'd like to do is have a label in the form header, that updates OnEnter in each control, showing the user the full description of the currently selected control. (It does display in the statusbar on the bottom, but it gets cut off usually.)

I -dont- want to plug in the code in each control's Enter/Leave/GotFocus, etc events. I want to auto-populate these on Form Load, or, maybe catch some other type of event when I switch focus between controls and then pass this to a generic function.

If this can be done on form load, I imagine I'd start with something like the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub SetControlEvents()
  2.     Dim i As Integer
  4.     For i = 0 To Controls.count - 1
  5.             If Controls(i).ControlType = acTextBox Or Controls(i).ControlType = acComboBox Then
  6.                  'set event for control
  7.             End If
  8.     Next
  10. End Sub
So, can dynamically written events be done like this? I understand I can use a single function to pass the name of the control, etc.. But this is being set up so that an end user can go into the table, enter new fields, type a description, and simply drop the new control onto the form and it'll work without having to enter in any events for it.
Feb 7 '09 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 489
Since it's a simple form as you say, why not just use the on timer event and check to see which control has the focus. If the focus has changed then change the label text. Something like this

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public strPreviousField As String
  4. Private Sub Form_Timer()
  5. If strPreviousField = Me.ActiveControl.Name Then
  6.     Exit Sub
  7. End If
  8.     strPreviousField = Me.ActiveControl.Name
  9.     Me.Label1.Caption = Me.ActiveControl.StatusBarText
  10. End Sub
Feb 7 '09 #2

Expert 2.5K+
P: 2,653
Hi, Megalog.

I had a same problem when was coding an interface feature based on mouse move over a certain control. The problem was the following - since form was overcrowded with controls, handling Detail_MouseMove event was not reliable to detect when mouse pointer leaves control. So I've written a class (similar to that in this thread) that "merges" events from all form components.

See the attached demo.


P.S. Hmm. Unfortunately it seems impossible to attach anything so far. If you are interested to see the code, then PM me your mail.
Feb 7 '09 #3

Expert Mod 15k+
P: 31,411
How about using the form's BeforeScreenTip event procedure.

If I understand your requirements correctly, you can check within the procedure that the ToolTip is one you're interested in (SourceObject Object) and, if it is, simply set the .Caption property of the label control to the value of the ScreenTipText object.
Feb 8 '09 #4

Expert 100+
P: 378
Thanks guys
I ended up using a timer, which I normally avoid, since I couldnt find a form event that would work.

Neo, I couldnt get the BeforeScreenTip event to trigger.. I'm not sure what I was doing wrong, I'll try to revisit that event when I get more time. Never knew it existed before. =)
Feb 9 '09 #5

Expert Mod 15k+
P: 31,411
Did you have a ControlTip Text value set for the control you were testing on?
Feb 11 '09 #6

Expert Mod 15k+
P: 31,411
My bad.

I did some testing and some further research and found that it's only valid for PivotTables & PivotCharts :(
Feb 11 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.