Solved

Using a global variable to filter a query

Posted on 2001-07-24
11
216 Views
Last Modified: 2012-06-27
I set a global variable up and it?s value is set using data from a form. I then wish  to filter records in a query using this variable. I have tried putting [var name] on the criteria line but this does not work. Has anyone a suggestions?
0
Comment
Question by:hotathens
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6313129
Have you tried referencing the form directly?

SELECT * FROM tblYourTable WHERE ID = [Forms]![YourFormName]![txtIDSelection]
0
 
LVL 2

Accepted Solution

by:
hotbudare earned 100 total points
ID: 6313165
You'll have to create UDF that returns the variable's value, something like (in a standard module):

public MyGlobalVar as YourDataType

public function MyGlobalVarValue() as YourDataType

    MyGlobalVarValue=MyGlobalVar
end function

and then use this function in your query.
Please keep in mind that this code has to be in a standard module ...

HTH/EQTA
T.S.U. Mario Osorio
Punto Fijo, Falcon, Venezuela

0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6313170
you could also make a public function in a module that returns the variable and use the function in the criteria:

Public Function GetCurrentID() As Long
    GetCurrentID = GlobalID
End Function

SELECT * FROM tblYourTable WHERE ID = GetCurrentID()
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 8

Expert Comment

by:dovholuk
ID: 6313184
cjswimmer's answer will suit you best. you don't need to use a function or a global variable if you reference the data from your form directly.

dovholuk
0
 
LVL 6

Expert Comment

by:Mach1pro
ID: 6313189
create a Public function and set its value to your global variable then reference the Function in your query.

Public Function MyVariable() As Long
     MyVariable = GlobalVariableName
End Function


In the criteria section of your query field just put in   MyVariable()
If your global variable is of numeric type and it hasn't been initialized, MyVariable() will equal zero.
If your global variable is not numeric and has not been initialized, you will probably get an error
0
 
LVL 2

Expert Comment

by:hotbudare
ID: 6314199
dovholuk,
You are right, but there's an advantage to my approach, and it is that in my proposed way hotathens will be able to use this query anytime, he'll just have to make sure he's got the appropiate values in the global variable(s)before he uses his query.

BTW, cjswimmer (in his second proposition) and Mach1pro are both using the same approach I originaly stated.

HTH/EQTA
T.S.U. Mario Osorio
Punto Fijo, Falcon, Venezuela

0
 
LVL 11

Expert Comment

by:joekendall
ID: 6314274
If you will be using the query for a control on a form or for a report, you could write code to do it. For example:

Dim sSQL As String

'UserName is the Global Variable
sSQL = "SELECT Report, User " & _
       "FROM Table2 " & _
       "WHERE (User Like 'all' Or " & _
       "User Like '*" & UserName & "*');"

'Set the controls RowSource to the Sequel        
cboReport.RowSource = sSQL

'Or you can use everything above but change the
'cboReport.RowSource line to this
Report.RecordSource = sSQL

This is just another way to do what has been stated above.

HTH,

Joe
0
 

Author Comment

by:hotathens
ID: 6316982
Joe

where would i put the code on the  form ?
0
 
LVL 11

Expert Comment

by:joekendall
ID: 6317417
You can put the code in the AfterUpdate event in the control that is used as the criteria. Then the control that needs this as a RowSource will automatically be updated with this criteria from your Global Variable.

HTH,

Joe
0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6365652
any update hotathens?
0
 
LVL 2

Expert Comment

by:hotbudare
ID: 6377373
Thanks a lot hotathens ... but why the "B" grade?
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access Query on CSV returning number not text 6 37
Use after update to change dates 2 16
Access 2016 - combo box 3 18
MS-Access not responding 5 20
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question