What are TempVars for?

M

M Skabialka

I have downloaded a couple of the MS Access templates and not liking macros
converted all of the macros on the forms to VBA code. However a lot of the
macros use TempVars, and when changed to code don't always work. Here's an
example

TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

When it gets to the 2nd line above, the value of TempVars!ActiveControlValue
is "[Screen].[ActiveControl]" which is not the ID number the code was
expecting.
What is the purpose of TempVars and how are they supposed to work in
examples such is this, which is the after update on a combo which should be
looking for a record on the form.
 
C

Clif McIrvin

I can't answer you full question, but it looks like a pair of extra
quotes are your trouble in this case; also you could try adding .value
to further disambiguate the code.

Change
TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"

to
TempVars.Add "ActiveControlValue", [Screen].[ActiveControl].Value

HTH!
--
Clif

M Skabialka said:
I have downloaded a couple of the MS Access templates and not liking
macros converted all of the macros on the forms to VBA code. However a
lot of the macros use TempVars, and when changed to code don't always
work. Here's an example

TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

When it gets to the 2nd line above, the value of
TempVars!ActiveControlValue is "[Screen].[ActiveControl]" which is not
the ID number the code was expecting.
What is the purpose of TempVars and how are they supposed to work in
examples such is this, which is the after update on a combo which
should be looking for a record on the form.
 
A

Albert D. Kallal

What is the purpose of TempVars

They are just a "holding" spot for values. Macros never had "variables"
before, and now they do. So, if you are a macro writing person, then this is
a big step forward for making macros useful...
how are they supposed to work in examples such is this

You likely can just go:

DoCmd.SearchForRecord , "", acFirst, "[ID]=" me.NameOfComboBox

I suppose you could declare a variable to "hold" that value, but in your
cause it likely just best to reference the combo box directly.....
 
M

M Skabialka

That's what I would have done if I had created the combo box myself.

How is a TempVar different from declaring a variable and using that in a
module? Or were they created for use in Macros?
Thanks,
Mich

Albert D. Kallal said:
What is the purpose of TempVars

They are just a "holding" spot for values. Macros never had "variables"
before, and now they do. So, if you are a macro writing person, then this
is a big step forward for making macros useful...
how are they supposed to work in examples such is this

You likely can just go:

DoCmd.SearchForRecord , "", acFirst, "[ID]=" me.NameOfComboBox

I suppose you could declare a variable to "hold" that value, but in your
cause it likely just best to reference the combo box directly.....
 
M

M Skabialka

The entire code module now says:

If (IsNull(Screen.ActiveControl)) Then
Exit Sub
End If
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
TempVars.Add "ActiveControlValue", [Screen].[ActiveControl].Value
'this works -- thanks

If (CurrentProject.IsTrusted) Then
'Screen.ActiveControl = Null '----???-----
End If
If (Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

Everything works except I had to comment out the line Screen.ActiveControl =
Null because it changed the value to null before it was used.
Can I also safety remove the if-then looking at the MacroError.Number since
it isn't a macro any more?
Thanks,
Mich


Clif McIrvin said:
I can't answer you full question, but it looks like a pair of extra quotes
are your trouble in this case; also you could try adding .value to further
disambiguate the code.

Change
TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"

to
TempVars.Add "ActiveControlValue", [Screen].[ActiveControl].Value

HTH!
--
Clif

M Skabialka said:
I have downloaded a couple of the MS Access templates and not liking
macros converted all of the macros on the forms to VBA code. However a
lot of the macros use TempVars, and when changed to code don't always
work. Here's an example

TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

When it gets to the 2nd line above, the value of
TempVars!ActiveControlValue is "[Screen].[ActiveControl]" which is not
the ID number the code was expecting.
What is the purpose of TempVars and how are they supposed to work in
examples such is this, which is the after update on a combo which should
be looking for a record on the form.
 
A

Albert D. Kallal

M Skabialka said:
That's what I would have done if I had created the combo box myself.

How is a TempVar different from declaring a variable and using that in a
module? Or were they created for use in Macros?
Thanks,
Mich

They were mainly created for macros. However, you can use them in code and
they don't re-set when you have a error. So, it is a "handy dandy" place to
stuff values that don't go away when errors occur..
 
C

Clif McIrvin

This is getting beyond what I have had experience with ... I berlieve

If err.num <> 0 then

would be the VBA replacement code.

--
Clif

M Skabialka said:
The entire code module now says:

If (IsNull(Screen.ActiveControl)) Then
Exit Sub
End If
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
TempVars.Add "ActiveControlValue", [Screen].[ActiveControl].Value
'this works -- thanks

If (CurrentProject.IsTrusted) Then
'Screen.ActiveControl = Null '----???-----
End If
If (Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

Everything works except I had to comment out the line
Screen.ActiveControl = Null because it changed the value to null
before it was used.
Can I also safety remove the if-then looking at the MacroError.Number
since it isn't a macro any more?
Thanks,
Mich


Clif McIrvin said:
I can't answer you full question, but it looks like a pair of extra
quotes are your trouble in this case; also you could try adding .value
to further disambiguate the code.

Change
TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"

to
TempVars.Add "ActiveControlValue", [Screen].[ActiveControl].Value

HTH!
--
Clif

M Skabialka said:
I have downloaded a couple of the MS Access templates and not liking
macros converted all of the macros on the forms to VBA code. However
a lot of the macros use TempVars, and when changed to code don't
always work. Here's an example

TempVars.Add "ActiveControlValue", "[Screen].[ActiveControl]"
DoCmd.SearchForRecord , "", acFirst, "[ID]=" &
TempVars!ActiveControlValue
TempVars.Remove "ActiveControlValue"

When it gets to the 2nd line above, the value of
TempVars!ActiveControlValue is "[Screen].[ActiveControl]" which is
not the ID number the code was expecting.
What is the purpose of TempVars and how are they supposed to work in
examples such is this, which is the after update on a combo which
should be looking for a record on the form.
 
S

Steve Schapel

They are even handier than that, and certainly more flexibly useful that a
global variable. For example they can also be used in queries, either in
the field definitions or in criteria.
 
S

Steve Schapel

They are even handier than that, and certainly more flexibly useful than
a global variable. For example, you can use them in queries, either as
part of the field definition, or inthe criteria.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top