• global form events (access 97)

    Author
    Topic
    #368363

    hi all,
    i have a particlar form with many text box fields. on gotfocus of each field, the current field back color changes to white, and on lostfocus, the current field back color changes to gray. is there an easier way to set this to work than to set each gotfocus/lostfocus event to trigger individually? in other words, can you set a group of controls, and whenever any one of them gets focus, change the corresponding backcolor to white, etc.? (perhaps dim ‘variable’ as control then whatever control has the focus, variable.backcolor = white) any help would be appreciated.

    thanks in advance

    Viewing 0 reply threads
    Author
    Replies
    • #577053

      The way to do this is using a class to handle the control events. You would build a TextBox class and in the Form_Open of your form, you would bind each textbox on the form to the TextBox class.

      Here’s a sample of a TextBox class. You would create a new class module and save it as clsTextBox. Then drop this code into the class module:

      Option Compare Database
      Option Explicit
        ' Created by Charlotte Foust
        ' last modified 11/19/99
       
        Private WithEvents mtxt As Access.TextBox
        Private mstrTxtNm As String
        Private mstrTxtParent As String
        Private mlngBackColor As Long
       
      Public Sub Init(ByRef rtxt As Access.TextBox)
        On Error GoTo Init_err
        mstrTxtNm = rtxt.name
        mstrTxtParent = rtxt.Parent.name
        Set mtxt = rtxt
        mtxt.OnGotFocus = "[Event Procedure]"
        mtxt.OnLostFocus = "[Event Procedure]"
        mlngBackColor = mtxt.BackColor
      Init_exit:
        Exit Sub
      Init_err:
        MsgBox "An error has occurred in the system." & vbCrLf _
              & "Unable to process your instructions. " _
              & "(" & mstrTxtParent & "." & mstrTxtNm & ")"
        Resume Init_exit
      End Sub
       
      Private Sub mtxt_GotFocus()
        'make it pale yellow
        mtxt.BackColor = 8454143
      End Sub
       
      Private Sub mtxt_LostFocus()
        'return to original color
        mtxt.BackColor = mlngBackColor
      End Sub

      This particular class example sets the backcolor to a pale yellow when the control gets the focus and then sets it back to its original color on the LostFocus event. Please note that this won’t work properly on a continuous form, only a single form view.

      You’ll need to declare a module level instance of the class for each of the textboxes like this:

          Dim mobjTxt1 As clsTextBox
          Dim mobjTxt2 As clsTextBox
          ... etc,

      In your form, you would bind each control like this:

      Set mobjTxt1 = New clsTextBox
           mobjTxt1.Init rtxt:=Me!TextBox1
      • #577167

        hey thanks Charlotte!
        it works great! i thought for sure i was going to have to ask you to help me understand more how to get your code to work, but i figured it out.

        i even created a class for combo boxes as well. works just fine.

        just one thing, is there any way to speed up or reduce the amount of code when declaring all those variables and setting the text boxes (like some sort of array or something?) thanks for your help!

        • #577185

          Well, you could loop through the controls in the form, test the controltype of each and if it was a textbox assign it to an object variable and bind it to the class. I generally use a clsForm module that I bind the form to. That class contains the code to bind each of the controls to its appropriate class.

          • #577192

            if its not too much trouble, could you send me the sample code for that? it would be greatly appreciated.

            thanks

            • #577347

              Here’s a sample. Create a new class module called clsForm and drop this code into it (you’ll need to paste it into Notepad first, I think, to get rid of the runons caused by the PRE tags that make it look good here). This class doesn’t do much except bind textboxes and comboboxes to their own classes, but it gives you an idea of how you can use a class to bind objects to another class.

              Option Compare Database
              Option Explicit
               
              Private WithEvents cForm As Access.Form
              Private mstrFormNm As String
              Private mcolControls As New Collection
              Private mblnTerminateCalled As Boolean
              Const CLASS_NAME = "clsForm"
                
              Public Function Init(ByRef rfrm As Access.Form)
                On Error GoTo Init_err
                mstrFormNm = rfrm.name
                Set cForm = rfrm
                 
                 'Set up events to intercept
                  cForm.OnClose = "[Event procedure]"
                  cForm.OnError = "[Event procedure]"
                   
                  'Bind the form's controls
                  Init = BindControls(rfrm)
              Init_exit:
                On Error Resume Next
                Exit Function
              Init_err:
                MsgBox "An error has occurred in the system." & vbCrLf _
                      & "Unable to process your instructions.  " _
                      & "(" & mstrFormNm & ")"
                Resume Init_exit
              End Function
               
              Public Sub Terminate()
                On Error GoTo Terminate_err
                Const PROC_NAME = "Terminate"
                  Dim obj As Object
               
                  'We want to call the class terminate for each control
                  If mblnTerminateCalled = False Then
                     For Each obj In mcolControls
                       On Error Resume Next
                       obj.Terminate
                       Err.Clear
                     Next
                     mblnTerminateCalled = True
                  End If
              Terminate_exit:
                Set obj = Nothing
                Exit Sub
              Terminate_err:
                MsgBox CLASS_NAME & "." & PROC_NAME & " Error #" & Err.Number _
                      & vbCrLf & Err.Description
                Resume Terminate_exit
              End Sub
               
              Private Function BindControls(ByRef rfrm As Access.Form) As Boolean
                On Error GoTo BindControls_err
                  Dim ctl As Access.Control
                  Dim objTxt As clsTextBox
                  Dim objCbo As clsComboBox
                    
                  Const PROC_NAME = "BindControls"
                  Const CTL_NOT_FOUND = 2465
                   
                  If cForm Is Nothing Then
                      Set cForm = rfrm
                  End If
                    
                  For Each ctl In cForm.Controls
                    If TypeOf ctl Is Access.TextBox Then
                      Set objTxt = New clsTextBox
                      objTxt.Init rtxt:=ctl
                      mcolControls.Add objTxt
                       
                    ElseIf TypeOf ctl Is Access.ComboBox Then
                      Set objCbo = New clsComboBox
                      objCbo.Init rcbo:=ctl
                      mcolControls.Add objCbo
                       
                    Else
                      'Skip any other kind for now
                    End If
                  Next
                  BindControls = True
                   
              BindControls_exit:
                On Error Resume Next
                Exit Function
                
              BindControls_err:
                Select Case Err.Number
                  Case CTL_NOT_FOUND
                    ' Control not used on this form
                    Resume Next
                  Case Else
                    MsgBox CLASS_NAME & "::" & PROC_NAME & " Error #" & Err.Number _
                      & vbCrLf & Err.Description
                    Resume BindControls_exit
                End Select
              End Function
                
              Private Sub cForm_Open(Cancel As Integer)
                On Error Resume Next
              End Sub
                
              Private Sub cForm_Close()
                Me.Terminate
              End Sub
               
              Private Sub Class_Terminate()
                Set cForm = Nothing
              End Sub
            • #577431

              thanks charlotte,
              but i still don’t understand how to put it into action and exactly what part of the code it eliminates that i had before. instead of setting each text box and combo box individually, this will set them automatically, right? please help me put this to work.

              thanks

            • #577435

              It eliminates all the control object variables and code you needed to bind the controls to their classes. Instead, you declare a module level form variable, like Dim mFrm As clsForm, in the form and bind the form to clsForms in the Form_Open event. The control binding is taken care of by the clsForm class when it gets initialized. So in the Form_Open event, you would have code something like this:

              Set mFrm = New clsForm
              mFrm.Init Me

              In the Current event of the form, you might want to include this code:

              If mFrm is Nothing Then
              Set mFrm = New clsForm
              mFrm.Init Me
              End If

              That insures that if the class gets stepped on somehow (yes, it happens), it will get reinstantiated and the code will keep working.

            • #577438

              that works wonderfully!

              thanks for all your help charlotte! you never cease to amaze me. bow

            • #577439

              blush When one lives as long as I have, one is bound to have learned something! grin

            • #577444

              I think that I will have to save this thread somewhere where I can study it at lesiure smile

    Viewing 0 reply threads
    Reply To: global form events (access 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: