Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error 2110 when trying to either .SetFocus or GoToControl

Status
Not open for further replies.

waynea

Programmer
Jul 11, 2000
41
0
0
US
Hi,

I have an app that I'm working on that enters a survey questionnaire. There are a number of fields that must be filled out if certain answers are given in previous fields. For example, if the answer "15" is selected in any of the three fields 151, 152, or 153, then field 15-2 must contain some answer.

Due to the number of possible error fields, and the fact that the operator could click any other field on the form instead of just tabbing or entering to the next sequential field, I wanted to set up a generic error trap so no matter what field the operator clicks on, if a previous required field was left empty, they would get a warning, then the cursor would move directly to that field. I started like this:

Dim blSkipError As Boolean
Dim strSkipField As String
Dim ctlSkipField As Control


... in the General Declarations. Then, as the operator makes a choice in the select field, I did this:

Private Sub Ctl151_Exit(Cancel As Integer)

blSkipError = True
strSkipField = "15-2"
Set ctlSkipField = Me!Ctl15_2

Me.Ctl152.Enabled = True
Me.Ctl152.SetFocus

End Sub


....

And this works fine, advancing through the three combo boxes until you arrive at field 15-2 (ctl15_2, as the code interprets the field name.) If the operator enters anything in that field, then the error condition is turned off, the variables are cleared, and we continue.

....

Next, in the On Enter event procedure for each field, I want to check blSkipError. If it's turned on, the code calls the generic error handling routine:

Private Sub Ctl16_Enter()

If blSkipError = True Then
Call SkipErrorHandler
End If

End Sub


This also works fine. The error handler routine is called, and the code runs.

BUT:

Public Sub SkipErrorHandler()
Dim Response As String

Response = MsgBox("Comments required in " & strSkipField, vbOKOnly)
If Response = vbOK Then
DoCmd.GoToControl (ctlSkipField.Name)
'DoCmd.GoToControl (ctlSkipField)
'DoCmd.GoToControl (strSkipField)
'DoCmd.GoToControl ("15-2")
'Me.Ctl15_2.SetFocus
End If

End Sub


Any one of these lines throws an error 2110, "Microsoft Office Access can't move the focus to the control 15_2."

Stepping through the code shows me that whenever one of those lines runs, code execution goes back and fires the Ctl151_Exit procedure again, even though I've tabbed to three other fields by this time. This is where the error occurs. So I can stop the error by removing the line in that procedure that sets the focus to 152 - but, since that's the next field in order, I want focus to go there the first time through the procedure - what I don't want is for the procedure to execute a second time, and if I remove all of the GoToControl or SetFocus statements in the error handler routine, Ctl151_Exit will not fire a second time.

I know this is probably confusing (it certainly is to me,) but does anyone have an idea as to what's going on here?
 
Why not simply do ALL the validation checks in the Before_Update event procedure of the Form ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The BeforeUpdate event doesn't fire until I try to close the form. Since it's a survey form, and they'll be filling it in while talking to the subject on the phone, I need to stop the operator before they proceed to the next field, rather than waiting until the form is completed, then having them go back and enter.
 
I've tried to manipulate focus like that before, even using API calls, and all I did was end up crashing Access. The Exit() and Enter() events occur before the LostFocus() and GotFocus() events, respectively, so you can't try to force focus in that way. Try using your technique with the LostFocus() and GotFocus() events:
Code:
Private Sub Ctl151_LostFocus()
  blSkipError = True
  strSkipField = "15-2"
  Set ctlSkipField = Me![Ctl15-2]
End Sub

Private Sub Ctl161_GotFocus()
  If blSkipError = True Then
    Call SkipErrorHandler
  End If
End Sub

Public Sub SkipErrorHandler()
  MsgBox "Comments required in " & strSkipField, vbOKOnly
  ctlSkipField.SetFocus
End Sub

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Another thing you might do to enable the right controls is monitor the control's Change() event and check for positive text length. That way if the user backspaces or deletes what they enter, the destination control is disabled when the text length is 0.
Code:
Private Sub Ctl151_Change()
  Me![Ctl15-2].Enabled = (Len(Me!Ctl151.Text) > 0)
End Sub

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Thanks, I'll give that a shot. I can make the focus go where I want it by tinkering with things, but doubling back through the code a second time concerns me. Somewhere down the line, it's going to cause problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top