Results 1 to 8 of 8
  1. #1
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    TempVar Need help on using TempVar in VBA and passing it to Access 2007 Query.

    I need to pass a Date from VBA to an Access 2007 query. Very little information exists. Most of it is related to Macros. I need help with it in VBA.



    I pull a Date field into my VBA. I need to subtract 1 year from it, and then pass it using a TempVar to an Access 2007 Append query. My understanding is that TempVars are available to Access Queries. My problem is that there is almost no practical information on TempVars. Here is my code so far:

    Code:
    Private Sub BTNNewPYD_Click()
    Dim stFFTaxYear As Date 'Current File Tax Year
    Dim stVar1 As TempVar
    stFFTaxYear = Me.TaxYear
    stFFTaxYear = (DateAdd("YYYY", -1, stFFTaxYear))
    stVar1 = stFFTaxYear 'It errors out on this line. Error 91 Object Variable or With Block variable not set.
    Can anyone help or point me in a practical direction?

    Thanks Phred

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    mmmm well I like to be able to set things up for a more straight forward visual debug.....so I see you have Me.TaxYear which implies there is a form open...
    so I would put in a new unbound field into this form named PriorYear and put its record source to be your formula for getting prior year

    then you can see the value in the field temporarily for development purpose - and then make it not visible later when you release the app...

    visible or not visible your query can call the PriorYear field easily

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,925
    As I understand them, can define TempVars in VBA or macro. http://blogs.msdn.com/b/thirdoffive/...13/570539.aspx

    The trick is figuring out where and when. Perhaps in the Open event of a form set to open by default or in an AutoExec macro which will run when the db opens.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    keviny04 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Apr 2015
    Posts
    128
    Alternatively, you may also consider building queries with code. That way, you can use your public variables or any variables, as you originally intended. I replied to your other thread as well. E.g.:

    Dim q As QueryDef
    Set q = DBEngine(0)(0).QueryDefs("My Query Name")
    q.SQL = "select * from MyTable where field1=' & MyPublicVar & "'"

  5. #5
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    NTC, I get what you are saying. Hadn't thought of that. I think this would work. Much easier than TempVars. Iwill try it and let you know.

    Thanks. All your guesses on the form were correct. Sorry I didn't have more info for you.

    Phred

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you are trying to pass the value from one or two fields from a form to a report, you can use Open Args as an alternative to Global Variables.

    Here is a copy from a mocrosoft example opening a form and passing literal text via Open Args. The value is Callahan

    DoCmd.OpenForm "Employees", acNormal, , , acReadOnly, ,"Callahan"

  7. #7
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    June7, thanks for the link. I found this code in the article. Do you think this code would function inside a vba module? Would you create a separate vba module to handle this such as in OnLoad. It looks like it operates under its own function. I don't see a Dim. Where did the tv come from? I know it is short for TempVar but why? It looks like a For Next loop that runs three times based on the value "3" set in the code. How is the value placed in the TempVar? The code is to cryptic for me to understand. I am hoping some of you more experienced coders could pick it apart and under stand its operation. My goal is to drop a date (it could be converted into a string) into the TempVar and, as I understand it, I would be able to drop it into a Access 2007 query to run. I would like to work on this on the side. If I can get it running, post it on the forum for others to use.

    Code:
    Function foo()
    TempVars("Temp1").Value = 3
    For Each tv In TempVars
        MsgBox tv.Name & " = " & CStr(tv.Value)
    Next t
    End Function
    Phred

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,925
    tv is an undeclared variable. Not the best code example because it lacks Dim statements. Without Dim statement the variable defaults to Variant type.

    All that function does is list TempVars variables that have been established elsewhere. I don't see that it is relevant to your requirements. The link was offered just for education about TempVars, not any code particularly suited to your needs.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Wildcard / TempVar Criteria
    By mrmims in forum Queries
    Replies: 3
    Last Post: 04-08-2015, 04:12 PM
  2. tempvar update query date field
    By raffi in forum Queries
    Replies: 2
    Last Post: 09-27-2014, 11:51 PM
  3. Condition if there is a tempvar
    By Ruegen in forum Programming
    Replies: 1
    Last Post: 11-18-2013, 05:06 PM
  4. Passing parameters into vb using access query
    By markyboy171 in forum Programming
    Replies: 4
    Last Post: 06-15-2012, 05:46 AM
  5. Replies: 1
    Last Post: 01-24-2012, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums