VB.NET insert statement

Chong

Well-known member
Joined
Apr 4, 2003
Messages
79
Will anyone show me how to insert data into a mdb file? Heres what I have and it doesnt 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
 
my dear you need to do this

Code:
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
 
Thank you very much for your quick and easy codes. Im greatly appreciate your help.

ljCharlie
 
Back
Top