setfocus

Gannet

Registered User.
Local time
Today, 12:44
Joined
Sep 21, 2006
Messages
55
I'm having a problem with a public sub I created. I have this in a module called globals and I reference it on the afterupdate event for every state field I have. The problem that I've run into is you can't access any of the commands such as setfocus. Perhaps I'm going about this all wrong.
I want it to check the string for the state they have entered and if it doesn't find it then give the error message and setfocus back to field. Everything works fine until it gets to the setfocus.

Private Sub HC_q1_State_AfterUpdate()
check_states (HC_q1_State)
End Sub

Public Sub check_states(variable As String)
Dim states As String
Dim compare_val As Integer

If Not IsNull(variable) Then

states = "AL AK CO CT DE FL GA HI ID MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PW PA TX UT VT VI VA WA WI"
compare_val = InStr(1, states, variable, 1)

If compare_val = 0 Then
MsgBox "Not a valid state"
variable.SetFocus <--Here is where I run into an error
End If
End If
End Sub
 
You are trying to set focus to a string variable named variable. What you want to do is set focus to the control that holds the string.


Forms![FormName]![ControlName].SetFocus
 
OK,
I understand what you're saying but I'm at a total loss as to what I need to do. I tried incorporating the code but I seemed to have used it wrong. This is what I changed.


Private Sub HC_q1_State_AfterUpdate()
call check_states (HC_q1_State, HC_questionnaire) <--I'm getting a type mismatch here
End Sub

Public Sub check_states(variable As String, FormName as Form)
Dim states As String
Dim compare_val As Integer

If Not IsNull(variable) Then

states = "AL AK CO CT DE FL GA HI ID MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PW PA TX UT VT VI VA WA WI"
compare_val = InStr(1, states, variable, 1)

If compare_val = 0 Then
MsgBox "Not a valid state"
Forms![FormName]![variable].SetFocusEnd If
End If
End Sub
 
Code:
Public Sub check_states([COLOR="red"]ControlName[/COLOR] As String, FormName as [COLOR="Red"]String[/COLOR])
Dim states As String
Dim compare_val As Integer

If Not IsNull(variable) Then

states = "AL AK CO CT DE FL GA HI ID MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PW PA TX UT VT VI VA WA WI"
compare_val = InStr(1, states, variable, 1)

If compare_val = 0 Then
MsgBox "Not a valid state"
[COLOR="red"]Forms(FormName).Controls(ControlName).SetFocus[/COLOR]
End If 
End Sub

Make sure that when you call it you pass strings:

call check_states ("HC_q1_State", "HC_questionnaire")
 
Thank you

I added your changes and it worked perfectly.
 

Users who are viewing this thread

Back
Top Bottom