Multiple check boxes enabled or disabled depending on previous (1 Viewer)

veraloopy

Registered User.
Local time
Today, 12:24
Joined
Apr 10, 2009
Messages
139
Bit of a tricky one here, sort of know what I've got to do but keep thinking that there must be an easier way.... :)

I have several check boxes on a form which represent 'stages' of work to be carried out, along with start and end dates for each stage. What I'm trying to achieve is that the stages have to be completed in order. (i.e. Stage 4 cannot be checked until stages 1,2 and 3 have been checked)

I've thought about using the following code on each checkbox but by the time I get to stage 8, the code will be massive....

if me.chkStage1 = True then
me.chkStage2.Enabled = True
me.dtStg2StartDate.Enabled = True
me.dtStg2EndDate.Enabled = True
end if

if me.chkStage2=true then
me.chkStage3.Enabled=true
me.dtStg3StartDate.Enabled = True
me.dtStg3EndDate.Enabled = True
end if

.... and so on

But I guess I'll need to add similar code to the 'after update' of each check box and also the form load event as well.

Does anyone know an easier way?

Many thanks
 

JANR

Registered User.
Local time
Today, 13:24
Joined
Jan 21, 2009
Messages
1,623
You could create a sub and in the After_Update event of each Checkbox call this Sub, also use the On_Current event of the form.

Since a checkbox is a boolean control you can use the status of the control as a check so you don't need IF-Then-Else block.

Code:
Private Sub EnableChkBox()
Me.chkStage2.Enabled = Me.chkStage1
Me.dtStg2StartDate.Enabled = Me.chkStage1
Me.dtStg2EndDate.Enabled = Me.chkStage1
 
Me.chkStage3.Enabled = Me.chkStage2
Me.dtStg3StartDate.Enabled = Me.chkStage2
Me.dtStg3EndDate.Enabled = Me.chkStage2
 
.... And So on
End Sub

Code:
Private FormName_OnCurrent()
   EnableChkBox
End Sub

Code:
Private chkStage1_AfterUpdate()
   EnableChkBox
End Sub

JR
 

JANR

Registered User.
Local time
Today, 13:24
Joined
Jan 21, 2009
Messages
1,623
FYI this only works IF you users follow normal workflow and dosen't get creative :eek:

As you can see if chechbox 1-3 is true and some savy users deside to uncheck box no. 1 then all the boxes in stage 2 will disable, but in stage 3 it will still be enabled.

You have to have some locking code to prevent this. :)

JR
 

vbaInet

AWF VIP
Local time
Today, 12:24
Joined
Jan 22, 2010
Messages
26,374
Here's another way:
Code:
public sub CheckStages()
   Dim i as integer

   if me.controls(Me.ActiveControl.Name).value = true then
      if right(Me.ActiveControl.Name, 1) <> "8" then
         me.controls("chkStage" & val(right(Me.ActiveControl.Name, 1)) + 1).enabled = true
      end if
   else
      if right(Me.ActiveControl.Name, 1) <> "8" then
         for i = val(right(Me.ActiveControl.Name, 1)) to 7
            me.controls("chkStage" & i + 1).value = false
            me.controls("chkStage" & i + 1).enabled = false
         next
      end if
   end if
end sub
Also follow JR's advice re calling the function from the After_Update event of each of the checkboxes.

For the Current event you would use similar code to iterate through all checkboxes and disable where necessary. So if checkbox2 is True and checkbox3 is False, set Checkbox4, 5, 6, 7, 8 to False and Disable.

NB: The code hasn't been tested but it gives you some idea
 

veraloopy

Registered User.
Local time
Today, 12:24
Joined
Apr 10, 2009
Messages
139
I've tried the suggestion from JanR and it works great :)

Many thanks for all your replies, this has saved me loads of time :)

Thanks again :) :) :) :)
 

Users who are viewing this thread

Top Bottom