Cannot close form from within a Gotfocus event

J

Jim Franklin

Hi,

I wonder if anyone can help me. I have a simple password entry form, with
two textboxes, one of which is formatted so it is effectively invisible (but
can receive the focus.) I also have one Cancel command button, which just
closes the form without taking any action.

The idea is that if the user inputs a password in textbox 1 and hits enter,
the focus moves to textbox 2 and the action code runs in textbox 2's
GotFocus event (code is below). If the user enters a password in textbox 1
but clicks Cancel, no action is taken.

My problem is that I get the error no 2585, "this action can't be carried
out while processing a form or report event" when I use the docmd.close
command to close the password form. Does anyone know why and how I can get
round this?

In case you are thinking "why would he not just have a command button to run
this code?" the answer is that the app is on a touchscreen with an onscreen
keyboard. Users are hitting the virtual Enter key at the end of their
password, and then having to either hit it again or click the command button
separately. I want the whole thing to happen whenever the password textbox
is updated, unless the Cancel button is clicked.

Thanks for reading and for any help anyone can provide!

Jim

Private Sub txtExit_GotFocus()
On Error GoTo MyERR

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

If IsNull(Me.PWInput) Then
Me.PWInput.SetFocus
MsgBox "Please enter a password!"
Exit Sub
End If

Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_Settings")
rs.MoveFirst

Select Case Me.FromForm
Case "frm_Cars"
If Me.PWInput = rs("PWLevel1") Then
DoCmd.OpenForm "frm_Customers"
Forms("frm_Customers")!Cust_ID = Forms(Me.FromForm)!Car_Cust_ID
Form_frm_Customers.PopulateCust
DoCmd.Close acForm, "frm_Password", acSaveNo
<===============ERROR OCCURS HERE
DoCmd.SelectObject acForm, "frm_Customers"
Else
Me.PWInput.SetFocus
MsgBox "The password you have entered is incorrect.", vbCritical
+ vbOKOnly, "Error"
End If
End Select

rs.Close
Set rs = Nothing
Set db = Nothing

MyEXIT:
Exit Sub

MyERR:
MsgBox Err.Number & " " & Err.Description
Resume MyEXIT
Resume

End Sub
 

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