Share via

TempVars Question

Anonymous
2015-09-06T15:50:10+00:00

Okay, so I'm having trouble getting TempVars to work. Maybe I'm just not understanding the syntax correctly, or TempVars can't do what I want it to.

I have two Forms. On Exit from the first Form, just before control is passed to open the second Form, I execute this Macro:

SetTempVar

      Name Label_Var

Then following the instructions I read on a different site, I replaced the Control Source name on the second Form with Label_Var. The second Form opens, but there's nothing in the control that should contain the TempVar.

Is there another way to do this? Am I doing something wrong? I just want the value that's in the Control on the first Form to show up in the corresponding Control on the second Form, and I was under the impression that using TempVars was the was to do it.

Thanks for any help.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

17 answers

Sort by: Most helpful
  1. Anonymous
    2015-09-06T19:26:50+00:00

    Nice try, but this simply doesn't work. It puts up a message box. I'm not trying to populate a message box.

    I've been struggling with this for over a week now, and to be honest, I'm about to abandon the whole project over this. This should be such a simple thing to accomplish. I can't believe Access has no way of doing it. Yet every reply I've gotten to my question has offered solutions that either don't work or are simply unrelated to what I need to do, and nothing I've tried works.

    Let me explain this one more time:

    I have a Form. The Form has a Control on it called HdrLabelwhich is bound to the same field name in the underlying Table.

    Now, on exiting the Form, I have a macro: SetTempVar

                Name  Label_Var

                Expression [Forms]![New_Entry]![HdrLabel]

    I then open a second Form which has a Control on it called DtlLabel. That Control is bound to the same field name in its underlying table. But the two fields/controls need to contain the same value because each is the primary key and the two files are linked by that key.

    So, when the second Form opens, I don't want the users to have to type in the Label id again, because they could make a mistake and foul things up.

    The HdrLabel Control from the first Form needs to simply be populated in the DtlLabel Control on the second Form. I don't know why this is so difficult. But no one seems to know how to make it work.

    A couple of times, something I did caused the program to blow up with an error, which was a good thing, because when I clicked on Debug, I was able to hover my cursor over the TempVar value coming into the second Form from the first Form, and it was there and correct.

    So the problem is not with setting the TempVar on an Event on the first Form. It's getting that TempVar to appear in the Control on the second Form.

    Like I said, I just can't believe Access has some very simple way to do this. I mean it's like Programming 101. And yet no one has been able to provide an example of code that actually works.

    Was this answer helpful?

    0 comments No comments
  2. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2015-09-06T18:47:49+00:00

    This works for me:

    SetTempVar

      Name varOne

      Expression "test1"

    OpenForm

      FormName Form2

      'etc.

    Then in Form2's Load event another macro:

    MessageBox

      Message =TempVars

      'etc.

    Note that if you were using VBA rather than macros you would be able to use the OpenArgs argument of DoCmd.OpenForm to pass in any desired values for the next form.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-09-06T17:33:08+00:00

    Nope, afraid not. Got an ugly error (2950) which I don't even understand, but I think it has something to do with the fact that you can't use SetValue to set the value of a control on one form to the control on another form. I think it wants you to set the value of a control to a literal string.

    What was the exact error message?

    Did you reference the forms like this --     [Forms]![YourFirstForm]![FormObjectName]  &  [Forms]![YourSecondForm]![FormObjectName]

    I do not know anything about TempVars.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-09-06T16:50:31+00:00

    Nope, afraid not. Got an ugly error (2950) which I don't even understand, but I think it has something to do with the fact that you can't use SetValue to set the value of a control on one form to the control on another form. I think it wants you to set the value of a control to a literal string.

    In any case, according to what I've read and understood, you shouldn't have to use SetValue. The TempVars is supposed to make a variable available across all Forms as long as the variable exists. So, my question continues to be a very basic one.

    If I have a control on one Form called Hdr_Label, how do I create a TempVar that contains the value of Hdr_Label on the first form?

    And then, how do I access the value of that TempVar to populate the control on the second form called Dtl_Label?

    On the first Form, shouldn't I just be able to code: TempVars!LabelVar!Hdr_Label?

    And the on the second Form, code: Dtl_Label!TempVars!LabelVar?

    Maybe I've got the syntax mixed up, but everything I've read indicates that once you set a TempVars, it's available across the board until it's removed. So how do I set a TempVars on the first Form based on a Control value, and then use that TempVars to populate the Control value on the second form? I should think there would be a simple way to do this.

    Thank you.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2015-09-06T16:18:34+00:00

    One way would be to use a macro triggered on open 2nd form (first not closed) to SET VALUE in 2nd form and then close first form.

    Was this answer helpful?

    0 comments No comments