Error in inserting Date in the INSERT INTO statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gazelle04
    New Member
    • Jul 2006
    • 52

    Error in inserting Date in the INSERT INTO statement

    I'm trying to enter the current date into the table through code but there's an error in inserting the date.

    Whats wrong with this code:

    strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
    strSQL = strSQL & "VALUES ( " & intUnivId & " , " & strBranchID & ", " & Date() & " );"

    I don't know how to make the Date a string in order to run in the SQL statement.
    Or should I leave it as it is without quotation marks.
  • PEB
    Recognized Expert Top Contributor
    • Aug 2006
    • 1418

    #2
    Try the follow:

    strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
    strSQL = strSQL & "VALUES ( " & intUnivId & " , " & strBranchID & ", Now()" );"


    or


    strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
    strSQL = strSQL & "VALUES ( " & intUnivId & " , " & strBranchID & ", Date()" );"


    Have a nice luck!

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      You should also leave a space before the word VALUES:

      strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
      strSQL = strSQL & " VALUES ( " & intUnivId & " , " & strBranchID & ", Date()"


      Originally posted by gazelle04
      I'm trying to enter the current date into the table through code but there's an error in inserting the date.

      Whats wrong with this code:

      strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
      strSQL = strSQL & "VALUES ( " & intUnivId & " , " & strBranchID & ", " & Date() & " );"

      I don't know how to make the Date a string in order to run in the SQL statement.
      Or should I leave it as it is without quotation marks.

      Comment

      • PEB
        Recognized Expert Top Contributor
        • Aug 2006
        • 1418

        #4
        And finally ;)

        strSQL = "INSERT INTO University ([UniversityID], [BranchID], [DateOpened])"
        strSQL = strSQL & " VALUES ( " & intUnivId & " , " & strBranchID & ", Date() );"

        Comment

        Working...