Results 1 to 6 of 6
  1. #1
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100

    Compare BeginTime and EndTime

    I have a Down Time table with a BeginTime field and an EndTime field. These fields are separate from the BeginDate field and EndDate field and are formatted as Date/Time fields. When entering the down Time on a form I want to ensure that the EndTime entered is later than the BeginTime. MWhen the "Save" button is clicked, I perform a check to make sure BeginTime is greater than EndTime as shown below:



    Select Case Me.txtEndTime
    Case (IsNull(TimeValue(Me.txtEndTime)) Or (TimeValue(Me.txtEndTime) = #12:00:00 AM#))
    myMsg = MsgBox("You must enter a valid End Time.", vbInformation, "Invalid End time")
    Cancel = True
    DoCmd.GoToControl "txtEndTime"
    ' Case (TimeValue(Me.txtEndTime) < TimeValue(Me.txtBeginTime))
    Case (StrComp(Me.txtEndTime, Me.txtBeginTime) = -1)
    myMsg = MsgBox("End Time must be greater than Begin Time", vbInformation, "Invalid End time")
    Cancel = True
    DoCmd.GoToControl "txtEndTime"
    Case (Me.txtEndTime < Me.txtBeginTime) And (DateValue(Me.txtEndDate) <= DateValue(Me.txtBeginDate))
    myMsg = MsgBox("End Time must be greater than Begin Time", vbInformation, "Invalid End time")
    Cancel = True
    DoCmd.GoToControl "txtEndTime"
    End Select

    When I debug the code it just falls through even though I enter an End time that is less than the begin time. In the immediate window the StrComp function returns 1 in stead of -1. The first Case is Ok when I don't enter an EndTime or skip the txtEndTime control that has a time of 0:00. the Third Case doesn't catch an invalid EndTime either.

    What am I doing wrong? Please help.

    Thank you.

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Combine both Date & Time together on both side (EndDate_PlusTime - Begin_Date_PlusTime) to make a simple comparison.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you dont want 2 fields, 1 for date and 1 for time.
    the datetime field holds both...this allows for getting the difference txtDateTime1 - txtDateTime2

    if you already have them separate, combine them in a query. Date1 & " " & Time1
    you cannot do time math on separate fields. it must be:
    if dateTime1 < dateTime2

  4. #4
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    apr pillai:

    Thnk you for your response. I replaced the case with the StrComp with the following:

    Case ((Me.txtEndDate + Me.txtBeginTime) - (Me.txtBeginDate + Me.txtBeginTime) <= 0)

    In the immediate window it returned 0 but in debugging, the case statement didn't catch it. Should I use the StrComp ?

  5. #5
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Quote Originally Posted by shylock View Post
    apr pillai:

    Thnk you for your response. I replaced the case with the StrComp with the following:

    Case ((Me.txtEndDate + Me.txtBeginTime) - (Me.txtBeginDate + Me.txtBeginTime) <= 0)

    In the immediate window it returned 0 but in debugging, the case statement didn't catch it. Should I use the StrComp ?

    I see a Problem with the Time part:

    Case ((Me.txtEndDate + Me.txtEndTime) - (Me.txtBeginDate + Me.txtBeginTime) <= 0)

  6. #6
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    Thanks apr pillai,

    My bad! I'm not the best typist. I was able to solve the problem with the code below:

    LcmpTime = (StrComp(Me.txtEndDate + Me.txtEndTime, Me.txtBeginDate + Me.txtBeginTime))

    If LcmpTime <= 0 Then
    myMsg = MsgBox("End Time must be greater than Begin Time", vbInformation, "Invalid End time")
    Cancel = True
    DoCmd.GoToControl "txtEndTime"
    End If

    How do I mark this thread as "Solved"

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Compare two passwords
    By AmanKaur123 in forum Programming
    Replies: 4
    Last Post: 02-21-2017, 12:41 PM
  2. Replies: 1
    Last Post: 08-15-2016, 10:29 PM
  3. need to compare two tables
    By ZeroWard in forum Access
    Replies: 1
    Last Post: 02-01-2016, 10:26 AM
  4. Replies: 6
    Last Post: 08-06-2015, 09:44 AM
  5. Get Entry From last EndTime Entered
    By sparlaman in forum Forms
    Replies: 0
    Last Post: 03-28-2011, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums