Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:19:28+00:00 2026-06-09T20:19:28+00:00

I am trying to use SQL in visual basic to insert Date and Time

  • 0

I am trying to use SQL in visual basic to insert Date and Time value into Access 2007 running in Access2002/3 mode. Using my code I have managed to insert text strings and numerical values into the table. However for the DateTime field get a syntax error. The field in Access is set as a Date/Time field type.

Below is my first function which generates the queries for the Database Accessing function:

Public Function NewUpload(ByVal UploadType As String) As Single

    Dim UploadNumber As Single
    Dim ColumnString As String
    Dim ValueString As String

    If DatabaseConnection("SELECT ID_UPL FROM tabUpload", "Read Recordset") = "Error" Then GoTo close
    Do Until rdrOLEDB.Read = False
        If Val(rdrOLEDB.Item(0).ToString()) > UploadNumber Then UploadNumber = Val(rdrOLEDB.Item(0).ToString())
    Loop
    rdrOLEDB.Close()
    cnnOLEDB.Close()

    UploadNumber = UploadNumber + 1

    'Update Uploads table:
    ColumnString = "ID_UPL,DateTime,IDUser,DataCalc"
    ValueString = Format(UploadNumber, "0000") & ",#" & Now.ToLongDateString & " " & Now.ToLongTimeString & "#,'" & My.User.Name & "','" & UploadType & "'"
    If DatabaseConnection("INSERT INTO tabUpload(" & ColumnString & ") VALUES(" & ValueString & ")", "Non-Query") = "Error" Then GoTo Close

    NewUpload = UploadNumber

Close:
cnnOLEDB.Close()

End Function

Here is the second Function which connects to the Database

Public Function DatabaseConnection(ByVal Query As String, ByVal Task As String) As String
    'On Error GoTo Err

    cnnOLEDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataDirectoryName & DatabaseFileName
    cmdOLEDB.Connection = cnnOLEDB
    cmdOLEDB.CommandText = Query
    cnnOLEDB.Open()

    Select Case Task
        Case "Read Recordset"
            rdrOLEDB = cmdOLEDB.ExecuteReader()
            DatabaseConnection = "Read Recordset"
        Case "Read Scalar"
            DatabaseConnection = Str(cmdOLEDB.ExecuteScalar)
        Case "Non-Query"
            DatabaseConnection = Str(cmdOLEDB.ExecuteNonQuery())
            DatabaseConnection = "Non-Query"
    End Select

    Exit Function
Err:
    MsgBox("Database connection error.")
    DatabaseConnection = "Error"

End Function

When I run this code I get the Query:
INSERT INTO tabUpload(IDUPL, DateTime, User, DataCalc) VALUES(0003, #17 August 2012 14:23:27#, 'UK\Pej', 'Calc')"

I have also tried several variations of DateTime using format and now(Year) giving yyyy-mm-dd etc.

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:19:29+00:00Added an answer on June 9, 2026 at 8:19 pm

    Both datetime and user are reserved words and must be enclosed in square brackets. Otherwise, it will run with that date.

     INSERT INTO tabUpload(IDUPL, [DateTime], [User], DataCalc) 
     VALUES(0003, #17 August 2012 14:23:27#, 'UK\Pej', 'Calc')"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use LINQ to SQL Server in C#/.NET on Visual Studio 2010.
I am currently trying out visual studio to access sql server databases instead of
I'm trying to use SQL Server Management Studio to edit database tables. But I
I'm trying to use SQL Server Profiler (2005) to track down some application performance
I am trying to use a SQL Server 2008 Ranking Function on a query
Trying to use Linq to SQL for a small project I'm working on at
I'm trying to use LINQ to SQL to select a few specific columns from
I'm trying to use linq to sql for my project (very short deadline), and
Trying to use: :On Error exit :r D:\opt\db_objects\REPORTS\dbo.sp_ReportCountLORUsers.sql ...and I get: Msg 102, Level
I am trying to use some parameters in my sql query like this; Answer_table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.