Results 1 to 11 of 11
  1. #1
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    global form events (access 97)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: global form events (access 97)

    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:

    <pre>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</pre>


    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:
    <pre> Dim mobjTxt1 As clsTextBox
    Dim mobjTxt2 As clsTextBox
    ... etc,</pre>

    In your form, you would bind each control like this:
    <pre>Set mobjTxt1 = New clsTextBox
    mobjTxt1.Init rtxt:=Me!TextBox1</pre>

    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: global form events (access 97)

    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!

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: global form events (access 97)

    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.
    Charlotte

  5. #5
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: global form events (access 97)

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

    thanks

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: global form events (access 97)

    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.

    <pre>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</pre>

    Charlotte

  7. #7
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: global form events (access 97)

    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

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: global form events (access 97)

    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.
    Charlotte

  9. #9
    2 Star Lounger
    Join Date
    May 2001
    Location
    indiana
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: global form events (access 97)

    that works wonderfully!

    thanks for all your help charlotte! you never cease to amaze me. <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: global form events (access 97)

    <img src=/S/blush.gif border=0 alt=blush width=15 height=15> When one lives as long as I have, one is bound to have learned something! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Charlotte

  11. #11
    3 Star Lounger
    Join Date
    Jun 2001
    Location
    Maidstone, Kent, England
    Posts
    398
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: global form events (access 97)

    I think that I will have to save this thread somewhere where I can study it at lesiure <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •