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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Can't set Focus" Message.

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
I have a form with an invisible subform that is used for entering search criteria. When the user clicks the "Enter Selection Criteris" button, the form is made visible. After there criteria is entered, the user has the choice of three buttons, "Select Records", "No Selection" (turns off filter, but leaves entries in place) and "Clear" (turns off filter and clears criteria fields)

The code for each of these buttons is:

Select records:

Private Sub EnterCmd_Click()
Dim retst as String
dim tststr as String
Me.EnterCmd.SetFocus
retst = ""
retst = FormatProcessParms(1)
If retst <> "I Open" Then
If retst <> ""
filterString = retst
FocusToTitle = 1
Else
FilterString = "Title LIKE '*'"
FocusToTitle = 1
End If
Forms!Books!FocusTgt.SetFocus
End If
End Sub

Note: FormatProcessParms function processes the values in the criteria selection fields and returns a filter string.

No Selection:

PrivateSub NoSelect_Click()
FocusToTitle = 2
Forms!Books!FocusTgt.SetFocus
End Sub

Clear:

Private Sub Clear_Click()
FormatProcessParms (2)
FocusToTitle = 3
Forms!Books!FocusTgt.SetFocus
End Sub

The FocusToTitle is a global variable that tells the Focus Target on the main form (an invisible unbound text box that only server as a target to receive the focus) which button was pushed.

FocusTgt:

Private Sub FocusTgt_GotFocus()
Select Case FocusToTitle
Case 1
Me.Filter = FilterString
Me.FilterOn = True
Case 2
Me.FilterOn = False
Case 3
FilterString = ""
Me.FilterOn = False
End Select
Me.CriteriaInput.Visible = False
FocusToTitle = 0
End Sub

My problem is this. When the main form is looking at the first record and criteria is entered and the Select Records button is pushed, all is well. If the main form is elsewhere, I get a message box telling me I can't set the focus to that target. It has an OK button.

If either of the other two buttons is pushed, I get a message box that says the same thing, but now there are Continue (grayed out), End, Debug, and Help buttons.

If I push the OK button, the focus goes exactly where it was told to go, to the FocusTgt text box. If the End button is pressed, the focus goes to the Title text box, which has the tab index of 1. (The FocusTgt text box is tab index 0 but tab stop is NO).

The proper selections are being made and the text box with the OK is must a major annoyance. However, the message box with the four buttons is a disaster waiting for the day that the user hits the Debug button.

I have played with SetWarnings and it doesn't seem to help.

Does anyone have any idea why this could be hapening?
 
Oops. It should be "If the main form is looking elsewhere" ie not set to the first record.
 
grnzbra,
I'm unclear as to which form is 'books', and what's the name of the subform, and on which form are the functions you've listed. Are the buttons on the subform or the main form?

In general--and I don't mean this to be overly critical--but when I find myself using globals to dictate what gets focus, and using setfocus all over, triggered by gotfocus events, etc, then I will rethink the process. There's a danger of recursion and also of the errors you see.

Using On Error Resume Next is what I will do anytime I use the SetFocus, or I will trap the 'cant set focus' error and Resume Next, because of all the diffuculty in knowing each situation when the setfocus is legal or illegal--especially when you're doing visible/invisible, enable/disable, etc.

During testing you can comment out the On Error Resume Next and decide if you need to recode something where you're doing setfocus when you don't mean to, but in general, a SetFocus is an error waiting to happen, and since it's typically a non-critical error, it's better to Resume Next than to annoy the user--he's got a mouse and he can do it manually in those situations if it can't possibly be recoded to do it automatically.
--Jim
 
Books is the main form. BookCriteria is the invisible subform. The Enter Selection Criteria button is on Books and makes the subform visible.

The other three buttons are on the subform. The Select Records button runs a Sub that goes all through the text boxes on the subform, looking for an with an entry. When it finds one, it includes it in a filter string. When it's done, the filter string is sent to the global variable, sends the focus back to the Title field on Books. The Got Focus event for the Title field then sets the filter property of Books and makes the subform invisible.

Personally, I would have preferred to use a dialog box to collect the criteria and create the filter string. Then all I would have to do is close the form. However, the boss insited on using a subform.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top