Results 1 to 4 of 4

Thread: Active Control

  1. #1
    Join Date
    Apr 2002
    Posts
    168

    Unanswered: Active Control

    I have a form that is based on a table with about 40 fields and half of the fields are Yes/No type (checkbox type in form). I am creating an event procedure (After Update) for each Yes/No field. Here is the sample:


    Private Sub Field1_AfterUpdate()
    If Field1.Value = -1 Then
    Field30.value = -1
    Field31.value = -1
    ...
    Field40.value = -1
    End If
    End Sub

    Private Sub Field2_AfterUpdate()
    If Field2.Value = -1 Then
    Field30.value = -1
    Field31.value = -1
    ...
    Field40.value = -1
    End If
    End Sub

    and so on until Field 20.


    I feel this is very repetitive and I am trying to make a more efficient code. The only difference between each procedure is in the If statement, depending on which checkbox/object is clicked. I have 2 questions:

    1. In order to have event procedure for each of the 20 fields, I need to create Private Sub Field_AfterUpdate() for each of them, there is no way to combine them since they are different obejct and therefore different trigger. This is correct, right ?

    2. I'd like to have a general procedure where each of the AfterUpdate procedure can refer to, so should I need to change one line of code, I don't need to copy across the 20 procedures. I am thinking of doing something like this :


    Private Sub Field1_AfterUpdate()
    Check
    End Sub

    Private Sub Field2_AfterUpdate()
    Check
    End Sub

    and so on until Field 20

    Sub Check()
    Set CurrentControl = Screen.ActiveControl
    If CurrentControl.Value = -1 Then
    Field30.value = -1
    Field31.value = -1
    ...
    Field40.value = -1
    End If
    End Sub


    Is this possible ? So, it's reading the last object that gets focused or clicked.




    Thanks

  2. #2
    Join Date
    Jan 2003
    Location
    Rockville, MD
    Posts
    179
    It's a matter of personal preference, I suppose, but I'd do it this way instead.
    Code:
    Private Sub Field1_AfterUpdate()
       Check "1"
    End Sub
    
    Private Sub Field2_AfterUpdate()
       Check "2"
    End Sub
    
    and so on until Field 20
    
    Sub Check(strFieldNumber As String)
       Dim intIndex As Integer
       If Me("Field" & strFieldNumber).Value = -1 Then 
          For intIndex = 30 to 40
             Me("Field" & CStr(intIndex)).Value = -1
          Next
       End If 
    End Sub
    Why bother worrying about the focus?

  3. #3
    Join Date
    Apr 2002
    Posts
    168
    Thanks a lot. I completely agree with you that this might be an easier way. However, the example I give for field names are just examples, so they are not exactly Field1, Field2 and so on. They could be Name, Address etc (no name regularity between one field to another). Of course, in this case, we can just do:

    Private Sub Name_AfterUpdate()
    Check "Name"
    End Sub

    The only concern I have with this method is that I need to make sure that I pass the right field name for 20 private sub, and should the field name change, I need to update the passing value. What I want to make sure is that active control will always work in this case (give the right object that just clicked) ? And the other question to confirm is, I need create those 20 private sub since they are for different objects and triggers ?

  4. #4
    Join Date
    Jan 2003
    Location
    Rockville, MD
    Posts
    179
    Oh, then this might work better. In the "AfterUpdate" event of each field, instead of choosing "[EventProcedure]", put:

    =Check()

    Then add this procedure to the form:
    Code:
    Private Function Check() As Boolean
       If (ActiveControl.Value = -1) Then
          Field30.value = -1
          Field31.value = -1
          ...
          Field40.value = -1
       End If
    End Function
    Now you don't need (and can/should delete) the 20 other _AfterUpdate private subs. And you don't have to worry about the name of the calling control.
    Last edited by JTRockville; 05-19-03 at 02:28.

Posting Permissions

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