Stopping tab to next cell when values fail input mask type test.

T

tdmailbox

I have a function that runs to test to make sure data in the tax_id
form cell fits inside one of two input masks. The fuction works fine,
if I put in bad data the msgbox tells me. However the
Me.tax_id.Setfocus doest work. If I put in bad data and tab out of
the cell my setfocus seems to run before the tab and I end up out of
the cell. How can I fix this? I tried Cancel=True as someone
mentioned but that doesnt fix it.


Private Sub tax_id_AfterUpdate()
If Me.tax_id Like "###-##-####" Or Me.tax_id Like "##-#######" Then
' it looks OK
Else

MsgBox "Format Must be ##-####### for " & _
"EIN or ###-##-#### for SSN"
Cancel = True
Me.tax_id.SetFocus

End If
End Sub
 
A

Andi Mayer

I have a function that runs to test to make sure data in the tax_id
form cell fits inside one of two input masks. The fuction works fine,
if I put in bad data the msgbox tells me. However the
Me.tax_id.Setfocus doest work. If I put in bad data and tab out of
the cell my setfocus seems to run before the tab and I end up out of
the cell. How can I fix this? I tried Cancel=True as someone
mentioned but that doesnt fix it.


Private Sub tax_id_AfterUpdate()
If Me.tax_id Like "###-##-####" Or Me.tax_id Like "##-#######" Then
' it looks OK
Else

MsgBox "Format Must be ##-####### for " & _
"EIN or ###-##-#### for SSN"
Cancel = True
Me.tax_id.SetFocus

End If
End Sub

you mixed something up:
the after-update can't be canceled, at this point there is no return,
the data gets written.

you have to use the bevorUpdate, there you can use the cancel.

BTW add at the beginning of the module option explicit
this helps in this case because the compiler would tell you that the
variable cancel is not assigned.
 
Top