Results 1 to 7 of 7

Thread: i can't save Records to Access Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    9

    i can't save Records to Access Database

    hi
    i used Textbox,Combobox and Checkbox in form ...
    from that .. input records to database .. but ... i can't save


    Dim mycon As New OleDbConnection(connstring)
    Dim mycmd As New OleDbCommand("INSERT INTO DailyR_tbl (DateIn,DName,Holiday,ComID,DepID,SecID,StaffName,9-10AM,10-11AM,11-12AM,1-2PM,2-3PM,3-4PM,4-5PM) VALUES ( '" & CDate(txtDate.Text) & "','" & Daycombo.SelectedValue.ToString & "','" & Holidaychbox.CheckState.ToString & "','" & Comcombo.SelectedValue.ToString & "','" & Depcombo.SelectedValue.ToString & "','" & Seccombo.SelectedValue.ToString & "','" & Staffcombo.SelectedValue.ToString & "','" & txtJob1.Text & "','" & txtJob2.Text & "','" & txtJob3.Text & "','" & txtJob4.Text & "','" & txtJob5.Text & "','" & txtJob6.Text & "','" & txtJob7.Text & "')", mycon)
    If (mycon.State = ConnectionState.Closed) Then
    mycon.Open()
    End If
    Try
    mycmd.ExecuteNonQuery()
    mycon.Close()
    mycon.Dispose()
    MessageBox.Show("save successfully")
    Catch ex As Exception
    MessageBox.Show("Can't Save")
    End Try

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: i can't save Records to Access Database

    what is the error message?

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    9

    Re: i can't save Records to Access Database

    OleDbException was caught ::

    syntax error in INSERT INTO statement

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: i can't save Records to Access Database

    It's very difficult to read all of that. I think if there was ever a case for Parameters it's this. Try this, if nothing else it will help you track down the problem:
    2 Code:
    1. Dim mycmd As New OleDb.OleDbCommand _
    2.             ("INSERT INTO DailyR_tbl (DateIn, DName, Holiday, ComID, DepID, SecID, StaffName, 9-10AM, 10-11AM, 11-12AM, 1-2PM, 2-3PM, 3-4PM, 4-5PM) " & _
    3.             "VALUES (@myDate, @dayCombo, @holBox, @comCombo, @DepCombo, @SecCombo, @staffCombo, @Job1, @job2, @job3, @job4, @job5, @job6, @job7)", mycon)
    4.  
    5.             mycmd.Parameters.AddWithValue("@myDate", CDate(txtDate.Text))
    6.             mycmd.parameters.addwithvalue("@myCombo", Daycombo.SelectedValue.ToString)
    7.             mycmd.parameters.addwithvalue("@holBox", Holidaychbox.CheckState.ToString)
    8.             mycmd.parameters.addwithvalue("@comCombo", Comcombo.SelectedValue.ToString)
    9.             mycmd.parameters.addwithvalue("@depCombo", Depcombo.SelectedValue.ToString)
    10.             mycmd.parameters.addwithvalue("@secCombo", Seccombo.SelectedValue.ToString)
    11.             mycmd.parameters.addwithvalue("@staffCombo", Staffcombo.SelectedValue.ToString)
    12.             mycmd.parameters.addwithvalue("@job1", txtJob1.Text)
    13.             mycmd.Parameters.AddWithValue("@job2", txtJob2.Text)
    14.             mycmd.Parameters.AddWithValue("@job3", txtJob3.Text)
    15.             mycmd.Parameters.AddWithValue("@job4", txtJob4.Text)
    16.             mycmd.Parameters.AddWithValue("@job5", txtJob5.Text)
    17.             mycmd.Parameters.AddWithValue("@job6", txtJob6.Text)
    18.             mycmd.Parameters.AddWithValue("@job7", txtJob7.Text)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: i can't save Records to Access Database

    try a bsic insert statement see it it works, then add your other bits in slowly....that way you will see where issue is.

    Normally I would built up the sql put it out to text box..open up Access and past in SQL into query run that and see if it works...best way to test this stuff

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: i can't save Records to Access Database

    It looks like you are tring to post date to a text field and are not using single qoutes around the values.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: i can't save Records to Access Database

    Try putting a breakpoint before the Insert command. Then step into it and hover over your command. Copy the command (it should have the values in it) and take it to Access. You should be able to run the command and it will tell you where your error is at.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

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