simple recordset question

N

NH

I want to write a simple fucntion which will delete a specified record from
a table. I have written this, but it halts at the rs.findfirst line saying
that the operation is not supported for this type of object.....I have tried
all sorts of syntax changes, but still get the same error. Can anyone point
out what I have done wrong?

===========================================
Public Function DeleteRecord(ID As Integer)
If IsMissing(ID) Then Exit Function

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("MyTable")

rs.FindFirst "[ID] = " & ID
If not rs.NoMatch Then
rs.Delete
rs.Update
End If
Set rs = Nothing

End Function
============================================

Thanks

Nick
 
M

Microsoft

Try to call something like this to replace the cursor at the begening of the
recordset:

rs.MoveFirst

regards,
//JF
 
M

Marshall Barton

NH said:
I want to write a simple fucntion which will delete a specified record from
a table. I have written this, but it halts at the rs.findfirst line saying
that the operation is not supported for this type of object.....I have tried
all sorts of syntax changes, but still get the same error. Can anyone point
out what I have done wrong?

===========================================
Public Function DeleteRecord(ID As Integer)
If IsMissing(ID) Then Exit Function

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("MyTable")

rs.FindFirst "[ID] = " & ID
If not rs.NoMatch Then
rs.Delete
rs.Update
End If
Set rs = Nothing

End Function
============================================


FindFirst does not apply to Table type recordsets, it
operates on Dynaset and SnapShot type recordsets.

Try this instead:
Set rs = db.OpenRecordset("MyTable", dbDynaset)
 
C

Chris Nebinger

You will find that the following function will work much
faster, especially on large tables.

Public Function DeleteRecord(ID As Integer)
CurrentDB.Execute "Delete from MyTable where ID=" &
ID
End Functioin

Also note, if ID is declared as an integer, it can not be
missing. There will have to be a value passed to it.


Chris Nebinger
 
N

NH

I have noted Marshall's message for future reference, but have decided to
use Chris' function.

Thanks everyone.
 
M

Marshall Barton

NH said:
I have noted Marshall's message for future reference, but have decided to
use Chris' function.


Wise decision ;-)

I only tried to explain why you got the error, Chris
provided an alternate approach that will perform the task
far more efficiently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top