Delete Rows In Access "VBExprees"  
Author Message
Shady9399





PostPosted: Visual Basic Express Edition, Delete Rows In Access "VBExprees" Top

Hi

How can I delete rows i Access database


My ListBox display values from DataBase, and when I select pretendet item in my listbox and then press the Button, that must delete the selected item with row from "Test.mdb".

Thank you




Visual Studio Express Editions22  
 
 
BortNE24





PostPosted: Visual Basic Express Edition, Delete Rows In Access "VBExprees" Top

Hey Shady,

Your ListBox should be data bound to your table, right Have it displaying one thing, then using the primary key for your Value Number (eg, Name for Display member, EntryID for Value member).

These lines of code might work (still kinda using the Name example from above). I'm still new to this too, so it will no doubt need a bit more work, plus it will need to be personalized for your database, but I hope it points you in the right direction.

Dim DeleteRow As DatabaseNameDataSet.TableNameRow

DeleteRow= Me.DatabaseNameDataSet.TableName.RemoveTableNameRow

Dim Name as Integer

Name = ListBox.ValueMember

' Any more random code here.

Me.DatabaseNameDataSet.TableName.Rows.Remove(Name)

Me.TableNameTableAdapter1.Update(DatabaseNameDataSet.TableName)

Good luck

Bort



 
 
Shady9399





PostPosted: Visual Basic Express Edition, Delete Rows In Access "VBExprees" Top

Your code require DadaBaseView or DataBaseSet.

If  yes it will not work for me. Because my listbox is directly connected to DataBase.

 

Module DisplayDataInform

Dim ds As New DataSet

Dim da As OleDbDataAdapter

Dim con As OleDbConnection

Dim sql As String

Sub DisplayTest1()

sql = "Select * From RegDB"

con = New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C:\Program Files\Common Files\Reg\RegDataBase.mdb;")

da = New OleDbDataAdapter(sql, con)

da.Fill(ds, "RegDB")

frmMain.ListBox1.DataSource = ds.Tables("RegDB")

frmMain.ListBox1.DisplayMember = "Field1"

End Sub

End Module



 
 
BortNE24





PostPosted: Visual Basic Express Edition, Delete Rows In Access "VBExprees" Top

Mine used a DataBaseSet, sorry. In that case, I'm afraid I can't help you

 
 
spotty





PostPosted: Visual Basic Express Edition, Delete Rows In Access "VBExprees" Top

Now I'm not sure about the exact syntax - I dont use Access that much but the general idea is here and I would definately advocate using parameters to pass information to a sql statement rather than simply building a string.

But general idea is you want to use ADO.NET to create a DB Command object which contains a connection to your database and a CommandText property set to the delete command you wish to run. Then with this you are going to open the connection - running the command object using executenonquery and then close the connection.

This approach doesnt require any datasets to be create or manipulated.

The general idea is correct but the syntax of the code may be a little off.

Dim cn As New System.Data.OleDb.OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C:\Program Files\Common Files\Reg\RegDataBase.mdb;")

Dim ocmdAboutVBCommand As New System.Data.OleDb.OleDbCommand
With ocmdAboutVBCommand
.Connection = cn
.CommandText = "Delete From Articles WHERE ID= " & FrmMain.Listbox1.selectedItem.Tostring
End With

cn.Open()
ocmdAboutVBCommand.ExecuteNonQuery()
cn.Close()