Auto-Generate Control Events on Form Load

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    Auto-Generate Control Events on Form Load

    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:
    Code:
    Private Sub SetControlEvents()
        Dim i As Integer
    
        For i = 0 To Controls.count - 1
                If Controls(i).ControlType = acTextBox Or Controls(i).ControlType = acComboBox Then
                     'set event for control
                End If
        Next
    
    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.
  • DonRayner
    Recognized Expert Contributor
    • Sep 2008
    • 489

    #2
    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

    Code:
    Option Compare Database
    Public strPreviousField As String
    
    Private Sub Form_Timer()
    If strPreviousField = Me.ActiveControl.Name Then
        Exit Sub
    End If
        strPreviousField = Me.ActiveControl.Name
        Me.Label1.Caption = Me.ActiveControl.StatusBarText
    End Sub

    Comment

    • FishVal
      Recognized Expert Specialist
      • Jun 2007
      • 2656

      #3
      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_MouseMov e 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.

      Regards,
      Fish.

      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.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        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.

        Comment

        • Megalog
          Recognized Expert Contributor
          • Sep 2007
          • 378

          #5
          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. =)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32653

            #6
            Did you have a ControlTip Text value set for the control you were testing on?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32653

              #7
              My bad.

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

              Comment

              Working...