Using conditional statement to enable and disable cmd buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Maiapu
    Contributor
    • Feb 2010
    • 259

    Using conditional statement to enable and disable cmd buttons

    I know this is pretty much simple but my brain is jammed and I cannot really make any progress with this.
    Can someone assist me with this problem?

    Ok it’s to do with validation for Overtime during working days and weekend only:
    I have 3 text boxes
    1. TimeIn
    2. TimeOut
    3. DateWorked

    And 3 command buttons that are disable by default:
    1. cmdNew ‘On click goes to new record
    2. cmdclockin ‘inserts time into TimeOut -clock out
    3. cmdclockout ‘inserts date into DateWorked and time into TimeIn –clockin

    On the On_Current event of the form I wish to control how these buttons are enable and disable triggered by each particular event.

    Ok when a user clicks on cmdclockin to clock in, Date() and Time is inserted into DateWorked and TimeIn respectively.

    I am confronted with so many if conditions and am stacked on how to logically arrange them so that they work perfectly as I wish.

    This is what I would like to do:

    1.If DateWorked<>dat e or IsNull(DateWork ed)=True then
    cmdNew.Enable=T rue

    2. If IsNull(TimeIn)= True and IsNull(TimeOut )=False then
    cmdclockout.Ena ble=True
    cmdclockin.Enab le=False

    3. If IsNull(DateWork ed)=True Then
    cmdclockout.Ena ble= True
    cmdclockin.Enab le= False

    4 If IsNull(DateWork ed)=False And IsNull(TimeOut) =True Then
    cmdclockout.Ena ble= False
    cmdclockin.Enab le= True
    5. if Time<#4:06pm # then ‘they need to clock in after 4:06 pm
    cmdNew.Enable=F alse 'Ready to Go to new record if clicked
    else
    cmdNew.Enable=T rue

    I know most people will figure out the logic. The problem is that I just need to logically put these so that it works.

    Thanks so much. It’s quite easy but my mind is not really fresh at this point of time because of a kind of pressure am going through and I cannot think logically at this time. So please help me out.

    If anyone think that I am missing some important component do include in the solution or ask.

    Thank you and thank you

    Jerry
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Statement 1 and 5 can be combined to:
    Code:
    If DateWorked <> Date Or Time < #4:06:00 PM# Then
        cmdNew.Enable = True
    Else
        cmdNew.Enable = False
    End If
    With your statement:
    Code:
    DateWorked<>date or IsNull(DateWorked)=True
    You will notice the second condition iss redundant because if DateWorked is Null it will not equal date.

    The rest are simply made into two conditions:
    Code:
    If (IsNull(TimeIn) And Not IsNull(TimeOut)) Or IsNull(DateWorked) Then
        cmdclockin.Enable = True
        cmdclockout.Enable = False
    ElseIf Not IsNull(DateWorked) And IsNull(TimeOut) Then
        cmdclockin.Enable = True
        cmdclockout.Enable = False
    End If
    I removed the "= True" and "= False" as they are not necessary.

    Just some thoughts on making it shorter.

    Comment

    • nico5038
      Recognized Expert Specialist
      • Nov 2006
      • 3080

      #3
      I guess you don't need a [New] button, as it's clear when opening the form that there's a record for today or not.
      When there isn't a record you can insert a new record with empty In and Out time fields.

      Remains a test for the TimeIn to be empty or filled.
      I would disable the TimeOut when the TimeIn is empty, thus only one [Set time] button will be needed.

      Idea ?

      Nic;o)

      Comment

      • Jerry Maiapu
        Contributor
        • Feb 2010
        • 259

        #4
        Nico, I definitely need a New button. I know what you're saying is true usually but this a different case. Well, when opening the form if there is a record for today then we have to be sure that the user has clocked out if not then we have to disabled the new button because if not disabled people might unnecessarily insert new records without clocking out.

        And for the TimeIn and TimeOut buttons what you say is what I wish to do too. But as I mentioned earlier am just perplexed here. Hope you could contribute like how TheServant replied.

        TheServant, Thank you so much for relieving me. Just like what Nic;o suggested When clocked in cmdclockin and cmdNew button should be disabled while cmdclockin is enable. And after cmdNew is clicked cmdNew and cmdclockin are disable so that they only clock in at time.

        I'll see and collect these ideas and put them together. But do post hints and helps

        Thanks
        Thanks so much

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          Hmm, looks like you're trying to use an Access form as a ClockIn/Out machine.

          Makes me wonder how you distinguish between the different employees. Looks to me they need to enter their (user)name first. This would allow you to check or there's for the running day a record and insert one when not present and enable the In/Out buttons depending on the value(s) found.

          Another consideration is the fact or overtime is recorded in an additional record, thus making it possible to have the StartTime entered twice in one day....

          Nic;o)

          Comment

          • Jerry Maiapu
            Contributor
            • Feb 2010
            • 259

            #6
            Originally posted by nico5038
            Hmm, looks like you're trying to use an Access form as a ClockIn/Out machine.

            Makes me wonder how you distinguish between the different employees. Looks to me they need to enter their (user)name first. This would allow you to check or there's for the running day a record and insert one when not present and enable the In/Out buttons depending on the value(s) found.

            Another consideration is the fact or overtime is recorded in an additional record, thus making it possible to have the StartTime entered twice in one day....

            Nic;o)
            Yes. Employees enter username to log in. Hereafter the clock-in/out form is loaded where the username=userna me.
            The clock-in/out form has a subform to do the clock ins and outs. The sub form is in SINGLE FORM VIEW NOT DATA SHEET and Navigation button property is set to "NO" likewise the parent form. When someone is allowed overtime by the manager (with admin privileges) a cmdovertime button is enabled in the clock-in/out form.
            The employees click on this cmdovertime button where the overtime form is loaded and that is where my problem was; in enabling/disabling cmd buttons on the overtime form.

            The overtime form is just the same as clock-in/out with similar sub form & proprieties.

            This is what I did.
            I sliced my problem and shared it with the cmdovertime button's on click event:

            Code:
            If DateWorked <>VbSaturday or DateWorked<>VbSunday And Time < #4:06:00 PM# Then
            msgbox"Overtime is done after close of business hours"
            exit sub
            else
            docmd.OpenForm "cmdovertime"
            then I did this for the on current event for the overtime's sub form to enable/disable buttons which is ok now..thanks every one for contributions..

            Code:
            Private Sub Form_Current()
            If DateWorked <> Date Then
            cmdNew.Enabled = True
            cmdout.Enabled = False
            cmdIn.Enabled = False
            ElseIf IsNull(TimeOut) Then
            cmdout.Enabled = True
            cmdIn.Enabled = False
            cmdNew.Enabled = False
            Else
            cmdout.Enabled = False
            cmdIn.Enabled = False
            cmdNew.Enabled = True
            End If
            
            If IsNull(DateWorked) Then 'when cmdNew button
            'is clicked DateWorked=Null therefore
            cmdNew.Enabled = False
            cmdout.Enabled = False
            cmdIn.Enabled = True
            End If
            
            End Sub

            Comment

            Working...