Hi Sir, Kimangel is here again! Gud Day!

How would I code that a cursor will automatically return to its place where it was encountered error? unless resolved.

Below is my code;

Private Sub txtContact_No1_LostFocus()

On Error Resume Next

If txtContact_No1.Text = "" Then
MsgBox "Fill up the field to avoid error.", vbOKOnly + vbInformation, "Employees"
Exit Sub

ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then
MsgBox "You must enter Numeric values.", vbOKOnly, "Employees"
Exit Sub

End If
End Sub

With the kind of errors you're showing, you'd be much better off with a masked edit control. That sort of thing is handled automatically.

If you insist on using a textbox, try putting txtContact_No1.SetFocus just before each Exit Sub.

Do the following...

Private Sub txtContact_No1_LostFocus()
On Error Resume Next
''This is bad coding, rather do proper error trapping (my 2cents worth :))

If txtContact_No1.Text = "" Then
    MsgBox "Fill up the field to avoid error.", vbOKOnly + vbInformation, "Employees"

    txtContact_No1.SetFocus

    Exit Sub
ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then
    MsgBox "You must enter Numeric values.", vbOKOnly, "Employees"

    txtContact_No1.SetFocus
    ''Note that textbox MUST be visible and must be enabled...

    Exit Sub
End If
End Sub

To do error trapping...

Private Sub txtContact_No1_LostFocus()
On Error GoTo ContactErr
''This is bad coding, rather do proper error trapping (my 2cents worth :))

If txtContact_No1.Text = "" Then
    MsgBox "Fill up the field to avoid error.", vbOKOnly + vbInformation, "Employees"

    txtContact_No1.SetFocus

    Exit Sub
ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then
    MsgBox "You must enter Numeric values.", vbOKOnly, "Employees"

    txtContact_No1.SetFocus
    ''Note that textbox MUST be visible and must be enabled...

    Exit Sub
End If

''Must exit sub to start reading your error code part...
Exit Sub

ContactErr

MsgBox "You have encountered an error in your Contact No.1 Box."
End Sub

from the above posts , given by an expert , you question has been answered

i've read this article and found why Kimangel using On Error Statement, in this case there is no need to use On Error Statement.

Now i come to the topic and use the following ( Already Given By The Expert ) code on the lostfocus event of txtContact_No1.

If txtContact_No1.Text = "" Then
MsgBox "Fill up the field to avoid error.", vbOKOnly + vbInformation, "Employees"
txtContact_No1.SetFocus
Exit Sub
ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then
MsgBox "You must enter Numeric values.", vbOKOnly, "Employees"
txtContact_No1.SetFocus
Exit Sub
End If

What you're thinking is done by setfocus method
hope this helps you . . .

commented: :) Thank you for the expert part... +13

thanks a lot sirs for this info, mask edit control in new to me, im interested to use that on my SSS number textbox and lots more. I have a lot changes to make, my system will soon be scheduled for defense in school. I will revert back to you sir if question will arise. Once again thank you very much for the info.

Only a pleasure, please mark as solved. thanx...

Thank you sir. I used the code below but when I type alpanumeric on contact_No1 textbox, just to test the error trapping, these error will show up first the "You must enter Numeric value." and right after "Fill up the field to avoid error.". Pls help me onthis. tnx.

Private Sub txtContact_No1_LostFocus()

If txtContact_No1.Text = "" Then
MsgBox "Fill up the field to avoid error.", vbOKOnly + vbInformation, "Employees"
txtContact_No1.SetFocus
Exit Sub

ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then
MsgBox "You must enter Numeric value.", vbOKOnly, "Employees"
txtContact_No1.SetFocus
Exit Sub
End If

End Sub

Remove both message line codes, only use the error trap code...

Private Sub txtContact_No1_LostFocus()
On Error GoTo ContactErr
''This is bad coding, rather do proper error trapping (my 2cents worth :))
If txtContact_No1.Text = "" Then

    txtContact_No1.SetFocus

    GoTo ContactErr
    Exit Sub
ElseIf Not IsNumeric(txtContact_No1.Text) Or Val(txtContact_No1.Text) <= 0 Then

    txtContact_No1.SetFocus

    GoTo ContactErr
    Exit Sub
End If
''Must exit sub to start reading your error code part...
Exit Sub
ContactErr
MsgBox "You have encountered an error in your Contact No.1 Box."
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.