请在模块中的代码下方插入此代码。
导入 System.Data.OleDb 模块 Module1
Public OleCn As New OleDbConnection()
Public Function StrConnection() As String
StrConnection = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Testing.Accdb;"
Return StrConnection
End Function
在此之后在保存按钮中插入下面的编码。
Dim msg As DialogResult = MessageBox.Show("你想保存这条记录吗?", "Response", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (msg = vbYes) Then
If RequiredEntry() = True Then
Return
End If
Try
With OleCn
If .State <> ConnectionState.Open Then
.ConnectionString = StrConnection()
.Open()
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Try
Dim sSQL As String = "insert into Vendor values(@VendorCode,@VendorName,@Address,@City,@LandPhone,@Mobile,@EmailID,@Balance)"
Dim cmd As OleDbCommand = New OleDbCommand(sSQL, OleCn)
'VendorCode
Dim VendorCode As OleDbParameter = New OleDbParameter("@VendorCode", OleDbType.VarChar, 10)
VendorCode.Value = txtVendorCode.Text.ToString()
cmd.Parameters.Add(VendorCode)
'VendorName
Dim VendorName As OleDbParameter = New OleDbParameter("@VendorName", OleDbType.VarChar, 25)
VendorName.Value = txtVendorName.Text.ToString()
cmd.Parameters.Add(VendorName)
'Address
Dim Address As OleDbParameter = New OleDbParameter("@Address", OleDbType.VarChar, 50)
Address.Value = txtAddress.Text.ToString()
cmd.Parameters.Add(Address)
'City
Dim City As OleDbParameter = New OleDbParameter("@City", OleDbType.VarChar, 25)
City.Value = txtCity.Text.ToString()
cmd.Parameters.Add(City)
'LandPhone
Dim LandPhone As OleDbParameter = New OleDbParameter("@LandPhone", OleDbType.VarChar, 50)
LandPhone.Value = txtLandPhone.Text.ToString()
cmd.Parameters.Add(LandPhone)
'Mobile
Dim Mobile As OleDbParameter = New OleDbParameter("@Mobile", OleDbType.VarChar, 15)
Mobile.Value = txtMobile.Text.ToString()
cmd.Parameters.Add(Mobile)
'EmailID
Dim EmailID As OleDbParameter = New OleDbParameter("@EmailID", OleDbType.VarWChar, 25)
EmailID.Value = txtEmailID.Text.ToString()
cmd.Parameters.Add(EmailID)
'Balance
Dim Balance As OleDbParameter = New OleDbParameter("@Balance", OleDbType.VarWChar, 10)
Balance.Value = txtBalance.Text.ToString()
cmd.Parameters.Add(Balance)
If cmd.ExecuteNonQuery() Then
OleCn.Close()
MessageBox.Show("New Record is Added Successfully.", "Record Saved")
Call clear()
Else
MsgBox("Record Addition Failed ", MsgBoxStyle.Critical, "Addition Failed")
Return
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Data Error")
Exit Sub
End Try
End If