Source :
PHP Code
<?php Dim database As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\test.accdb") Dim cmdadd As New OleDb.OleDbCommand cmdadd.CommandText = "INSERT INTO table (ID, name) VALUES (@ID, @name);" cmdadd.Connection = database cmdadd.Parameters.AddWithValue("ID", txtid.Text) cmdadd.Parameters.AddWithValue("name", txtname.Text) database.Open() cmdadd.ExecuteNonQuery() database.Close()
and Tested This :
PHP Code
<?php If Me.txtidsamad.Tag & "" = "" Then 'add new 'add data to table cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES (" & Me.txtid.Text & "," & Me.txtname.Text & ")" cmd.ExecuteReader() cnn.Close() MsgBox("Added Successfully") Else msgbox(ex.message) End If
End If
Working on Access Database (Office 2007)
Any Help ?
asked Friends in HF and google, Yahoo, Bing And everything!!
Thanks in advance.
"Insert Into db (name, id) Values('a', '1')"
Yeah, try putting single quotes before/after the double quotes before your variables. Like this:
cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"
(May 26, 2013 08:15 AM)Cody8295 Wrote: Yeah, try putting single quotes before/after the double quotes before your variables. Like this:
cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"
No that's a very bad habit. Just use parameters for inserting variables.
(May 26, 2013 07:15 AM)C3ol Wrote: "Insert Into db (name, id) Values('a', '1')"
Got the Same Error another solution ?..
Thanks in advance
(May 26, 2013 08:15 AM)Cody8295 Wrote: Yeah, try putting single quotes before/after the double quotes before your variables. Like this:
cmd.CommandText = "INSERT INTO table([iD], [name]) VALUES ('" & Me.txtid.Text & "','" & Me.txtname.Text & "')"
ExecuteNonQuery Error Same Error ..
Thanks For Helping anyways..🤨
(May 26, 2013 08:27 AM)x64 Wrote: No that's a very bad habit. Just use parameters for inserting variables.
Hey man can you help me with that..?
Thank you
(May 26, 2013 08:49 AM)scoder Wrote: Got the Same Error another solution ?..
Thanks in advance
ExecuteNonQuery Error Same Error ..
Thanks For Helping anyways..🤨
When using parameters you're a supposed to replace the word you've added.
So not :
Code cmdadd.Parameters.AddWithValue("ID", txtid.Text)
cmdadd.Parameters.AddWithValue("name", txtname.Text)
But
Code cmdadd.Parameters.AddWithValue("@ID", txtid.Text)
cmdadd.Parameters.AddWithValue("@name", txtname.Text)
(May 26, 2013 08:52 AM)x64 Wrote: When using parameters you're a supposed to replace the word you've added.
So not :
Code cmdadd.Parameters.AddWithValue("ID", txtid.Text)
cmdadd.Parameters.AddWithValue("name", txtname.Text)
But
Code cmdadd.Parameters.AddWithValue("@ID", txtid.Text)
cmdadd.Parameters.AddWithValue("@name", txtname.Text)
So the Code Will Be Like This :
PHP Code
<?php Dim database As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\test.accdb") Dim cmdadd As New OleDb.OleDbCommand cmdadd.CommandText = "INSERT INTO table (ID, name) VALUES (@ID, @name);" cmdadd.Connection = database cmdadd.Parameters.AddWithValue("@ID", txtid.Text) cmdadd.Parameters.AddWithValue("@name", txtname.Text) database.Open() cmdadd.ExecuteNonQuery() database.Close()
But Still Getting The Same Error.. :/
(May 26, 2013 08:27 AM)x64 Wrote: No that's a very bad habit. Just use parameters for inserting variables.
... This isn't PHP, you can't do it like that.
May 26, 2013 01:40 PM
(This post was last modified: May 26, 2013 01:43 PM by x64.)
(May 26, 2013 09:53 AM)Cody8295 Wrote: ... This isn't PHP, you can't do it like that.
I know it's not PHP. When using queries in ODB, you can use parameters for data. Look at my previous post
It's injection safe.
|