Testing if record in form is available for editing

M

macroapa

HI, I have a form bound to a query so it returns just one record from
the table. My VBA code automatically assigns a value to one of the
fields on the form, but the code fails if someone else has already
opened it (I get the message Unable to assign a value to the object).

So how can I test if the record is available for editing before I try
to assigne the value to the field?

Thanks
 
K

Karl Hoaglund

HI, I have a form bound to a query so it returns just one record from
the table.  My VBA code automatically assigns a value to one of the
fields on the form, but the code fails if someone else has already
opened it (I get the message Unable to assign a value to the object).

So how can I test if the record is available for editing before I try
to assigne the value to the field?

Thanks

Hi. The best way I've been able to find to do what you want is to try
to update the record with code and trap an error that occurs. I put
this code behind a button, but you could do it on form open, or where
ever it is appropriate.

Private Sub Command1_Click()
Dim rs1 As Recordset
Set rs1 = Me.Recordset
On Error GoTo error_handler
rs1.Edit
rs1![Qty] = 0
rs1.Update
Exit Sub

error_handler:
If Err.Number = 3188 Then
'Handle the error here.
MsgBox "There's an error. . ."
Else
Err.Raise Err.Number
End If

End Sub


You can see that the code puts a reference to the form's recordset
into the variable rs1. It then calls the .Edit statement. At this
point, if the record is locked by someone else, you will get an error
(in my case error 3188). You can trap it and handle it appropriately,
like with your own error message followed by closing the form.

Hope this helps!

Karl Hoaglund
www.nexuscgi.net
 

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