DoCmd.GotoControl not available (1 Viewer)

jmq

Registered User.
Local time
Yesterday, 20:36
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!
 

JHB

Have been here a while
Local time
Today, 05:36
Joined
Jun 17, 2012
Messages
7,732
Is "txtEmpNo" in the same form as the errorhandling?
Why not use Me.txtEmpNo.Setfocus?
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
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
 

JHB

Have been here a while
Local time
Today, 05:36
Joined
Jun 17, 2012
Messages
7,732
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.
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
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

  • FuncXTimeSheet.accdb
    992 KB · Views: 73

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
Why do i receive this message?

no docmd
no setfocus?:banghead:
 

Attachments

  • 2046.PNG
    2046.PNG
    12.5 KB · Views: 73

JHB

Have been here a while
Local time
Today, 05:36
Joined
Jun 17, 2012
Messages
7,732
Place the code in the BeforeUpdate and use Cancel=True to stay in the control.
Database attached.
 

Attachments

  • FuncXTimeSheet.zip
    45.4 KB · Views: 56

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
Last thing regarding txtEmpNo, I want to highlight the text when it return focus, :D

Thank you for that solution!
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
because using txtEmpNo.setFocus give me this error
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
Plus i cannot extract the data that i want with the date criteria.

Please check code/database i have uploaded

Thanks
 

JHB

Have been here a while
Local time
Today, 05:36
Joined
Jun 17, 2012
Messages
7,732
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!
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
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: 56

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:36
Joined
May 7, 2009
Messages
19,246
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
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
it returned to txtEmpNo but did not highlight the text inside it.. maybe access vba doesnt have such capability..

im starting to give up..
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:36
Joined
May 7, 2009
Messages
19,246
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
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
SOLVED BY:

Code:
ME.txtEmpNo.Undo

THANK YOU!
 

jmq

Registered User.
Local time
Yesterday, 20:36
Joined
Oct 4, 2017
Messages
87
I have another thread. please help

Time format Problem

Thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:36
Joined
Feb 19, 2002
Messages
43,484
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

Top Bottom