How to test if a record is locked

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

How to test if a record is locked

Post by Peter Kinross »

I am having record locked problems. I am trying to see if a record is locked before editing it, but I am having trouble ascertaining this. I think I got the idea of trying to edit a record to see if it was locked from this Lounge a while back.
The code I use is:

Code: Select all

Function IsRecordLocked(ThisID As Long) As Boolean
....
....
Set rst = CurrentDb.OpenRecordset("tblContacts", dbOpenDynaset)
With rst
    .FindFirst "[ID]=" & ThisID
     If Not .NoMatch Then
        .Edit: .Update
     Else       'Can't find this person so create error
        Err.Raise vbObjectError + 513
     End If
End With   'rst
If Err.Number = 0 Then
    IsRecordLocked = False
Else
    IsRecordLocked = True
End If
Most times this seems to work but occasionally it doesn't work - the .Edit and .Update don't generate an error if the record is locked. Hence IsRecordLocked returns false. I suspect that the times it works must be when the record isn't locked.
Is there a better way? If not how can I make this one work - without actually writing data.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to test if a record is locked

Post by HansV »

Do you have a line On Error Resume Next somewhere in the ... part?
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: How to test if a record is locked

Post by Peter Kinross »

How dumb can one get and still breathe!
Yep I sure did.
You do not have the required permissions to view the files attached to this post.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to test if a record is locked

Post by HansV »

Er, does this mean the problem is solved now, or does it still stand?
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: How to test if a record is locked

Post by Peter Kinross »

I'm pretty sure that will fix it.
I will know after a few uses. but I am going on hols so probably won't know for a couple of weeks.
Thanks again Hans.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to test if a record is locked

Post by HansV »

Enjoy your holidays! :smile:
Best wishes,
Hans