Welcome to Dream.In.Code
Become a VB Expert!

Join 137,260 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,542 people online right now. Registration is fast and FREE... Join Now!




delete record

 
Reply to this topicStart new topic

delete record, help with deleting a record

jpamps
6 Dec, 2006 - 11:53 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 3


My Contributions
hi, i've been having trouble with deleting a record in a database. i'm very new to adodb, so i dont know much. welps.. this is what i have so far:

Private Sub cmdDelete_Click()

Dim MyConn As adodb.Connection
Dim rs As adodb.Recordset

Set MyConn = New adodb.Connection
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\List1.mdb;"
MyConn.Open

If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then 'check if you really want to delete this record
Exit Sub
Else

With MyConn
.BeginTrans
.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index)
.CommitTrans
End With

End If

rs.Close
Set rs = Nothing


how come this doesnt work? i get an error saying Syntax error in string query expression 'OrigTitle = 'whatever'. where 'whatever' is what the user selects in the first column of the ListView.

it then points to:
.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index)

so can anybody help me out with this? or give any other advise on how to delete a record? many thanks in advance for your help!
User is offlineProfile CardPM
+Quote Post

2much2young
RE: Delete Record
7 Dec, 2006 - 12:50 AM
Post #2

New D.I.C Head
*

Joined: 15 Nov, 2006
Posts: 14


My Contributions
Think you might need to add a bit at the end

.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index) & "'"
User is offlineProfile CardPM
+Quote Post

jpamps
RE: Delete Record
7 Dec, 2006 - 08:50 PM
Post #3

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 3


My Contributions
QUOTE(2much2young @ 7 Dec, 2006 - 01:50 AM) *

Think you might need to add a bit at the end

.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index) & "'"



YAY!!!! it works now!!! thanks a lot!!

This post has been edited by jpamps: 7 Dec, 2006 - 11:20 PM
User is offlineProfile CardPM
+Quote Post

arun1516
RE: Delete Record
8 Feb, 2007 - 04:18 AM
Post #4

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 5


My Contributions
QUOTE(jpamps @ 7 Dec, 2006 - 12:53 AM) *

hi, i've been having trouble with deleting a record in a database. i'm very new to adodb, so i dont know much. welps.. this is what i have so far:

Private Sub cmdDelete_Click()

Dim MyConn As adodb.Connection
Dim rs As adodb.Recordset

Set MyConn = New adodb.Connection
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\List1.mdb;"
MyConn.Open

If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion, "Delete?") = vbNo Then 'check if you really want to delete this record
Exit Sub
Else





With MyConn
.BeginTrans
.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index)
.CommitTrans
End With

End If

rs.Close
Set rs = Nothing


how come this doesnt work? i get an error saying Syntax error in string query expression 'OrigTitle = 'whatever'. where 'whatever' is what the user selects in the first column of the ListView.

it then points to:
.Execute "DELETE FROM List1 WHERE OrigTitle = '" & ListView1.ListItems.Item(ListView1.SelectedItem.Index)

so can anybody help me out with this? or give any other advise on how to delete a record? many thanks in advance for your help!





if you want to delete all the records from the database use this code to delete all the records from the database

conn.execute "delete from table name "
thats it ....
you can place a message box or anything you want to alert the user with




now if you want delete specific record from the database
here is the code

sql = " delete from table name where fields = '" & fieldname & "'"
( fields here is the database feildname and fieldname is the field on your form)
rs.open , sql , conn , openstatic , adlockpessimistic
with cmd
cmd is basically the command string we have defined in the module section
.activeconnection = conn
.commandtext = sql
.exexute
thats it .........

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 06:49PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month