i had problem in inserting data into a table in access database.i am using visual studio2005 for the projects.here is my code

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" _
       & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "
        Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConnection)
Dim cmd As New OleDb.OleDbCommand
        With cmd
            .CommandText = "INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & ")"
            .Connection = cn
            .Connection.Open()
            .ExecuteNonQuery()
            .Connection.Close()
        End With

Recommended Answers

All 5 Replies

why are you writing so much of cmd it's simple for adding the data in access database see the code

Imports SYstem.Oledb

dim cn as OledbConnection
Dim cmd As OleDbCommand
dim sqlstr as string,ans as Boolean

private sub btn_click

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "

cn  = New OleDbConnection(strConnection)
cn.open()
sqlstr=""INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"

''trim use for removing space
cmd=new OleDbCommand(sqlstr,cn)
ans=cmd.ExequteNonquery()
if (ans=true) then
''message added
else
'' error
End if
cmd=nothing  ''Disposing the object from memory
cn.close

end sub

try it will work

why are you writing so much of cmd it's simple for adding the data in access database see the code

Imports SYstem.Oledb

dim cn as OledbConnection
Dim cmd As OleDbCommand
dim sqlstr as string,ans as Boolean

private sub btn_click

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "

cn  = New OleDbConnection(strConnection)
cn.open()
sqlstr=""INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"

''trim use for removing space
cmd=new OleDbCommand(sqlstr,cn)
ans=cmd.ExequteNonquery()
if (ans=true) then
''message added
else
'' error
End if
cmd=nothing  ''Disposing the object from memory
cn.close

end sub

try it will work

sorry just mistak happen write like this

sqlstr="INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"

Hi Guys im having some trouble using this code im having a few errors on it
Its about the undeclared variables, end of statement expected, name not declared
how do I fix those things

if you can write the code and where you got the error just mark then i'lll help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.