• Date variable in SQL (2002)

    Author
    Topic
    #410648

    SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
    “WHERE Session_Date = ” & dteSessionDate & “;”
    returns a Type Mismatch error – can anyone tell me what’s wrong with my syntax, the field Session_Date is a date field and the variable dteSessionDate is declared as date.

    Viewing 2 reply threads
    Author
    Replies
    • #884130

      Is this running against Access or SQL Server? In either case, you need to delimit the date in the SQL string, but the delimiters are different in Access {#}and in SQL (‘), plus if the back end is SQL Server, you have to watch out for smalldatetime fields as opposed to datetime fields. Can you add some detail on the problem?

    • #884139

      I have tried surrounding the variable in # eg –
      SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
      “WHERE Session_Date = #” & dteSessionDate & “#;”
      with the same result. Running a jet backend but may need to upsize in the future.

      • #884141

        You can try :
        SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
        “WHERE Session_Date = #” & Format(dteSessionDate,”mm/dd/yyyy”) & “#;”

        If you still get an error put a msgbox after you statement :
        SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
        “WHERE Session_Date = #” & dteSessionDate & “#;”
        MsgBox SQLSessionDate

        What is the result in the msgbox ?

        • #884145

          Thank you everyone – I found the problem – some idiot (me) had declared the variable SQLSessionDate as date instead of string.

        • #884146

          Thank you everyone – I found the problem – some idiot (me) had declared the variable SQLSessionDate as date instead of string.

      • #884142

        You can try :
        SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
        “WHERE Session_Date = #” & Format(dteSessionDate,”mm/dd/yyyy”) & “#;”

        If you still get an error put a msgbox after you statement :
        SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
        “WHERE Session_Date = #” & dteSessionDate & “#;”
        MsgBox SQLSessionDate

        What is the result in the msgbox ?

    • #884140

      I have tried surrounding the variable in # eg –
      SQLSessionDate = “SELECT tblSession.* FROM tblSession ” & _
      “WHERE Session_Date = #” & dteSessionDate & “#;”
      with the same result. Running a jet backend but may need to upsize in the future.

    Viewing 2 reply threads
    Reply To: Date variable in SQL (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: