DoCmd.GotoControl not available

jmq

Registered User.
Local time
Today, 10:08
Joined
Oct 4, 2017
Messages
87
Code:
errHandler:
    If Err.Number = 3021 Then
        MsgBox "Invalid Employee Number!" & vbCrLf & _
        "Please check the number again.", vbOKOnly + vbInformation, "Message"
        
        DoCmd.GoToControl "txtEmpNo"
    Else
        MsgBox "A problem occurred when trying to retrieve the employee record.", _
                vbOKOnly + vbCritical, "Error"
    End If

Focus does not go to txtEmpNo

How do you do it?

Thanks!
 
Is "txtEmpNo" in the same form as the errorhandling?
Why not use Me.txtEmpNo.Setfocus?
 
Yes. Same form.

Code:
errHandler:
    If Err.Number = 3021 Then
        MsgBox "Invalid Employee Number!" & vbCrLf & _
        "Please check the number again.", vbOKOnly + vbInformation, "Message"
        
        'DoCmd.GoToControl "txtEmpNo"
        Me.txtEmpNo.SetFocus
    Else
        MsgBox "A problem occurred when trying to retrieve the employee record.", _
                vbOKOnly + vbCritical, "Error"
    End If

No Luck.. Still wont go to txtEmpNo
 
Could you post your database with some sample data, zip it because you haven't 10 post yet. And a description how to reproduce the problem.
 
The behaviour i wanna achieve when the txtEmpNo is invalid is to return back to txtEmpNo. not in txtName

Note:
The uploaded file is full of error :):p
 

Attachments

Why do i receive this message?

no docmd
no setfocus?:banghead:
 

Attachments

  • 2046.PNG
    2046.PNG
    12.5 KB · Views: 112
Place the code in the BeforeUpdate and use Cancel=True to stay in the control.
Database attached.
 

Attachments

Last thing regarding txtEmpNo, I want to highlight the text when it return focus, :D

Thank you for that solution!
 
because using txtEmpNo.setFocus give me this error
 
Plus i cannot extract the data that i want with the date criteria.

Please check code/database i have uploaded

Thanks
 
Plus i cannot extract the data that i want with the date criteria.
Sorry - but what exactly do you mean?
You can't set focus to a control when it already has the focus!
 
attached is what i mean..

just to highlight the text inputted after it set focus.. :(
 

Attachments

  • This is what i mean.PNG
    This is what i mean.PNG
    11.6 KB · Views: 91
use the BeforeUpdate Event of txtEmpNo textbox:

Code:
Private Sub txtEmpNo_BeforeUpdate(Cancel As Integer)
On Error GoTo errHandler
    
    If IsNull(txtEmpNo) Or IsEmpty(txtEmpNo) Then
        Exit Sub
    End If
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT FirstName, MiddleName, LastName " & _
                              "FROM tbl1Employees " & _
                              "WHERE EmpNo ='" & txtEmpNo & "';")
    
    If (rs.BOF And rs.EOF) Then
        
        MsgBox "Invalid Employee Number!" & vbCrLf & _
        "Please check the number again.", vbOKOnly + vbInformation, "Message"
        
        'DoCmd.GoToControl "txtEmpNo"
        'Me.txtEmpNo.SetFocus
        Cancel = True
        
    Else
        [txtName] = rs!FirstName & " " & Left(rs!MiddleName, 1) & ". " & rs!LastName
    End If
ExitSub:
    Set rs = Nothing
    Exit Sub
errHandler:
    MsgBox "A problem occurred when trying to retrieve the employee record.", _
            vbOKOnly + vbCritical, "Error"
    Cancel = True
    Resume ExitSub
End Sub
 
it returned to txtEmpNo but did not highlight the text inside it.. maybe access vba doesnt have such capability..

im starting to give up..
 
you add Me.txtEmpNo.Undo

Code:
Private Sub txtEmpNo_BeforeUpdate(Cancel As Integer)
On Error GoTo errHandler
    
    If IsNull(txtEmpNo) Or IsEmpty(txtEmpNo) Then
        Exit Sub
    End If
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT FirstName, MiddleName, LastName " & _
                              "FROM tbl1Employees " & _
                              "WHERE EmpNo ='" & txtEmpNo & "';")
    
    If (rs.BOF And rs.EOF) Then
        
        MsgBox "Invalid Employee Number!" & vbCrLf & _
        "Please check the number again.", vbOKOnly + vbInformation, "Message"
        
        'DoCmd.GoToControl "txtEmpNo"
        'Me.txtEmpNo.SetFocus

	ME.txtEmpNo.Undo
        Cancel = True
        
    Else
        [txtName] = rs!FirstName & " " & Left(rs!MiddleName, 1) & ". " & rs!LastName
    End If
ExitSub:
    Set rs = Nothing
    Exit Sub
errHandler:
    MsgBox "A problem occurred when trying to retrieve the employee record.", _
            vbOKOnly + vbCritical, "Error"
    Cancel = True
    Resume ExitSub
End Sub
 
SOLVED BY:

Code:
ME.txtEmpNo.Undo

THANK YOU!
 
I have another thread. please help

Time format Problem

Thanks
 
Please don't ask the same question in multiple threads or if you do, at lease mention that you have done it and post a link to the duplicate question.
 

Users who are viewing this thread

Back
Top Bottom