If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Active Control

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-03, 19:01
milan milan is offline
Registered User
 
Join Date: Apr 2002
Posts: 168
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
Reply With Quote
  #2 (permalink)  
Old 05-17-03, 03:14
JTRockville JTRockville is offline
Registered User
 
Join Date: Jan 2003
Location: Rockville, MD
Posts: 177
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?
Reply With Quote
  #3 (permalink)  
Old 05-17-03, 09:02
milan milan is offline
Registered User
 
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 ?
Reply With Quote
  #4 (permalink)  
Old 05-19-03, 01:10
JTRockville JTRockville is offline
Registered User
 
Join Date: Jan 2003
Location: Rockville, MD
Posts: 177
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 01:28.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On