CurrentForm Variable Error (1 Viewer)

TheSearcher

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 21, 2011
Messages
304
I'm trying to use a global variable to hold the name of a form. It works great and returns the correct form name but I get the error below when referencing it from within the form collection. Does anyone know why?

'On Open event of frm_X
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm
Globals.glb_CurrentForm = frmCurrentForm.Name 'This returns the correct form name.

'Code in frm_Y
Forms!Globals.glb_CurrentForm!lst_Tasks.Requery

1667851460632.png
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:50
Joined
Oct 29, 2018
Messages
21,477
You have to use a different syntax. Try:

Forms(Globals)...
 

TheSearcher

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 21, 2011
Messages
304
Thanks DBguy. Unfortunately, that didn't work. I tried:
  • Forms!(Globals).glb_CurrentForm!lst_Tasks.Requery
  • Forms!(Globals.glb_CurrentForm)!lst_Tasks.Requery
and now I get this error:
1667852489466.png
 

MarkK

bit cruncher
Local time
Yesterday, 17:50
Joined
Mar 17, 2004
Messages
8,182
I'm trying to use a global variable
This is the hazard of using globals. You cannot know for sure that some other code hasn't changed or damaged the value you think resides there. With a global, you can only hope that it still contains the value you previously assigned it. Some other developer or user or process has full access to it. If you design a process that leverages a global, make to always run it with your fingers crossed.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:50
Joined
Oct 29, 2018
Messages
21,477
Thanks DBguy. Unfortunately, that didn't work. I tried:
  • Forms!(Globals).glb_CurrentForm!lst_Tasks.Requery
  • Forms!(Globals.glb_CurrentForm)!lst_Tasks.Requery
and now I get this error:
View attachment 104444
Hi. It didn't work because you didn't follow my example. I didn't use the bang operator.
 

TheSearcher

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 21, 2011
Messages
304
Globals.glb_CurrentForm = "Forms(" & Globals.glb_CurrentForm & ")"

I removed the bang, included "Forms" in my variable and enclosed it with parentheses. Now I get the error below. Not sure how to qualify lst_Tasks.Requery. I tried the bang and a period in my variable but no luck.

It errors out on this: Forms!Globals.glb_CurrentForm!lst_Tasks.Requery
I'm not surprised because it's being read as Forms(Globals.glb_CurrentForm)lst_Tasks.Requery
1667859470573.png
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:50
Joined
Oct 29, 2018
Messages
21,477
Globals.glb_CurrentForm = "Forms(" & Globals.glb_CurrentForm & ")"

I removed the bang, included "Forms" in my variable and enclosed it with parentheses. Now I get the error below. Not sure how to qualify lst_Tasks.Requery. I tried the bang and a period in my variable but no luck.

It errors out on this: Forms!Globals.glb_CurrentForm!lst_Tasks.Requery
I'm not surprised because it's being read as Forms(Globals.glb_CurrentForm)lst_Tasks.Requery
View attachment 104445
Hmm, I gave you a very simple example, and you seem determined to not follow it. I'm not sure how to help you further. Perhaps if you could post your actual code, we can then tell you exactly how to change it. In essence, this is what I was saying:
Code:
Public Globals As String
...
Globals = "frmMain"
...
Forms(Globals).Requery 

or

Forms(Globals).Visible = False

etc.
Hope that makes sense...
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:50
Joined
May 21, 2018
Messages
8,536
Here is some of the why

In short Bang never works with a variable

Assume the form's name is: "SomeLiteralName"
Assume you set a variable to this name.

Code:
Dim VariableName as string
VariableName = "SomeLiteralName"

These work
Code:
forms!SomeLiteralName   'Use literal name
forms("SomeLiteralName")  ' User literal enclosed in quotes
forms(VariableName)   'Pass a variable for the index

This does not work since you are using a variable with Bang.
Code:
Forms!VariableName

So this fails because you cannot use Bang notation with a variable
Code:
'Code in frm_Y
Forms!Globals.glb_CurrentForm!lst_Tasks.Requery
Same reason here
Code:
Forms!(Globals).glb_CurrentForm!lst_Tasks.Requery
Forms!(Globals.glb_CurrentForm)!lst_Tasks.Requery
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:50
Joined
Feb 19, 2002
Messages
43,302
I'm not sure what you are trying to accomplish so I'm just going to tell you what I do and why.

I strongly dislike having multiple forms open so when I open one form from another, I hide the calling form and then open the next form as a Dialog to ensure that focus stays there until I close the second form and using the OpenArgs argument, I pass in the name of the calling form -- Me.Name. That means that when the called form closes, it always opens the form passed in the OpenArgs. Keep in mind that if you are opening a form from a subform, you want control to return to the parent form, NOT the subform so you would use Me.Parent.Name.

Most of the time formB is only ever opened by formA so when formB closes, control should return to formA but sometimes, the same form needs to be opened from different forms and I want control to return appropriately. The described method gives me that flexibility. I get to have only one form open, most of the time and each form when it closes, always returns to the form that opened it. Obviously, you have a different close method for the login form. I also use this technique to allow me to keep the login form open but hidden throughout a session. That way, I can keep various user data available without having to use tempVars or globals. I just refer to controls on the hidden login form. And finally, since the login form is always the first to stay open and it remains open it does two things:
1. keeps a permanent link to the BE to reduce the close/open cycle
2. Gives me a place to do shut down processing like make backups, etc.

I also never use Screen.Active... to refer to the current form or control. There are strange errors in situations where what you think is active isn't really active. See if you can make do with Me.Name instead, if you want to pass a name or just use Me if you want to pass the form object itself.
 

cheekybuddha

AWF VIP
Local time
Today, 01:50
Joined
Jul 21, 2014
Messages
2,280
It looks like you are fully qualifying your variable name with the name of the module it resides in.

So you must adapt the syntax offered to you by @theDBguy :
Code:
Forms(Globals.glb_CurrentForm).lst_Tasks.Requery
 

TheSearcher

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 21, 2011
Messages
304
Thank you all very much for taking time with this. cheekybuddha's example above (via DBguy) definitely should have worked. Unfortunately it didn't along with everything else I've tried. It seems Access cannot recognize a variable used in this way. I will follow Pat's advise and take a completely different approach. Thanks again!
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:50
Joined
May 21, 2018
Messages
8,536
It seems Access cannot recognize a variable used in this way.
NO! Of course Access can. Just because you must be doing something incorrectly, does not mean the application can not do it. Referencing a form name with a variable is done all the time.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:50
Joined
Oct 29, 2018
Messages
21,477
Thank you all very much for taking time with this. cheekybuddha's example above (via DBguy) definitely should have worked. Unfortunately it didn't along with everything else I've tried. It seems Access cannot recognize a variable used in this way. I will follow Pat's advise and take a completely different approach. Thanks again!
As I had mentioned earlier, perhaps you could consider posting your entire code, so we can tell you how to fix it.
 

moke123

AWF VIP
Local time
Yesterday, 20:50
Joined
Jan 11, 2013
Messages
3,921
If this is only to requery listboxes try this. I usually only have 2 or 3 forms open at the same time and this insures that all the listboxes are in sync with any changes I make to data.

Code:
Public Sub RequeryAllLists()

    Dim ctl As Control
    Dim frm As Variant

    For Each frm In CurrentProject.AllForms
        If frm.IsLoaded Then
            For Each ctl In Forms(frm.Name).Controls
                If ctl.ControlType = acListBox Then
                    ctl.Requery
                End If
            Next
        End If
    Next

End Sub
 

TheSearcher

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 21, 2011
Messages
304
moke123 - My code is not only to requery listboxes but your code is very elegant and helpful. Thanks!
 

Users who are viewing this thread

Top Bottom