Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2011, 03:46 AM
Authorized User
 
Join Date: Feb 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default TempVar

I quite "new" in this group / Using Access2007
I want to "define" a TempVar [MyName] through a login-form. This [MyName] I want to be able to re-use it in later forms and reports so that the originater is identified.
I tried some basic coding, but when I try te recall with
=[TempVar]![MyName] this seems to be an unknown item.
I am a beginner in VBA
Thanks
Marc
 
Old August 2nd, 2011, 01:24 PM
gjgriffith's Avatar
Wrox Author
 
Join Date: Jul 2009
Posts: 110
Thanks: 5
Thanked 14 Times in 14 Posts
Default Using TempVars

Hello Cram,

Thanks for posting your question here about using TempVars in Access. You said:

Quote:
I tried some basic coding, but when I try te recall with =[TempVar]![MyName] this seems to be an unknown item.
It looks like you are using the collection correctly (from your notes above), so my questions would be:

1. Did you call TempVar.Add() or SetTempVar() before trying to access the TempVar from the collection?
2. If so, how do you know "this seems to be an unknown item"?

Anyway, please let me know and I'll do what i can to help!

Thanks,
__________________
Geoffrey L. Griffith
http://www.ImagineThought.com

Wrox Author of:
Microsoft Access 2010 24-Hour Trainer
Access 2010 Programmer's Reference
Access 2007 VBA Programmer's Reference

*** Please click the THANKS button (to the right) if this post helped you! *** ---------------------------------------------------------------------------------------------------------->
 
Old August 2nd, 2011, 02:11 PM
Authorized User
 
Join Date: Feb 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey Griffith, thanks for this quick reply.
Here what I have tried:
I have a form with a combo and a text field. beside of that I have a btn to activate the whole thing (so I hoped I could follow/debug).
That's what my "code" looks alike

Option Compare Database
Dim MyName As TempVar
Private Sub btnTest_Click()
MyName.Value = Me.cboCurrentUser
Me.MyNameTxt.Text = MyName
End Sub


I suppose I first had to dim my variable "MyName", and hoping I can use it as "public" I dim before the "private sub" line, which normaly should be a "public sub" I guess.
Thanks,

Marc
 
Old August 2nd, 2011, 02:43 PM
gjgriffith's Avatar
Wrox Author
 
Join Date: Jul 2009
Posts: 110
Thanks: 5
Thanked 14 Times in 14 Posts
Default Use Application.TempVars.Add

Hi Cram,

I saw your post, thanks for the follow up here!

Ok...so, I think I see the issue now. The TempVars collection exists in the application's memory, so to use them, you just add a new TempVar to the collection by name. So, for example, you could create an Access Form, add 2 Button controls to that Form, and add the following code to the Click events for those buttons:

Code:
Private Sub btnSetTempVar_Click()

    Application.TempVars.Add "MyVarName", "My TempVar Value"

End Sub


Private Sub btnGetTempVar_Click()

    MsgBox "TempVar Value: " & Application.TempVars("MyVarName").Value
    
End Sub
In the above code, the btnSetTempVar_Click() method simply sets a new TempVar variable (in the TempVars collection) by calling the Add() method and passing the specific name and value for the variable that you want to set. The btnGetTempVar_Click() method gets the value of the "MyVarName" variable that I set when (I click the previous button) and shows it in a message box, by simply calling up the item by name ("MyVarName") from the TempVars collection and getting the Value property for the TempVar.

Anyway, hopefully this all makes sense and should be very easy to use in your code. But if you are still having problems, please let me know and I'll do what I can to help!

Thanks,
__________________
Geoffrey L. Griffith
http://www.ImagineThought.com

Wrox Author of:
Microsoft Access 2010 24-Hour Trainer
Access 2010 Programmer's Reference
Access 2007 VBA Programmer's Reference

*** Please click the THANKS button (to the right) if this post helped you! *** ---------------------------------------------------------------------------------------------------------->
 
Old August 6th, 2011, 01:37 PM
Authorized User
 
Join Date: Feb 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Geoffrey
thanks for your reply. This helped me to "activate" my phantasy in trying some other code, and ....it works.
Marc

Quote:
Originally Posted by gjgriffith View Post
Hi Cram,

I saw your post, thanks for the follow up here!

Ok...so, I think I see the issue now. The TempVars collection exists in the application's memory, so to use them, you just add a new TempVar to the collection by name. So, for example, you could create an Access Form, add 2 Button controls to that Form, and add the following code to the Click events for those buttons:

Code:
Private Sub btnSetTempVar_Click()

    Application.TempVars.Add "MyVarName", "My TempVar Value"

End Sub


Private Sub btnGetTempVar_Click()

    MsgBox "TempVar Value: " & Application.TempVars("MyVarName").Value
    
End Sub
In the above code, the btnSetTempVar_Click() method simply sets a new TempVar variable (in the TempVars collection) by calling the Add() method and passing the specific name and value for the variable that you want to set. The btnGetTempVar_Click() method gets the value of the "MyVarName" variable that I set when (I click the previous button) and shows it in a message box, by simply calling up the item by name ("MyVarName") from the TempVars collection and getting the Value property for the TempVar.

Anyway, hopefully this all makes sense and should be very easy to use in your code. But if you are still having problems, please let me know and I'll do what I can to help!

Thanks,
 
Old August 7th, 2011, 06:20 AM
gjgriffith's Avatar
Wrox Author
 
Join Date: Jul 2009
Posts: 110
Thanks: 5
Thanked 14 Times in 14 Posts
Default Get to hear it is working!

Hello Marc,

I'm glad to hear that you were able to get your TempVars to work. They can certainly be useful in an Access database application! And if there is anything else I can do to help, please let me know!

Thanks,
__________________
Geoffrey L. Griffith
http://www.ImagineThought.com

Wrox Author of:
Microsoft Access 2010 24-Hour Trainer
Access 2010 Programmer's Reference
Access 2007 VBA Programmer's Reference

*** Please click the THANKS button (to the right) if this post helped you! *** ---------------------------------------------------------------------------------------------------------->









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.