Solved Variable not defined

ClaraBarton

Registered User.
Local time
Today, 14:23
Joined
Oct 14, 2019
Messages
788
Code:
Public Sub FormFocus()
Dim sf1 As Control
Dim sf2 As Control

    Set sf1 = Me!Form.sfcInvoice.Form
    Set sf2 = sf1.sfcInvItem

sfl.SetFocus
sf2.SetFocus
sf2.Form!Description.SetFocus

Set sf2 = Nothing
Set sf1 = Nothing

at sf1.SetFocus I get a Variable Not Defined error
This sub is called from sfcCustDetail
Which has a subform named sfcInvoice
Which has a subform named sfcInvItem

I have also tried Set sf1 = Forms!sfcCustDetail.Form.sfcInvoice.Form and I get the same error
Can you point me to my error?
 
It looks like you have a form with a subform that contains a subform and you want to set the focus to a control on the subsubform.
I would try change this line:

Set sf1 = Me!Form.sfcInvoice.Form
to
Set sf1 = Me.sfcInvoice.Form
 
I don't think you can dim sf1 as a control then try and set it a form reference?
Shouldn't it be

Dim sf1 as Form

Unless sf1 is a subform control, and not the form itself ?
 
Still same error. sf1 is a subform control and the form in the control has the same name
 
Code:
Public Sub FormFocus()
Dim sf1 As Form
Dim sf2 As Form

    Set sf1 = Me!sfcInvoice.Form
    Set sf2 = sf1.sfcInvItem

sfl.SetFocus
sf2.SetFocus
sf2.Form!Description.SetFocus
same error; same place
Although this error (and code) is on the sfcCustDetail, this form is a subform of a main form. That shouldn't be a problem, should it?
 
Probably, as you have to get the syntax correct, re the structure.
 
Can you clarify your form subform setup and exactly where the code is running?
Have you simply tried using the form/subform names rather than dimming form variables?
 
The purpose here is to go to sfcInvItem.Description. When adding a new invoice I want items and not receipts.
 
I get what you're saying but you didn't actually change anything from message #6 and I still get "Variable not defined".
 
I cannot believe I was so STUPID! I am not a newby! I actually thought I had copied it down because I wanted to check everything before I asked. I thank you so much for your trouble! Going somewhere to die now.
 
I see the problem now!!!

One variable is sf1 and the other one is sfl

Edit for Clarity

Dim sf1 As Form

sfl.SetFocus
Great eye Tony? (y)
Option Explicit would have picked that up as well on a compile. :(
 
try this code:
Code:
    ' arnelgp
    Forms!frmAAAA.SetFocus
    Forms!frmAAAA!sfcCustDetail.SetFocus
    Forms!frmAAAA!sfcCustDetail!sfcInvoice.SetFocus
    Forms!frmAAAA!sfcCustDetail!sfcInvoice.Form!Received.SetFocus
 
Option Explicit did pick it up. It wouldn't compile. I was blind.
 
It's hard to distinguish between a 1 and a lowercase l.

Sometimes the errors get picked up are illusory as well. it's easy to get coding constructs that pass the syntax tests but don't run in the way you wanted.
 

Users who are viewing this thread

Back
Top Bottom