Access VBA Can't set focus

Hi

In my Access VBA I run the following code behind a Combo Box and I get the error
"Can't set focus to Customer_Code"
What am I doing wrong?

Private Sub xcmbCustomer_AfterUpdate()

On Error GoTo EH
    '-----------------------------
    Dim oCustomerCode As String
    
    DoCmd.SetWarnings (False)
    
    oCustomerCode = Me.xcmbCustomer.Text
    Me.Customer_Code.SetFocus
    Me.Customer_Code.Text = oCustomerCode
    Call oInsert_New_Invoice_Number 'eg 2018-001
    
    DoCmd.SetWarnings (True)
    '-----------------------------
    Exit Sub
EH:
    MsgBox Err.Description
    
End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Is the control Customer_Code enabled?
0
mbizupCommented:
You don't need to change the focus.  Just use the Value property of the controls (which is the default):


   Private Sub xcmbCustomer_AfterUpdate()

   On Error GoTo EH
    '-----------------------------
    Dim oCustomerCode As String
    
    DoCmd.SetWarnings (False)
    
    oCustomerCode = Me.xcmbCustomer
    Me.Customer_Code= oCustomerCode
    Call oInsert_New_Invoice_Number 'eg 2018-001
    
    DoCmd.SetWarnings (True)
    '-----------------------------
    Exit Sub
EH:
    MsgBox Err.Description
    
End Sub

Open in new window


See this article for a more detailed explanation:
Value vs Text Properties of controls
0
Dale FyeCommented:
You need to get rid of all of the Docmd.SetWarnings False/True commands in your program and add error handling to every procedure.  

Use of docmd.SetWarnings False will eventually bite you in the a@@, especially when you have no error handling in your code.

Dale
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mbizupCommented:
>>>  Docmd.SetWarnings False/True

I assume that's being done to suppress Access's "You are about to add N records" warning in the call to oInsert_New_Invoice_Number.  

An alternative way to suppress those warnings is to use CurrentDB.execute instead of OpenQuery or RunSQL, coupled with good error handling as Dale suggested:
CurrentDB.Execute  "INSERT INTO yourTable ... etc", dbFailOnError

Open in new window

0
PatHartmanCommented:
You use the .text property in the change event to capture the values chracter by character.  In all other places you would use the default prperty which is .value and can be omitted.

These three lines:

    oCustomerCode = Me.xcmbCustomer.Text
    Me.Customer_Code.SetFocus
    Me.Customer_Code.Text = oCustomerCode

Should be:

Me. Me.Customer_Code = Me.xcmbCustomer

Or probably isn't even necessary at all since it would be very unusual to have the same field bound to two controls.

Also, if by chance you have used a naming standard to name the controls differently from the bound field, then you need to use the names appropriately in code.  For example of the control Name for Customer_Code is txtCustomer_Code then Access will insist that you use Me.txtCustomer_Code when referring to the properties of the control and Me.Customer_Code when referring to the bound field.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.