Tempvars help

BlueJacket

Registered User.
Local time
Today, 07:42
Joined
Jan 11, 2017
Messages
92
Can I use tempvar to carry on a value from one form to another? For instance, I have a form called frmClients, where the Client name is displayed from a combo box bound to the ClientID. I want to carry that ClientID value to another field in my next form.

To give you an example of what I'm (unsuccessfully) trying to do:

Code:
Private Sub butNewProperty_Click()

    If Me.Dirty Then
        Me.Dirty = False
    End If

    Dim tvClientID As TempVars
    
    TempVars!tvClientID = Me.cboClient
    
    DoCmd.OpenForm frmNewProperty, , , , acFormAdd
    DoCmd.Close acForm, Me.Name
    
End Sub

Which I was then thinking about recalling in my next form (frmNewProperty) like...

Code:
Private Sub Form_Open(Cancel As Integer)

    Me.cboClient = TempVars!tvClientID
    
End Sub

I know this code is not correct, as I'm getting errors saying TempVars cannot store objects (which I don't fully understand how I'm trying to store an object). I'm moreso trying to get a sense if this is the best way to go about what I'm trying to do.

Thanks in advance.

Edit: I did get figure out how to get the code to work, but my main question still remains if this is the best practice.
 
Last edited:
The new code that I got to sort've work (it's doing some funky stuff and changing the names of my clients to the ClientID value):


frmClientInfo
Code:
Private Sub butNewProperty_Click()

    If Me.Dirty Then
        Me.Dirty = False
    End If

    Dim stDocName As String
    Dim tvClientID As TempVars
    

    stDocName = "frmNewProperty"
    TempVars!tvClientID = Me.txtClientID.Value
    
    DoCmd.OpenForm stDocName, , , , acFormAdd
    DoCmd.Close acForm, Me.Name
    
End Sub

frmNewProperty
Code:
Private Sub butBack_Click()

    If Me.Dirty Then
        Me.Dirty = False
    End If

    Dim stDocName As String
    Dim stNewRecord As String

    stDocName = "frmClientInfo"
    
    stNewRecord = "[ClientID] = " & Me.cboClient
    DoCmd.OpenForm stDocName, , , stNewRecord
    
    DoCmd.Close acForm, Me.Name
    
End Sub

Private Sub Form_Current()

    Me.txtClientID = TempVars!tvClientID
    TempVars.Remove (tvClientID)
    
End Sub
 
There are a bunch of ways to do this. TempVars is OK, but there is a risk that the value in the TempVar might stale, so say your user runs your process as designed early in the session, and the second form opens correctly. Then maybe later in the session the user opens your second form directly from the NavPane. In the second case your form will still be triggered by the presence of the TempVar from--possibly--hours ago.

Another option is use the OpenArgs parameter of the DoCmd.OpenForm method.

Another option is to set a value on the form in code after it opens. If the second form, for instance, exposes a public method, calling code can call that method and pass in values in the parameter list. So if Form2 exposes a method like...
Code:
private m_data as object

Public Sub PushDataIn(data as object)
   set m_data = data
End Sub
...then when you open Form2 from Form1, you can push data into Form2 like...
Code:
const FN as string = "Form2"
docmd.openform FN
Forms(FN).PushDataIn Me
...so you can see that with code like that, Form2 now hosts a reference to its calling form Form1.

So there are a few ways to get forms to talk to each other directly, as opposed to using a third party like a TempVar.

Hope this helps,
 
TempVars are not handles like regular Variables; they're not 'dimmed' and cannot be set to an Object, like a Textbox, directly.

To create a TempVars and set it to the Value of a Textbox, you need to do it like this, assigning the Value of the Textbox to a 'regular' Variable, first:

Code:
Dim CID as String

CID = Me.txtClientID.Value

TempVars.Add "tvClientID", CID

Also, to Delete a TempVars, it's name has to be in Quotes

Code:
TempVars.Remove "tvClientID"

Mark's concern: "In the second case your form will still be triggered by the presence of the TempVar from--possibly--hours ago" is elimintaed by your Deletion of the TempVars as soon as it is used.

Also note...standard practice is to declare all 'standard' Variables at the top of a Sub, not mixed throughout the Sub's code.

Linq ;0)>
 
Last edited:
Another option is use the OpenArgs parameter of the DoCmd.OpenForm method.

I've used OpenArgs of DoCmd.OpenForm to go to a specific record on the next form, but I don't see how it works if I just want to pass on a value of one box to the value of another box in another form.

So here, I have the user selecting/creating a new client. After which, they have a new record form to put in some basic property information, but I want the ClientID field to carry over.

Edit: I was actually able to figure this out by using an On Load event to pull the OpenArg into Me.txtClientID.
 
Last edited:
@missingling

Thanks for help. I updated my code based off your suggestions, but it's still acting very weird. It works great for the first few clients I have listed, but when it gets down to client 13, 14, or 18 (some missing numbers there) it starts acting weird. It changes the client 1's name to the client number of 18 (which would be 18). If I'm passing 13 from one form to the next, it's changing client 13's name to its client ID number, which would be 13. I don't know why it's interacting with the client names at all.
 
missinglinq,

This might not be the correct way to do it, but I've always set Tempvars in the format of Tempvars("tvClientID")=Me.txtClientID

One line of code?

TempVars are not handles like regular Variables; they're not 'dimmed' and cannot be set to an Object, like a Textbox, directly.

To create a TempVars and set it to the Value of a Textbox, you need to do it like this, assigning the Value of the Textbox to a 'regular' Variable, first:

Code:
Dim CID as String

CID = Me.txtClientID.Value

TempVars.Add "tvClientID", CID

Also, to Delete a TempVars, it's name has to be in Quotes

Code:
TempVars.Remove "tvClientID"

Mark's concern: "In the second case your form will still be triggered by the presence of the TempVar from--possibly--hours ago" is elimintaed by your Deletion of the TempVars as soon as it is used.

Also note...standard practice is to declare all 'standard' Variables at the top of a Sub, not mixed throughout the Sub's code.

Linq ;0)>
 
Yes, it appears that you can create a TempVar without specifically calling the Add method. I just did this in the immediate pane...
Code:
TempVars("Test123") = 12

? TempVars("Test123")
 12 

? TempVars.Count
 1 

? TempVars(0).Name
Test123
 

Users who are viewing this thread

Back
Top Bottom