Chong Posted July 29, 2003 Posted July 29, 2003 Will anyone show me how to insert data into a mdb file? Here's what I have and it doesn't work. Private Shared mdbPath As String = Application.StartupPath & "\Computers.mdb" Public Shared Sub dataInsert(ByVal txtPserial As TextBox, ByVal txtCpName As TextBox, ByVal txtVersion As TextBox, ByVal txtTime As TextBox, ByVal txtDate As TextBox, ByVal txtWhat As TextBox, ByVal txtAddtnInfo As RichTextBox) Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & mdbPath Dim strPserial, strName, strVersion, strTime, strDate, strWhat, strAddtnInfo As String Dim cn As New OleDbConnection(strConn) cn.Open() Dim cmd As OleDbCommand = cn.CreateCommand() strPserial = txtPserial.Text strName = txtCpName.Text strVersion = txtVersion.Text strTime = txtTime.Text strDate = txtDate.Text strWhat = txtWhat.Text strAddtnInfo = txtAddtnInfo.Text cmd.CommandText = "INSERT INTO Program (PSerial, Name, Version, Time, Date, What, Addtn_Info) VALUES ('" & strVersion & "', '" & strName & "', '" & strVersion & "', '" & strTime & "', '" & strDate & "', '" & strWhat & "', '" & strAddtnInfo & "')" Dim intRecordAffected As Integer = cmd.ExecuteNonQuery() If intRecordAffected = 1 Then MsgBox("Insert Successful") End If End Sub Any suggestion is greatly appreciated! ljCharlie Quote
Madz Posted July 30, 2003 Posted July 30, 2003 my dear you need to do this Dim mycn as OleDBConnection = new OleDBConnection(Connection String) Dim mycmd as OleDbCommand = new OleDbCommand mycmd.Connection = mycn mycmd.CommandText = "Insert Into Table (Column1,Column2,Column3) Values ('" & Value1 & "','" & Value2 & "','" & value3 "')") mycn.open ' For UPDATE, INSERT, and DELETE statements, the return value of ExecuteNonQuery is the number of rows affected by the command. For all other types of statements, the return value is -1. dim intI as integer = mycmd.ExecuteNonQuery if intI <> -1 MsgBox ("Data Inserted Successfully") end if mycn.Close Quote The one and only Dr. Madz eee-m@il
Chong Posted July 30, 2003 Author Posted July 30, 2003 Thank you very much for your quick and easy codes. I'm greatly appreciate your help. ljCharlie Quote
mrdutchie Posted August 6, 2003 Posted August 6, 2003 (edited) nm forgot a line Edited August 6, 2003 by mrdutchie Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.