Hello,
I am amateur with VB database usage.
I've written a little database that keeps track of names, address, phone numbers etc. It displays the data in a DataGrid and stores them in a database. I am able to store and UPDATE new records without problems. Because of my ignorance of VB databases usage, I've tried for hours to get it to DELETE a record. I'm to the point I need to ask for expert help. Here is my code, which is based off of some code I found on the Internet:
I would greatly appreciate any help.
Thanks,
John - Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
-
-
myOleDbConnection = New _
-
OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
-
"user id=admin; Data Source=" & myDataSource)
-
-
Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
-
OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
-
-
OleDbUpdateCommand.CommandText = _
-
"Delete from Table1 WHERE id=" & KeyID & " ; "
-
-
OleDbUpdateCommand.Connection = myOleDbConnection
-
-
myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
-
-
myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
-
-
End Sub
5 4648
I think I got it working now. I added this line and it now appears to work.
myOleDbConnection.Open()
so now the code looks like this: - Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
-
-
myOleDbConnection = New _
-
OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
-
"user id=admin; Data Source=" & myDataSource)
-
-
myOleDbConnection.Open()
-
Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
-
OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
-
-
OleDbUpdateCommand.CommandText = _
-
"Delete from Table1 WHERE id=" & KeyID & " ; "
-
OleDbUpdateCommand.Connection = myOleDbConnection
-
-
myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
-
-
myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
-
-
End Sub
-
Hope this helps someone.
John
I've written a little database that keeps track of names ...
Hello,
As you've said you have a datagrid where all record your from database are displayed, right? and Let me Guess, you have also a Command button, A DELETE Button for Deleting? if that so, we can continue.
Heres the step for deleting:
1st, Select a record from datagrid then Click Command Button Delete.
Now On your Datagrid, RowColChange Event you can Have - Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
-
-
KeyID = DataGrid1.Columns(1).Value' If your Unique Key Is found in Column 0 then Used DataGrid1.Columns(0).Value
-
-
End Sub
2nd, On your Command Button "DELETE" Click Event, you can have - Public Sub CommandDelete_TableRow()
-
-
Set myOleDbConnection = New ADODB.Connection
-
set myOleDbConnection=Nothing
-
-
'Establish Connection.
-
OleDbConnection.Open "provider=microsoft.jet.oledb.4.0;user id=admin; Data Source=" & myDataSource""
-
-
'Delete Record Corresponding to the Selected Data on your datagrid.
-
myOleDbConnection.Execute "Delete from Table1 WHERE id=" & KeyID & "
-
-
End sub
Regards
Ervin
P.S This is the way I delete records, I don't force you to use this.
Hello,
As you've said you have a datagrid where all record your from database are displayed, right? and Let me Guess, you have also a Command button, A DELETE Button for Deleting? if that so, we can continue.
Heres the step for deleting:
1st, Select a record from datagrid then Click Command Button Delete.
Now On your Datagrid, RowColChange Event you can Have
...
Ervin,
Yes I have a datagrid that displays all database records and I have a Delete button.
Thanks for you help - you're code is more concise and therefore better than what I have.
John
... - Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
-
-
KeyID = DataGrid1.Columns(1).Value' If your Unique Key Is found in Column 0 then Used DataGrid1.Columns(0).Value
-
-
End Sub
2nd, On your Command Button "DELETE" Click Event, you can have - Public Sub CommandDelete_TableRow()
-
-
Set myOleDbConnection = New ADODB.Connection
-
set myOleDbConnection=Nothing
-
-
'Establish Connection.
-
OleDbConnection.Open "provider=microsoft.jet.oledb.4.0;user id=admin; Data Source=" & myDataSource""
-
-
'Delete Record Corresponding to the Selected Data on your datagrid.
-
myOleDbConnection.Execute "Delete from Table1 WHERE id=" & KeyID & "
-
-
End sub
Ervin,
For:
myOleDbConnection = New ADODB.Connection
I'm getting this error:
"Value of type 'ADODB.ConnectionClass' cannot be converted to 'System.Data.OleDb.OleDbConnection'."
John
Hello,
I am amateur with VB database usage.
I've written a little database that keeps track of names, address, phone numbers etc. It displays the data in a DataGrid and stores them in a database. I am able to store and UPDATE new records without problems. Because of my ignorance of VB databases usage, I've tried for hours to get it to DELETE a record. I'm to the point I need to ask for expert help. Here is my code, which is based off of some code I found on the Internet:
I would greatly appreciate any help.
Thanks,
John - Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
-
-
myOleDbConnection = New _
-
OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
-
"user id=admin; Data Source=" & myDataSource)
-
-
Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
-
OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
-
-
OleDbUpdateCommand.CommandText = _
-
"Delete from Table1 WHERE id=" & KeyID & " ; "
-
-
OleDbUpdateCommand.Connection = myOleDbConnection
-
-
myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
-
-
myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
-
-
End Sub
Ok, thats why ive said that i dont force you to use my coding.
Regards
Ervin
Sign in to post your reply or Sign up for a free account.
Similar topics
by: authorking |
last post by:
I use the following code to insert a data record in to a datatable of an
access database.But every time I execute the command, there will rise an
exception and the insert operation can't be...
|
by: Tinius |
last post by:
I am trying to update a field of type IMAGE.
I am able to do this by deleting the record and then INSERTing a new record
as shown below in the commented section.
But I really want to do this...
|
by: Phi |
last post by:
Hi,
I hope somebody could help me with this problem.
I would like to make a form to add and delete records from my ms access
database. I've found most of the codes from the internet and...
|
by: |
last post by:
I have been trying to delete a record with the following code, but not
receiving any results, not even an error message. I have verified that I am
passing a valid record index and that my...
|
by: Bryan |
last post by:
hello all...
im trying to add a record to an sql db on ms sql server 2000, using vb.net.
seems to be working.. except for one thing, one of the columns in the
database is a bit datatype, and...
|
by: Stanav |
last post by:
Hello all,
When a user click on the "Update" button on my program, I want to update the
database using the sub below. I was able to insert and modify records, but
can't delete records. The error...
|
by: Khamal |
last post by:
What is wrong with this
public bool syncTOmysqlcon(string Query)
{
try
{string strConnA = "Database=felcradb;Data Source=development;User
Id=root;Password=citranet";
MySqlConnection...
|
by: csharpa |
last post by:
I have 2 tables Patient(parent) , Battery(Child). when i gave input to it, in parent table the new record will be added but in child table the new record will not be added although I'm sending the FK...
|
by: fniles |
last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection).
I read using DataAdapter and DataSet, not DataReader.
When many people try to access the database at the same time, I get...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |