Load a form/report dynamically with tempvars

GrandMasterTuck

In need of medication
Local time
Today, 15:03
Joined
May 4, 2013
Messages
129
[SOLVED] Load a form/report dynamically with tempvars

Hey folks... is there a way to code a button that will load a form or report, but have the form or report's name saved in a TempVar?

For example, have a button on Form1 that loads either Form2 or Form3, depending on which form's name is saved to a TempVar. So if some other action saves the text "Form1" as TempVars!TheFormName, the button's code says Open the form called """ & TempVars!TheFormName & """ or whatever...

Am I making any sense? Here's the OpenForm VBA:

Code:
Private Sub OpenForm_Click()
    DoCmd.OpenForm "frmForm1"
End Sub

What I want to do is fetch that value in quotes there with the value of a TempVar. What's the syntax for this?

Code:
DoCmd.OpenForm """ & [TempVars]![TheFormName] & """
 
Last edited:
Ah... as usual, I was overcomplicating it. As it turns out, you don't need quotes at all. The command is simply:

Code:
DoCmd.OpenForm [TempVars]![TheFormName]

I must have tried fifty different permutations with quotes. Single-Double-Single, I tried an equals sign and enclosing the whole command in quotes and doing the & triple quote & thing... and all that time, I didn't need any quotes at all. Duh!
 
But it's an important principle to see, that you need delimiters with a literal or hard-coded value. Delimiters are type specifiers, which are usually not required when you refer directly to a variable. A variable is commonly already typed.

Nice job!
 
I'm finding more and more often that VBA is quite a bit more simple than it's made out to be, and the VBA methods for accomplishing tasks that I've thus far been using macros for are much easier in most cases. I really need to dig further into more complicated uses of VBA and get comfortable with the syntax, because this macro thing has gotten me using bad habits!
 
I agree. People have anxiety that VBA is hard, so MS makes other more opaque tools, reinforcing that VBA is hard. Yeah, there's a learning curve, but VBA is more transparent, and so is easier to document, understand and maintain. And VBA gives you way more control.
 

Users who are viewing this thread

Back
Top Bottom