Results 1 to 9 of 9

Thread: Type mismatch in Date/Time value build, starting 1-October.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Type mismatch in Date/Time value build, starting 1-October.

    This code has been working flawless for months of testing (of single digit month numbers?) until the calendar rolled over to 1-Oct or 10/1/2015.

    Now its has a Type Mismatch error 13.

    Here is the code:
    Code:
    Dim d0, d1, d2, d3 As Date
    Dim drt as Date
    'drt value loaded from database value, Format: Short Time, tested Medium Time, same prob.
    'drt = 8:00
            d0 = DateAdd("d", -1, Date) ' Gets previous day's date
            d1 = Format(Now(), Date) ' Gets today's date
            'No error to this point
            '--- Error in below 2 lines ---
            d2 = d0 & " " & drt  'Combine previous day's date & DRT
            d3 = d1 & " " & drt  'Combine today's date & DRT
    
    'Previous results looked like  d2 = 9/26/2015 8:00 AM, d3 = 9/27/2015 8:00 AM
    When I was creating this I could display the newly created d2 and d3 and they were perfect, and worked in my SQL string.

    I have tried everything I can think of.
    What might have changed simply by the month change?

    Any suggestions appreciated.
    Last edited by Enrique_; Oct 1st, 2015 at 11:46 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Type mismatch in Date/Time value build, starting 1-October.

    Well d0 thru d2 are not dimmed as date they will be variants from the way you dimmed them

    You say drt is loaded from the db but from the code shown it does not look to be loaded at all so it looks like you are combing a string that holds a date a space and an empty date value
    I am surprised that you are just now seeing a problem. Looks like you may have made a change as what you say it did display was 8:00 but the line that sets drt to 8:00 is commented out so that code should be giving you 12:00 AM as the value for DRT

    The problem causing the error is in your format statement.
    Code:
    d1 = Format(Now(), "mm/dd/yyyy")
    Of course there is no need for that format statement at all because you would be using a date variable if you dimmed it correctly. furthermore there is no need for the d1 variable at all since Now( ) will give you the same result
    Last edited by DataMiser; Oct 1st, 2015 at 12:16 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Type mismatch in Date/Time value build, starting 1-October.

    DataMiser thank you so much!!

    The format syntax corrected it.

    This is simple test code with format correction, no errors.

    Code:
    Private Sub Command4_Click()
    Dim d0, d1, d2, d3 As Date
    'public drt value = 8:00
    
    d0 = DateAdd("d", -1, Date)
    'd1 = Format(Now(), Date)
    d1 = Format(Now(), "mm/dd/yyyy")
      d2 = d0 & " " & drt
      d3 = d1 & " " & drt
        txtDate1.Text = d2   'Displays:  9/30/2015 8:00:00 AM
        txtDate2.Text = d3   'Displays:  10/1/2015 8:00:00 AM
    End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Type mismatch in Date/Time value build, starting 1-October.

    But the code is still not right, there are a few things wrong there. using variants where you think you are using dates, poorly named variables and improper use of format, more lines than needed.
    Consider the following edit of your code
    Code:
    Private Sub Command1_Click()
    Dim drt As Date
    drt = "8:00"
    
    Dim Yesterday As String, Today As String
    Yesterday = DateAdd("d", -1, Date) & " " & drt
    Today = Date & " " & drt
    
    Debug.Print Yesterday, Today
    
    End Sub
    Result is
    Code:
    9/30/2015 8:00:00 AM        10/1/2015 8:00:00 AM

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Type mismatch in Date/Time value build, starting 1-October.

    As DM stated, you incorrectly dimmed some of these variables when you did this:
    Code:
    Dim d0, d1, d2, d3 As Date
    d3 is dimmed as a date, the other three turn out to be Variants
    If you did want all four (although DM's example proves you definitely don't need them all) to be dates, you'd (commonly) do it like this:

    Code:
    Dim d0 as Date, d1 as Date, d2 as Date, d3 as Date
    Or better yet (for clarity IMHO):
    Code:
    Dim d0 as Date
    Dim d1 as Date
    Dim d2 as Date
    Dim d3 as Date
    How you attempted to dim variables is a very common mistake by initial learners of VB6. Check out this link for more guidance: https://msdn.microsoft.com/en-us/lib...=vs.90%29.aspx

    When you move on to VB.NET (if you do, that is), dimming variables is slightly different. How you did it above is perfectly acceptable in the .NET versions and will produce all four of those variables as Dates...however, that is not true in VB6.
    Last edited by SamOscarBrown; Oct 1st, 2015 at 12:41 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Type mismatch in Date/Time value build, starting 1-October.

    Oh I understand!, about multiple Dimmed items.
    I'll correct all such multiples I have dimmed incorrectly Sam.

    DM
    I do have my drt variable Dimmed in a Module as Date.
    (I omitted that fact in my last post)

    The drt value is pulled from a database field on startup. It is stored as 8:00, no quotes, but works ok.

    I understand your method, simple and compact. I'll use your method.
    Thanks

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Type mismatch in Date/Time value build, starting 1-October.

    If you Dim it in a module and then Dim it in a sub or function any reference within that sub or function is to the one dimmed there not the one from the module so if it is a public variable then you need to remove the dim statement from you routine shown here.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Type mismatch in Date/Time value build, starting 1-October.

    If you Dim it in a module and then Dim it in a sub or function any reference within that sub or function is to the one dimmed there not the one from the module so if it is a public variable then you need to remove the dim statement from you routine shown here.
    This I did know.
    They were only dimmed once in my module.
    I had them dimmed in my initial post so readers would not think I failed to dim.
    Thanks

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Type mismatch in Date/Time value build, starting 1-October.

    Ok.. I kind of thought that may be the case after I posted and looked over the thread again. Just wanted to make sure you knew that.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width