Results 1 to 8 of 8

Using module-wide variable as a parameter in a query

  1. #1
    Join Date
    Dec 2010
    Posts
    12

    Using module-wide variable as a parameter in a query


    In a module I have a public variable. In a procedure within that module I define that variable. Later on in that same procedure I call a query (through a report). How do I use that public variable as a parameter in my query? Thanks.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,732
    You can't use it directly in a saved query, if that's what you mean. You can create a public function that does nothing but return the value of the variable and use that.

    WHERE FieldName = FunctionName()
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Join Date
    Dec 2010
    Posts
    12
    I tried that, and it seems to call my function three times. Here's my query:

    Code:
    SELECT Table1.*
    FROM Table1
    WHERE ID = Test_ID();
    and here's my function:

    Code:
     
    Public Function Test_ID() As Integer
        Test_ID = InputBox("Enter the Test ID")
        Public_ID = Test_ID
    End Function
    The input box comes up three times. Public_ID is my attempt to capture the value input. Any idea why my query calls my function three times?

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,732
    The function is calling itself, which is generally not good. I would expect somewhere else in your code:

    Public_ID = InputBox("Enter the Test ID")

    And then your function is one line:

    Code:
    Public Function Test_ID() As Integer
      Test_ID = Public_ID
    End Function
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Join Date
    Dec 2010
    Posts
    12
    That worked, thanks.

    Last question (and maybe this belongs in the Reports section): now that I have this public variable, is there any way I can reference this in the header of a report?

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,732
    Same way, with the function. The variable itself can only be accessed directly from within VBA. So, in a textbox:

    =Test_ID()
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Join Date
    Dec 2010
    Posts
    12
    Perfect, thanks very much.

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,732
    No problemo.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. How do I use a MODULE in a Cross-Tab Query?
    By timo1999 in forum Modules
    Replies: 2
    Last Post: 12-13-2014, 04:51 PM
  2. Replies: 3
    Last Post: 10-15-2010, 11:17 AM
  3. Variable from Parameter Query
    By OldCityCat in forum Programming
    Replies: 4
    Last Post: 08-23-2010, 11:25 AM
  4. Replies: 1
    Last Post: 06-30-2010, 12:47 PM
  5. Variable Table Names in a Module
    By Jessica in forum Modules
    Replies: 1
    Last Post: 04-19-2010, 07:38 PM

Posting Permissions

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