Setting Focus to a field on form


I have a button on a primary form:  VGCustomer that performs (or I am trying to do) the following:


But I get the error:

Can't find the field 'frmVGCust3055CoDriverNoneSub' referred to in your expression.

I have used this in another form and it works, but here...I can't seem to get it to work.

any ideas?

Also I am running other code:

            Form_frmVGCust3055.Visible = False
            Me.frmVGCust3055CoDriverNone.Visible = True
            Form_frmVGCust3055CoDriverNoneSub.Company = Form_frmVGCust3055.Company
            Form_frmVGCust3055CoDriverNoneSub.Make = Form_frmVGCust3055.Make
            Form_frmVGCust3055CoDriverNoneSub.TractorState = Form_frmVGCust3055.TractorState
            Form_frmVGCust3055CoDriverNoneSub.TractorPlate = Form_frmVGCust3055.TractorPlate
            Form_frmVGCust3055CoDriverNoneSub.TrailerState = Form_frmVGCust3055.TrailerState
            Form_frmVGCust3055CoDriverNoneSub.TrailerPlate = Form_frmVGCust3055.TrailerPlate
            Form_frmVGCust3055CoDriverNoneSub.TrailerNumber = Form_frmVGCust3055.TrailerNumber
            Form_frmVGCust3055CoDriverNoneSub.TruckNumber = Form_frmVGCust3055.TruckNumber
            Form_frmVGCust3055CoDriverNoneSub.Location = Form_frmVGCust3055.Location
            Form_frmVGCust3055CoDriverNoneSub.Inspected = "No"
            Form_frmVGCustomer2.State.Enabled = False
            Form_frmVGCustomer2.License.Enabled = False
            Form_frmVGCustomer2.cmdSrchCoDriver.Enabled = False
Ernest GroggSecurity Management InfoSecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Dale FyeOwner, Developing Solutions LLCCommented:
Before setting the focus to a control on a subform, you generally have to set the focus to the form.  So you would do this in two steps.  A couple of other points:
1.  I generally prefer not to refer to the Form with the FORM_ predicate, so I would personally use
2.  Rather than continuing to use the long reference to the main form, I would set a form variable and use that.

Dim frm as form, frmSub as Form

set frm = forms("frmVGCust3055CoDriverNone")
set frmSub = forms("frmVGCust3055CoDriverNone").frmVGCust3055CoDriverNoneSub.Form

Then, for your other code you could use code similar to:

  frmSub.TruckNumber = frm.TruckNumber

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
Ernest GroggSecurity Management InfoSecAuthor Commented:
I am not sure, but Isn't there a with variable in there somewhere?  I have never been able to get this working so I never have used it.  I know in the past it gave me an error as that the "with" variable needed set...
Dale FyeOwner, Developing Solutions LLCCommented:
I don't see a With/End With construct in the code you provided.  But that is another option you could use.  It would look something like:
With forms("frmVGCust3055CoDriverNone")

    .frmVGCust3055CoDriverNoneSub.Form.TruckNumber = .TruckNumber 

End With

Open in new window

This option would use the main form in the with clause, which would require the long reference to the subform control and form in the subsequent lines.  I believe you could also use:
With forms("frmVGCust3055CoDriverNone").frmVGCust3055CoDriverNoneSub.Form

    .TruckNumber = .parent.TruckNumber 

End With

Open in new window

This version sets the With object to the subform, in the With clause, which allows you to use much shorter syntax to set the focus to that object or to refer to the controls or fields on that form.  And then, to refer to the values on the main form, you simply use the syntax:

Is the other form already open?  If not, you cannot reference controls on it.
Ernest GroggSecurity Management InfoSecAuthor Commented:
Ur right...the form was open..didn't realize that it was already open on another operation and didn't fully close as it should have.

Got it now.

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.