Results 1 to 3 of 3

Thread: Date Field

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    19

    Date Field

    Hi I am new to VB 2010. I have the following code which saves the data from 4 text boxes. The last one is a date field (txtdate) in the database and it gives me error on it. Can you please let me know what is the syntax to specify a date value? I tried with and without quotes. But didn't work.
    Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim cn As New OleDb.OleDbConnection
            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\db1.mdb;Persist Security Info=True"
            cn.Open()
            Dim Command As OleDb.OleDbCommand
    
            Command = New OleDb.OleDbCommand("INSERT INTO Projects1(ProjectID, ProjectName, ProjectDesc, ProjectDate)" & _
            " VALUES (" & txtid.Text & ",'" & txtname.Text & "', '" & txtdesc.Text & "', '" & txtdate.Text & "')", cn)
            Command.ExecuteNonQuery()
            cn.Close()
            MsgBox("New Records Added to the database")
        End Sub
    Last edited by sn15164; Jan 14th, 2012 at 08:49 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Date Field

    Don't use string concatenation to build SQL statements. Use parameters and this and various other issues just go away. Follow the Blog link in my signature and check out my post on Parameters In ADO.NET to learn how.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    19

    Re: Date Field

    Thank You. will do that.

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