Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,242 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,230 people online right now. Registration is fast and FREE... Join Now!




Datetimepicker to Access database

 
Reply to this topicStart new topic

Datetimepicker to Access database, Adding datetimepicker value to access datetime picker

Paul Washburn
10 Oct, 2008 - 12:10 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 22

I am trying to add a new record to an access data base using the INSERT INTO sql function, my code is getting the following error

OLEDb excpetion was unhandled: Syntax error in INSERT INTO statement.

I believe ive managed to narrow it down to the datetimepicker.

both datetimes are set to short format.

ive tried using the following formats for the date field also.

CODE

cboDate.Value.ToShortDateString


CODE

System.Convert.ToDateTime(cboDate.Value).ToOADate


The System.convert generated this error:
InvalidCastException was unhandled: Conversion from string "INSERT INTO tblCaseAdmin ( Digit" to type 'Double' is not valid.

I have also tried changing the date format in VB to custom and setting custom to mm/dd/yyyy, but this gave me a date of 00/10/2008 for some reason.

Current code:
CODE

            Dim addUser As String = "INSERT INTO tblCaseAdmin ( Digit, Day ) VALUES (" + cboDigit.Text & "," + cboDate.text + ")"
            
            Dim cmd As OleDbCommand = New OleDbCommand(addUser, conn)

           'The Error is being flagged on this line        
            cmd.ExecuteNonQuery()


Any ideas?

Sorry if this is a repost, and thank you for the help.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Datetimepicker To Access Database
10 Oct, 2008 - 05:33 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Put the # sign around the date value in the Insert statement.

CODE

Dim addUser As String = "INSERT INTO tblCaseAdmin ( Digit, Day ) VALUES (" + cboDigit.Text & ",#" + cboDate.Value.ToShortDateString + "#)"

User is offlineProfile CardPM
+Quote Post

Paul Washburn
RE: Datetimepicker To Access Database
13 Oct, 2008 - 06:50 AM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 22

Tried the # with no luck, still generate the same syntax error.
User is offlineProfile CardPM
+Quote Post

dlkirk03
RE: Datetimepicker To Access Database
13 Oct, 2008 - 06:08 PM
Post #4

New D.I.C Head
*

Joined: 12 Sep, 2008
Posts: 24


My Contributions
Change the string sqlstmt to your sql Statement and it will execute your statement. at least if you do it in access this works. I will get a coding for VB.net in the morning.

CODE

Dim sqlstmt As String
Dim con As DAO.Database
Set con = CurrentDb
sqlstmt = "CREATE TABLE test (First_Name TEXT(20), Last_Name TEXT(25), dob DATETIME, Constraint Employees_PK Primary Key (First_Name, Last_Name, dob));"
con.Execute (sqlstmt)
con.Close


This post has been edited by dlkirk03: 13 Oct, 2008 - 06:20 PM
User is offlineProfile CardPM
+Quote Post

Paul Washburn
RE: Datetimepicker To Access Database
14 Oct, 2008 - 05:13 AM
Post #5

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 22

Not sure this is the same thing. The table already exists in Access, im just trying to add a new record or update the table. From waht i can gather the code is not running due to a conversion inconsistency between Access and Vb.net in the datetimepicker.

Thanks for the help though.
User is offlineProfile CardPM
+Quote Post

Paul Washburn
RE: Datetimepicker To Access Database
14 Oct, 2008 - 09:10 AM
Post #6

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 22

I was able to track down a solution from

http://www.devnewsgroups.net/group/microso.../topic3403.aspx

The issue was resolved by placing [] brackets around Date

CODE

Dim addUser As String = "INSERT INTO tblCaseAdmin ( Digit, [Day] ) VALUES (" + cboDigit.Text & ",#" + cboDate.Value.ToShortDateString + "#)"

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:45AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month