I wh=ant to delete a record using this code but error is coming (1 Viewer)

sbaud2003

Member
Local time
Today, 19:41
Joined
Apr 5, 2020
Messages
178
Private Sub CMDEL_Click()


'On Error GoTo ErrorHandler

Dim sql As String
Dim rs As DAO.Recordset
Dim ActID As Long
ActID = Me.OccupantID.value
sql = "SELECT * FROM RoomOccupations WHERE ([OccupantID] = " & ActID & ""

Set rs = CurrentDb.OpenRecordset(sql)

With rs

If Not .BOF And Not .EOF Then

.MoveLast
.MoveFirst

If .Updatable Then


.Delete

End If
End If

.Close

End With

ExitSub:
Set rs = Nothing
'...and set it to nothing
Exit Sub
'ErrorHandler:
'Resume ExitSub
End Sub
 

ebs17

Well-known member
Local time
Today, 16:11
Joined
Feb 7, 2020
Messages
1,946
but error is coming
Which message for which line?
You should ask yourself this question first, if you can assume that the bug was code generated and not sent to you by God.

I myself would simply use a simple design and since certain errors cannot occur at all, which means that you do not have to deal with them:
Code:
Dim sql As String
sql = "DELETE FROM RoomOccupations WHERE OccupantID = " & Me.OccupantID.Value
CurrentDb.Execute sql, dbFailOnError
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:11
Joined
Sep 21, 2011
Messages
14,306
What is all this for?
Code:
.MoveLast
.MoveFirst

PS note the code tags. :(
Can't even say what the error is?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:11
Joined
Feb 28, 2001
Messages
27,187
Thanks a lot sir, my work is done

It is done this time, but for future reference - because in the world of software there is ALWAYS a next time:

Please remember that when you have an error to report, two or three things will help make our response much easier.

First, if there is an error, copy the error number and/or the text of the message.
Second, if the system offers you the chance to do a DEBUG, it will show you the line of code that is at fault. You can show us that line.
Third, tell us at least briefly what you INTENDED to do and what actually happened.
 

Users who are viewing this thread

Top Bottom