cypress1976
Member
- Joined
- Aug 18, 2009
- Messages
- 7
- Programming Experience
- 3-5
I have a windows form which connects to multiple database tables. On my form, I have a combobox. I want to be able to hit the delete button and remove the row from the access database which corresponds with the selected combobox value. I've tried many variations of this in my code but am continually running into problems. Here is my relevant code thus far:
I have declared these items at that top of my code.
Then here is the code for my delete button
I have tried other ways of deleting the row based on the combobox selection but none seem to work. I have performed code like this in the past and am not sure what I am missing. Thank you.
I have declared these items at that top of my code.
VB.NET:
Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Dim TheDatabase As String = "\Certifications.accdb;Persist Security Info=True"
Dim MyDocumentsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim FullDatabasePath As String = MyDocumentsFolder & TheDatabase
Dim strConnection As String = dbProvider & FullDatabasePath
Dim con As New OleDb.OleDbConnection(strConnection)
'string to load All Employees into combobox
Dim strAllEmployees As String = "SELECT DISTINCT OFFICERS.LName + ', ' + OFFICERS.FName + ' - ' + OFFICERS.DSN as FullName from OFFICERS"
Then here is the code for my delete button
VB.NET:
con.Open()
Dim dataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim command As OleDb.OleDbCommand
Dim parameter As OleDb.OleDbParameter
'this takes the value from the combobox and pulls out only the DSN field
Dim strcboEmployeeSelectedValue As String = cboEmployee.SelectedValue.ToString
Dim strNewValue As String = strcboEmployeeSelectedValue.Split("- ").Last()
' Your DELETE command.
command = New OleDb.OleDbCommand("DELETE * FROM OFFICERS WHERE DSN = ?", con)
parameter = command.Parameters.AddWithValue("?", strNewValue)
parameter.SourceVersion = DataRowVersion.Original
dataAdapter.DeleteCommand = command
command.ExecuteNonQuery()
I have tried other ways of deleting the row based on the combobox selection but none seem to work. I have performed code like this in the past and am not sure what I am missing. Thank you.
Last edited: