Insert Command

Morn

Active member
Joined
Dec 4, 2006
Messages
40
Programming Experience
Beginner
I am trying to get this to run an insert into the database. I keep getting an error saying that it is missing a ;. I don't really get where that would go.

Any one?

The actual method works in other places of the application so it must be a syntax problem.

VB.NET:
' Creates the OleDb connection
        Dim connect As New OleDb.OleDbConnection
        connect.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Holiday.mdb"

        'Creates a variable to store the sql ? says look for the value in the parameter
        'Always specify the field names that you want the data to be entered into
        Dim sql As String = "INSERT INTO tblbooking (totalcost) VALUES (?) WHERE [BookingID] =" & booking

        'Creates a variable command so that the sql and connection variables are run
        Dim command As New System.Data.OleDb.OleDbCommand(sql, connect)

        'insted of using a concat use the param value
        command.Parameters.AddWithValue("@totalcost", runningtotalofbooking)

        'opens connection
        connect.Open()

        'runs the command variable
        command.ExecuteNonQuery()

        'closes connection
        connect.Close()
 
INSERT INTO tblbooking (totalcost) VALUES (?) WHERE [BookingID] =

You cannot have an INSERT command with a where clause; either you want to insert, or you dont. If you decide you dont want to insert some values, dont run the insert statement!
 
The connection stringis missing it's closing ';':

connect.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Holiday.mdb;"

I've never found that to be an issue.. To generate a conenction string,

Make a file called TEMP.UDL
Double click it and set all the options, test the connection
CLick OK
Open the TEMP.UDL file in notepad
 
Back
Top