Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I want to insert textbox, radio button and Month calender values to ms access 2010 database using vb.net.

I want to clear the all the text box values, unchecked the radio buttons and set system date for month calender after insert the values to database.

But it says there is a syntax error at INSERT INTO statement.

Please help me on this.

What I have tried:

Imports System.Data.OleDb
Public Class dailyTrackingfrm
    Dim Provider As String
    Dim datafile As String
    Dim connString As String
    Public myConnection As OleDbConnection = New OleDbConnection
    Public dr As OleDbDataReader
-------------------------------------------------------------------
<pre> Private Sub dailyTrackingfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        datafile = "D:\Tubewell\Tubewell\Tubewell.accdb"
        connString = Provider & datafile
        myConnection.ConnectionString = connString
    End Sub
-----------------------------------------------------------------------------------

Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
        Dim res As String
        Dim s As String
        Dim obj As New DateTime()
        If RadioButton1.Checked = True Then
            s = RadioButton1.Text
        Else
            s = RadioButton2.Text
        End If
        res = MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Record")
        If res = vbYes Then
            Try
                Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb")
                    Using sqlquery As New OleDb.OleDbCommand("INSERT INTO DailyTracking(EmpNo,Type,Attendance,Date) VALUES (?,?,?,?)", sqlconn)
                        sqlquery.Parameters.AddWithValue("@EmpNo", txtempno.Text)
                        sqlquery.Parameters.AddWithValue("@Type", txttype.Text)
                        sqlquery.Parameters.AddWithValue("@Attendance", s)
                        sqlquery.Parameters.AddWithValue("@Date", MonthCalendar1.SelectionStart)
                        sqlconn.Open()
                        sqlquery.ExecuteNonQuery()
                        MsgBox("Records inserted successfully", vbOKOnly, "Records inserted")
                        txtempno.Text = ""
                        txttype.Text = ""
                        RadioButton1.Checked = False
                        RadioButton2.Checked = False
                        MonthCalendar1.TodayDate = obj
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class
Posted
Updated 24-Aug-17 7:33am

1 solution

Your not declaring the parameters correctly in the INSERT string try;

Using sqlquery As New OleDb.OleDbCommand("INSERT INTO DailyTracking(EmpNo,Type,Attendance,Date) VALUES (@EmpNo,@Type,@Attendance,@Date)", sqlconn)


side note; It is not a good idea to use reserved words for field names, Date is a variable type for instance, when using names like this block the name like so [Date] so it is identified as a field not a variable type.
 
Share this answer
 
Comments
Richard Deeming 25-Aug-17 13:25pm    
AFAIK, the Access OLEDB provider doesn't use named parameters, so the ? placeholders are correct.
Michael_Davies 25-Aug-17 13:33pm    
AFAIK please see https://code.msdn.microsoft.com/windowsdesktop/access-2007-oledb-with-2fed4cc1

Either way he is mixing ? with placeholder names.
Richard Deeming 25-Aug-17 13:36pm    
In that case, I suspect it supports both, and only cares about the order of the placeholders matching the order in which the parameters are added.

But TBH, it's a long time since I touched an Access database! :)
Member 13363527 26-Aug-17 13:20pm    
Yes, It works. Thanks lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900