SetFocus Error from Function

Sorrells

Registered User.
Local time
Today, 20:23
Joined
Jan 13, 2001
Messages
258
I have a form for entering a person's address information and it has a few required fields that I check in a VBA function behind a command button.

If a required field is not completed, I send a message then SetFocus to the control.

The problem is no matter what control I SetFocus to in the function, Access says it "can't move the focus to the control".

The function is in a class module and is declared as Private. I have also attempted Public with the same dismal results.

I have no problem setting focus to the controls from another control. Thus I am thinking that the action coming from within the function has a bearing on the problem.

I could supply code but as you know, that is very straightforward. Have any of you encountered this problem and can you advise me as how to resolve it?
 
Rich,

Here is the function: The text boxes do exist, are visible and enabled.

============================================
Private Function Client_InfoOK() As Integer
' CHECK FOR REQUIRED FIELDS
'
'
Client_InfoOK = True
ClientInfoOK_Flag = False
If IsNull(txtFirst_Name) Then
MsgBox ("You must enter the Client's First name")
Forms!frmClient_Info_New!txtFax.SetFocus
txtLast_Name.SetFocus
Client_InfoOK = False
ElseIf IsNull(txtLast_Name) Then
MsgBox ("You must enter the Client's Last name")
txtFax.SetFocus
txtLast_Name.SetFocus
Client_InfoOK = False
ElseIf IsNull(txtHome_Phone) Then
MsgBox ("You must enter the HOME PHONE NUMBER of the client.")
txtFax.SetFocus
txtHome_Phone.SetFocus
Client_InfoOK = False
ElseIf IsNull(txtAddress1) Then
MsgBox ("You must enter the STREET ADDRESS of the client.")
txtFax.SetFocus
txtAddress1.SetFocus
Client_InfoOK = False
ElseIf IsNull(txtCity) Then
MsgBox ("You must enter the CITY of the client's home.")
txtFax.SetFocus
txtCity.SetFocus
Client_InfoOK = False
ElseIf IsNull(txtState) Then
MsgBox ("You must enter the STATE of the client's home.")
txtFax.SetFocus
txtState.SetFocus
Client_InfoOK = False
Else
ClientInfoOK_Flag = True 'class module level flag
End If

End Function
 
I found the problem but not yet the solution.

The SetFocus commands in the function are called from the last command button on the form. To make sure that a new record is not opened, I have on the LostFocus event of this command button a SetFocus to the first control of the form.

This LostFocus SetFocus is in direct conflict with any SetFocus in the called function.

What I think I will try is to remove the command button's tabstop and make the previous control tab to the first control. That might work.
 
I know users can't be trusted, but aren't you being a little over zealous?
I prefer to use a simple function and the tag property to tell users they haven't completed all the fields, the function simply set the focus to the relevant field
 
What about using a validation rule?

In the table (or form) for each field you do not want Null, Key Is Not Null in the Validation Rule field. You should also give them a reason in the Validation Text field which will give the user a message box if the field is Null when they try to save the record.

Also, in your code you are setting the focus to more than one object but the last set focus command is the one that the user will see.

HTH
 
Rich & gHudson,

Thanks for your comments! I never thought of being over zealous. Perhaps I am but I think what you see is a style I just sort of stepped into it as a single catchall function when there are several exits from the form.
I know more about the Tag Property but that still is very little. I see from the HELP how it can be used in this manner.
gHudson's comment about the 2 set-focuses deserves a reply. It is a habit that I need to get out of. When I started VBA I would accost the user right away about required fields OnLostFocus but I could not SetFocus on the control that already had focus. So I'd place focus on another control then back to the one missing the required data. When I am not thinking too hard, I slip back into that. You are very right, the first SetFocus in each phase of the IF statement is not needed.
I like the thought of the Validation Rule and text also. I have never used that. If this stops the writing of the record until all "Is Not Nulls" are completed, this sounds like the most efficient solution.
I truly appreciate you guys taking the time to help me with this. I have gained in knowledge!
 

Users who are viewing this thread

Back
Top Bottom