+ Reply to Thread
Results 1 to 4 of 4

Comparing one date to another

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2007
    Posts
    10

    Comparing one date to another

    I swear this worked when I tried it a couple of days ago... now suddenly it doesn't

    It's not doing the bit in bold, checking the date entered is greater than the date in txtDateReg.

    Dim FindPatients As Range
    
    Set FindPatients = myPatients.Find(what:=cboPatients, lookat:=xlWhole)
    txtDateReg.Value = FindPatients.Offset(0, 3)
    txtDateReg.Enabled = True
    
    myDate = InputBox("Enter the Patient Admitted Date", "Date Entry", Date)
    
    If myDate <> "" Then
        Do Until IsDate(myDate)
        If IsDate(myDate) = False Then
            MsgBox "Date is not entered"
            myDate = InputBox("Enter the Patient Admitted Date", "Date Entry", Date)
                If myDate < txtDateReg.Value Then
                    Do Until myDate > txtDateReg.Value
                    MsgBox "Registered Date is after admitted date!"
                    myDate = InputBox("Enter the Patient Admitted Date", "Date Entry", Date)
                    Loop
                End If
        End If
        Loop
    End If

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    An InputBox returns a string, so the comparisons are evaluating things alphabeticaly rather than numericaly. This routine uses CDate to convert those strings to dates. It also uses a more compact structure.
    Dim myDate As String
    Dim FindPatients As Range
    
    Set FindPatients = myPatients.Find(what:=cboPatients, lookat:=xlWhole)
    txtDateReg.Value = FindPatients.Offset(0, 3)
    txtDateReg.Enabled = True
    
    
        Do
            myDate = InputBox("Enter the Patient Admitted Date", "Date Entry", Date)
            If StrPtr(myDate) = 0 Then Exit Do: Rem Cancel pressed
            If IsDate(myDate) Then
                If CDate(myDate) < txtDateReg.Value Then
                        MsgBox "Registered Date is after admitted date!"
                        myDate = "not a date"
                End If
            End If
        Loop Until IsDate(myDate)

  3. #3
    Registered User
    Join Date
    12-09-2007
    Posts
    10
    Thanks that worked

    Could you explain this line in a little more detail please?

    Thanks

    If StrPtr(myDate) = 0 Then Exit Do: Rem Cancel pressed

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Running the following code, the user can either press Cancel, press OK before entering a value or enter something. In the first two cases, userInput will equal vbNullString.
    To distinguish between those:
    StrPtr(userInput) returns the memory location where the value of the variable is stored.
    If OK is pressed, the value vbNullString is assigned to the variable userInput, which involves putting that value in a memory location.
    If Cancel is pressed, no value is assigned to userInput and its pointer remains 0, i.e."unassigned".
    StrPtr(UserInput)=0 will test if the Cancel Button was pressed.

    Sub test()
    Dim userInput As Variant
    
    userInput = InputBox("Enter something")
    
    If userInput = vbNullString Then
        If StrPtr(userInput) = 0 Then
            MsgBox "Cancel was pressed."
        Else
            MsgBox "A zero length string was entered"
        End If
    Else
        MsgBox userInput & " was entered. It is a " & TypeName(userInput)
    End If
    
    End Sub
    Note also that if "123" is entered, it is read as a string not a number. These are two of the reasons that I prefer to use the
    Application.InputBox method rather than InputBox function used in the posted code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1