Solved Variable not defined (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 07:55
Joined
Oct 14, 2019
Messages
605
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
 
Dim myForm As Form
Set myForm = Me!SubformControlName.Form
 
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?
 
I am working on a form named:- sfcCustDetail

Open the form:- sfcCustDetail

Press the button:- Add &New Invoice

And the control named receipt in the subform will be changed to Red

I use the following code:-


Code:
Private Sub btnNew_Click()
'''Dim dbs As DAO.Database
'''Dim rst As DAO.Recordset
''''Dim frm As Form
''''Set frm = Me.sfcInvoice.Form
'''
'''Set dbs = CurrentDb
'''Set rst = dbs.OpenRecordset("Select * from tblInvoice")
'''    With rst
'''        .AddNew
'''        !fNameID = Me.NameID
'''        !InvoiceNo = Nz(DMax("InvoiceNo", "tblInvoice"), 0) + 1
'''        .Update
'''        .Close
'''    End With
'''
'''Set rst = Nothing
'''Me.Recordset.Requery
Call FormFocus
End Sub

Public Sub FormFocus()

    MsgBox " >>> " & "Public Sub FormFocus()"

    Dim sf1 As Form

    ' Reference the subform's form object
    Set sf1 = Me!sfcInvoice.Form

    MsgBox " >>> " & sf1.Name

    ' Set focus on a control within the subform (e.g., Received)
    sf1!Received.SetFocus

    ' Change the BackColor to #BA1419 (which is RGB(186, 20, 25))
    sf1!Received.BackColor = RGB(186, 20, 25)

End Sub
 

Attachments

The purpose here is to go to sfcInvItem.Description. When adding a new invoice I want items and not receipts.
 
The purpose here is to go to sfcInvInf.Description. When adding a new invoice I want items and not receipts.

And my purpose is to provide you with a working example...

You will need to modify the code to suit your requirements....
 
I want items and not receipts.

I was trying to identify the mismatch between the answer I provided, and your last comment, which seemed to infer that I had answered the wrong question?

So I retraced my steps - I opened your sample database...

Using the form that opened, I opened it in design view and went to the code behind the form.

I added the following comments at the top of the code module:-

Option Compare Database
Option Explicit

Public faytNameSearch As New FindAsYouTypeCombo

'Search
'sf1

"Search"
is my place marker for text I am searching for in the database

And below that is the name of the variable you mentioned in your first post:-

Set sf1 = Me!Form.sfcInvoice.Form >>>sf1<<<

I selected "sf1" and searched the whole project. I was taken to the code behind the form named:- sfcCustDetail

And to a function in that form named:- Public Sub FormFocus()

Where I did experiments and tests, to test the code and eventually got it working....
 
I get what you're saying but you didn't actually change anything from message #6 and I still get "Variable not defined".
 
I see the problem now!!!

One variable is sf1 and the other one is sfl

Edit for Clarity

Dim sf1 As Form

sfl.SetFocus
 
Last edited:
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.
 
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.

Hi Clara, Don’t be too hard on yourself — we’ve all been there! You’re definitely not stupid, you’ve just encountered a classic developer dilemma: staring at something for so long that you miss the smallest detail. It’s like getting stuck in the trees and missing the forest!

This happens to everyone, and believe me, it’s practically a badge of honor in the developer world. You're officially part of the "I can’t believe I missed that!" club, and trust me, membership is very exclusive!
 

Users who are viewing this thread

Back
Top Bottom