• Using global variables in reports

    Author
    Topic
    #460077

    In my database I have defined a global variable ‘gstrSubsYear’. I can check that it is initialising correctly by putting a text box on a form and setting it to gstrSubsYear in the form’s OnOpen event.

    I am trying to use it on a report by having a text box’s control source property set to ‘=”Year to April ” & gstrSubsYear’. Access pops up a parameter box asking for gstrSubsYear and has converted & gstrSubsYear into [gstrSubsYear].

    I thought I knew about concatenation of strings but have never come across this behaviour before. How do I get the field to recognise the global variable?

    Regards

    David

    Viewing 0 reply threads
    Author
    Replies
    • #1161979

      You can use global variables in VBA code but not in queries and in expressions such as the control source of a text box.

      To get around this, create a function in a standard module that returns the value of the variable:

      Code:
      Public Function GetSubsYear() As String
        GetSubsYear = gstrSubsYear
      End Function

      You can use it like this:

      =”Year to April ” & GetSubsYear()

      • #1162010

        Many thanks Hans. I hadn’t come across that limitation before.

        Regards
        David

    Viewing 0 reply threads
    Reply To: Using global variables in reports

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: