VBA code command not setting focus on a specific field (1 Viewer)

Alhakeem1977

Registered User.
Local time
Today, 21:19
Joined
Jun 24, 2017
Messages
308
Hi All,
I've got a VBA code on a field called [ActualAC] it should trigger in a specific condition but the issue is, when it triggers it returns an error message and then the cursor should set focus on another field called [AccountNo], but it not setting the focus.

How can I overcome this issue? Please find below my code:

Code:
Private Sub ActualAC_BeforeUpdate(Cancel As Integer)
On Error Resume Next
Dim strLinkCriteria As String
    strLinkCriteria = "[CustTypeID] = " & Me!CustTypeID & " AND " & _
                      "[ActualAC] = '" & Me!ActualAC & "'"

    If DCount("*", "Customerstbl", strLinkCriteria) > 0 Then
Cancel = True
        Me.ActualAC.Undo
        MsgBox "This customer already exists!" & vbCrLf & _
        "Please type the A/C No. in the Customer's A/C field.", vbCritical, "Wrong Entry"
End If
    Me.AccountNo.SetFocus
End Sub

Thanks in advance!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:19
Joined
May 7, 2009
Messages
19,230
if you want to move to another control, don't set Cancel to True.
 

June7

AWF VIP
Local time
Today, 10:19
Joined
Mar 9, 2014
Messages
5,470
Why would you go to another control and not stay on the one that just triggered 'incorrect data' message so user can enter valid data?

Is ActualAC a required field?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:19
Joined
Oct 29, 2018
Messages
21,467
Hi. Is it "always" not setting the focus or only when you get the message?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:19
Joined
May 7, 2009
Messages
19,230
hmm, do it in AfterUpdate event
Code:
Private Sub ActualAC_AfterUpdate()
On Error Resume Next
Dim strLinkCriteria As String
    strLinkCriteria = "[CustTypeID] = " & Me!CustTypeID & " AND " & _
                      "[ActualAC] = '" & Me!ActualAC & "'"

If DCount("*", "Customerstbl", strLinkCriteria) > 0 Then
        MsgBox "This customer already exists!" & vbCrLf & _
        "Please type the A/C No. in the Customer's A/C field.", vbCritical, "Wrong Entry"
    Me. ActualAC = Me.ActualAC.OldValue
    Me.AccountNo.SetFocus
End If
End Sub
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 11:19
Joined
Oct 29, 2018
Messages
21,467
No, I am not getting any error.

Did you try stepping through the code? Have you tried setting the focus somewhere else just to see if it will work with other controls?

Or, maybe Arnel is correct, you can't set the focus using the BeforeUpdate event.
 

Alhakeem1977

Registered User.
Local time
Today, 21:19
Joined
Jun 24, 2017
Messages
308
hmm, do it in AfterUpdate event
Code:
Private Sub ActualAC_AfterUpdate()
On Error Resume Next
Dim strLinkCriteria As String
    strLinkCriteria = "[CustTypeID] = " & Me!CustTypeID & " AND " & _
                      "[ActualAC] = '" & Me!ActualAC & "'"

If DCount("*", "Customerstbl", strLinkCriteria) > 0 Then
        MsgBox "This customer already exists!" & vbCrLf & _
        "Please type the A/C No. in the Customer's A/C field.", vbCritical, "Wrong Entry"
    Me. ActualAC = Me.ActualAC.OldValue
    Me.AccountNo.SetFocus
End If
End Sub

Thanks a lot, it's working :)
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:19
Joined
May 7, 2009
Messages
19,230
you're welcome!
 

Alhakeem1977

Registered User.
Local time
Today, 21:19
Joined
Jun 24, 2017
Messages
308
Did you try stepping through the code? Have you tried setting the focus somewhere else just to see if it will work with other controls?

Or, maybe Arnel is correct, you can't set the focus using the BeforeUpdate event.

Thanks, thaDBguy with the code provided by arnelgp.

Thanks a lot for your attention!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:19
Joined
Feb 19, 2002
Messages
43,257
This is what the code should look like:
Code:
Private Sub ActualAC_BeforeUpdate(Cancel As Integer)
On Error Resume Next
Dim strLinkCriteria As String
    strLinkCriteria = "[CustTypeID] = " & Me!CustTypeID & " AND " & _
                      "[ActualAC] = '" & Me!ActualAC & "'"

    If DCount("*", "Customerstbl", strLinkCriteria) > 0 Then
        Cancel = True
        Me.ActualAC.Undo
        MsgBox "This customer already exists!" & vbCrLf & _
        "Please type the A/C No. in the Customer's A/C field.", vbCritical, "Wrong Entry"
        Exit Sub
End If
End Sub
The update event gets cancelled and focus stays in the field with the error. Personally, I almost NEVER use .Undo in this way. I think it is annoying to the user to have his entry deleted. It is better for him to see the entry that was bad so that he knows what is going on. The ONLY situation where I use .Undo is if the user is just not authorized to make the change so it doesn't matter what he entered. I'm not going to allow him to make any change.
 

Users who are viewing this thread

Top Bottom