Syntax for using a variable to pass a form name to a VBA function that calls for a string name (1 Viewer)

JMongi

Active member
Local time
Today, 08:28
Joined
Jan 6, 2021
Messages
802
I probably butchered that question description...lol!
This is a simple question (I think) but the process has eluded me in the past. The following VBA command works fine:
Code:
DoCmd.OpenForm "frmOperationsDashboard", , , , , , "AI_Hide"
The extra goodies are using a function to hide the Access application window but it's generally irrelevant to the discussion (I think).

I would like to store the previously opened form name as a global variable so that I can use the same code in my template without changing. Something like...
Code:
'In a generic module somewhere
Global OldForm as Form
---------------------------
'In my close form button event code'
Application.Echo False
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm OldForm.Name , , , , , , "AI_Hide"
Application.Echo True

I seem to struggle using variables in the VBA construct of Some.VBA.Command "NecessaryString" yada, yada where I want to manage the "necessary string". Hopefully I've described this well enough.

Edit: I would clearly need to set the OldForm somewhere else in the code.
 

Isaac

Lifelong Learner
Local time
Today, 05:28
Joined
Mar 14, 2017
Messages
8,738
I'm not sure I understand this post in its entirety, but if you need to store a form name, why not just store the string version of the name?

What exactly is the problem?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:28
Joined
Oct 29, 2018
Messages
21,357
Hi. Not sure if this is what you mean, but here goes...

When you use a command that accepts a String argument, you can simply replace the argument with a String variable. For example:

DoCmd.OpenForm "FormName"

can be replaced with
Code:
strForm = "FormName"
DoCmd.OpenForm strForm
Hope that helps...
 

JMongi

Active member
Local time
Today, 08:28
Joined
Jan 6, 2021
Messages
802
@Issac - That works too. I swear whenever I've tried to something like:

DoCmd.OpenForm "String" to
DoCmd.OpenForm StringVar

it doesn't work. Or i'm dumb. Which is quite possible.
 

JMongi

Active member
Local time
Today, 08:28
Joined
Jan 6, 2021
Messages
802
@theDBguy - So it looks like the answer is I'm dumb! LOL. I swear I've tried that on more than one time without success. I guess at least I have the peace of mind now that if it doesn't work it is something ELSE wrong.
 

Isaac

Lifelong Learner
Local time
Today, 05:28
Joined
Mar 14, 2017
Messages
8,738
@Issac - That works too. I swear whenever I've tried to something like:

DoCmd.OpenForm "String" to
DoCmd.OpenForm StringVar

it doesn't work. Or i'm dumb. Which is quite possible.
Hmm, it definitely should work.

Maybe you hadn't assigned the variable a value or something.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:28
Joined
Oct 29, 2018
Messages
21,357
@theDBguy - So it looks like the answer is I'm dumb! LOL. I swear I've tried that on more than one time without success. I guess at least I have the peace of mind now that if it doesn't work it is something ELSE wrong.
If you try and it doesn't work, you can post the code you used, so we can take a look. Good luck!
 

JMongi

Active member
Local time
Today, 08:28
Joined
Jan 6, 2021
Messages
802
@Isaac - That's probably the most likely scenario now.

I've wasted more than a little time troubleshooting something where VBA didn't work like I thought it did. So, it's good to know I wasn't crazy (this time).
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:28
Joined
Sep 12, 2006
Messages
15,613
@JMongi

You do have option explicit set, don't you?
Maybe you are duplicating the variables

if you have formname as a variable in a form, and also in a public module, depending where your code is running, you may be setting one variable but then testing another in subsequent code.

Also any error would clear all variables back to their default state.

If things aren't working right, I tend to throw up a lot of msgboxes to trace stuff. Others use the immediate window. We also use programme breaks, so you can step through the code line by line, trace the logic, and examine variables
 

JMongi

Active member
Local time
Today, 08:28
Joined
Jan 6, 2021
Messages
802
@gemma-the-husky - Thanks for the great tips! I do have Option Explicit set. I've been caught out by the double variable definition before. That was a head scratcher (modifying someone else's code.)

I'm not having any immediate issues at the moment. This was more of a preemptive strike. My brain was telling me that it SHOULD work that way and I know that whatever situation I tried that in before didn't work as intended. Now I know it was for some OTHER reason. I'm still green enough in this whole world to have the nagging voice in the back say, "Psst...You could be missing something basic." So, now I can troubleshoot without worrying that I'm trying to do something VBA doesn't like.
 

Users who are viewing this thread

Top Bottom