troubleshooting Question

VB6 - Check if date format = "MM/DD/YYYY' and check if date also exist

Avatar of Wilder1626
Wilder1626Flag for Canada asked on
Visual Basic Classic
23 Comments1 Solution966 ViewsLast Modified:
Hi,
I need your help with below code. I must check all dates in MSHFlexgrid1 col (1) to see if the date is a real date with format "MM/DD/YYYY"

If the date don't exist, it will put in Col(0) the note: "BAD DATE FORMAT".

Example:
If it says in Col(1): 01/15/2017, January 15 2017 is a date that exist, so not problem.
If it says in Col(1): 15/01/2015, this is a bad date as there is no Month 15.

So far, i have this code:
Private Sub val_date_exist_Click()
    Dim strDate As String
    Dim dt As Date
    Dim I As Long
    For I = 1 To MSHFlexGrid1.Rows - 1

        strDate = Trim(MSHFlexGrid1.TextMatrix(I, 1))
        dt = DateSerial(CInt(Mid(strDate, 1, 2)), CInt(Mid(strDate, 4, 2)), CInt(Mid(strDate, 7, 4)))
        If strDate = Format(dt, "MM/DD/YYYY") Then

        Else
            MSHFlexGrid1.TextMatrix(I, 0) = MSHFlexGrid1.TextMatrix(I, 1) & " - " & "BAD DATE FORMAT"
        End If
    Next

    'Auto column fit
    Dim c As Long
    Dim cell_wid As Single
    Dim col_wid As Single
    Dim z As Long
    For c = 0 To MSHFlexGrid1.Cols - 1
        col_wid = 0
        For z = 0 To MSHFlexGrid1.Rows - 1
            cell_wid = TextWidth(MSHFlexGrid1.TextMatrix(z, c))
            If col_wid < cell_wid Then col_wid = cell_wid
        Next z
        MSHFlexGrid1.ColWidth(c) = col_wid + 120
    Next c

End Sub

But it always tell me that the date is bad even if it as the good format.

I must be missing something in the code.

Hope you will be able to help.

Thank you
Date-validation.zip
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 23 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros