473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Record Delete - ExecuteNonQuery requires an open and available Connection

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



Expand|Select|Wrap|Line Numbers
  1. Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
  2.  
  3.   myOleDbConnection = New _
  4.   OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
  5.   "user id=admin; Data Source=" & myDataSource)
  6.  
  7.   Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
  8.   OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
  9.  
  10.   OleDbUpdateCommand.CommandText = _
  11.   "Delete from Table1 WHERE id=" & KeyID & " ; "
  12.  
  13.   OleDbUpdateCommand.Connection = myOleDbConnection
  14.  
  15.   myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
  16.  
  17.   myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
  18.  
  19. End Sub
Oct 6 '07 #1
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:

Expand|Select|Wrap|Line Numbers
  1.      Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
  2.  
  3.         myOleDbConnection = New _
  4.                OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
  5.                 "user id=admin; Data Source=" & myDataSource)
  6.  
  7.         myOleDbConnection.Open() 
  8.         Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
  9.         OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
  10.  
  11.         OleDbUpdateCommand.CommandText = _
  12.                 "Delete from Table1 WHERE id=" & KeyID & " ;  "
  13.         OleDbUpdateCommand.Connection = myOleDbConnection
  14.  
  15.         myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
  16.  
  17.         myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
  18.  
  19.     End Sub
  20.  
Hope this helps someone.

John
Oct 6 '07 #2
jrtox
89
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
  2.  
  3. KeyID = DataGrid1.Columns(1).Value' If your Unique Key Is found in Column 0 then Used DataGrid1.Columns(0).Value 
  4.  
  5. End Sub

2nd, On your Command Button "DELETE" Click Event, you can have

Expand|Select|Wrap|Line Numbers
  1. Public Sub CommandDelete_TableRow()
  2.  
  3. Set myOleDbConnection = New ADODB.Connection
  4. set myOleDbConnection=Nothing
  5.  
  6. 'Establish Connection.
  7.  OleDbConnection.Open "provider=microsoft.jet.oledb.4.0;user id=admin; Data Source=" & myDataSource""
  8.  
  9. 'Delete Record Corresponding to the Selected Data on your datagrid.
  10. myOleDbConnection.Execute "Delete from Table1 WHERE id=" & KeyID & "
  11.  
  12. End sub

Regards
Ervin

P.S This is the way I delete records, I don't force you to use this.
Oct 7 '07 #3
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
Oct 7 '07 #4
...
Expand|Select|Wrap|Line Numbers
  1. Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
  2.  
  3. KeyID = DataGrid1.Columns(1).Value' If your Unique Key Is found in Column 0 then Used DataGrid1.Columns(0).Value 
  4.  
  5. End Sub

2nd, On your Command Button "DELETE" Click Event, you can have

Expand|Select|Wrap|Line Numbers
  1. Public Sub CommandDelete_TableRow()
  2.  
  3. Set myOleDbConnection = New ADODB.Connection
  4. set myOleDbConnection=Nothing
  5.  
  6. 'Establish Connection.
  7.  OleDbConnection.Open "provider=microsoft.jet.oledb.4.0;user id=admin; Data Source=" & myDataSource""
  8.  
  9. 'Delete Record Corresponding to the Selected Data on your datagrid.
  10. myOleDbConnection.Execute "Delete from Table1 WHERE id=" & KeyID & "
  11.  
  12. 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
Oct 7 '07 #5
jrtox
89
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



Expand|Select|Wrap|Line Numbers
  1. Public Sub Delete_TableRow(ByVal RowNumber As Integer, ByVal KeyID As Integer)
  2.  
  3.   myOleDbConnection = New _
  4.   OleDbConnection("provider=microsoft.jet.oledb.4.0;" & _
  5.   "user id=admin; Data Source=" & myDataSource)
  6.  
  7.   Dim OleDbUpdateCommand As System.Data.OleDb.OleDbCommand
  8.   OleDbUpdateCommand = New System.Data.OleDb.OleDbCommand
  9.  
  10.   OleDbUpdateCommand.CommandText = _
  11.   "Delete from Table1 WHERE id=" & KeyID & " ; "
  12.  
  13.   OleDbUpdateCommand.Connection = myOleDbConnection
  14.  
  15.   myOleDbDataAdapter.UpdateCommand = OleDbUpdateCommand
  16.  
  17.   myOleDbDataAdapter.UpdateCommand.ExecuteNonQuery()
  18.  
  19. End Sub
Ok, thats why ive said that i dont force you to use my coding.

Regards
Ervin
Oct 7 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
2
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...
3
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...
2
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...
20
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...
4
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...
5
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...
1
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...
7
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...
0
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,...
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.