?
Solved

Pass Function info As Query Criteria

Posted on 2007-03-26
13
Medium Priority
?
171 Views
Last Modified: 2013-11-25
I would like to pass information to a query via a function in access.

For example,

Public Function fnGetClinicFromSwitch() As Variant
   
    Dim intStatus As Variant
   
   
    intStatus = fnGetClinicAllStatus()
   
   
    If intStatus = -1 Then
        fnGetClinicFromSwitch = ">0"
    Else
        fnGetClinicFromSwitch = Forms!frmReports.lstSelectClinic
    End If

End Function

I would like to place this function in the criteria of a query to return the ">0" as the query's  criteria.  It is not working.   I.e. intStatus = -1..

Any Help?
0
Comment
Question by:jamesdean666
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 18792713
Use the function in the query criteria:

WHERE fnGetClinicFromSwitch = somevalue

or

WHERE [SomeField] =fnGetClinicFromSwitch
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18792715
why not just use <> 0 in the criteria of your query

0
 

Author Comment

by:jamesdean666
ID: 18792752
For some reason.. when I place this function in my query, it works when the Else of the function is true..  I return an Id from the selection in a list on a form (frm_Reports) and my query runs fine - i.e. Id = 2.

When I don't select a specific id, i want to return all id's over 0 - hence the function.  The query is not understanding this as ">0" and returning zero records.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Glenn_Moore
ID: 18792767
<>0 or >0
0
 

Author Comment

by:jamesdean666
ID: 18792788
I know the syntax for >0.  It's a problem with the way that the query is reading the function.  It returns and integer, but won't return the string >0.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18792867
you may have to edit the query using the queryDef in VBA to pass that criteria to your query.

post the original SQL of your query.

dim qd as querydef, sSql
set qd=currentdb.querydefs("NameOFQuery")
ssql="Select....

'modify the ssql here using the function that you have

'assign the new sql for the query

dq.sql=ssql



0
 

Author Comment

by:jamesdean666
ID: 18792958
ugh..
I'm trying to not create a lot of code to do this..  Not sure if it's possible, but I would like to handle this through the function. If it doesn't work, then I am going to have to invest more time in the "querydefs" route and code my queries.

Anyone else have any idea on this one?

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18793011
you may try using True and False if that field is a boolean field?
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 18793021
Another possibility depending on how your query is used is to leave the criteria out of the query itself, and apply it when you need to open a report/form/etc ...

Modify your function like this:
Public Function fnGetClinicFromSwitch() As Variant
   
    Dim intStatus As Variant
   
   
    intStatus = fnGetClinicAllStatus()      
    If intStatus = -1 Then
        fnGetClinicFromSwitch = ">0"
    Else
        fnGetClinicFromSwitch = "= " &  Forms!frmReports.lstSelectClinic  '*** this line changed
    End If

End Function


ssql = "ID  = " &  fnGetClinicAllStatus
Docmd.openReport "YourRpt",acviewpreview,, ssql

You can modify the SQL for any object (listbox, combo, etc...) in a similar fashion
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18793033
try this

Public Function fnGetClinicFromSwitch() As Boolean
   
    Dim intStatus As Variant
   
   
    intStatus = fnGetClinicAllStatus()
   
   
    If intStatus = -1 Then
        fnGetClinicFromSwitch = True
    Else
        fnGetClinicFromSwitch = Forms!frmReports.lstSelectClinic
    End If

End Function
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18793035
Sorry... change this:
>ssql = "ID  = " &  fnGetClinicAllStatus

to this:
ssql = "ID " &  fnGetClinicAllStatus
0
 
LVL 39

Expert Comment

by:stevbe
ID: 18793109
sounds like a variation of the <All> being placed at the top of your combobox ...

so if nothing is selected you want all IDs greater than 0 ... do you have IDs that are less than 0? or is it that is nothing is selected you want to return all records?
0
 
LVL 39

Expert Comment

by:stevbe
ID: 18793147
if it is everything then make a hidden textbox on your form and make it's control source property be ...

=NZ([Form]![Combo0],"*")

change Combo0 to the name of your listbox

in your query use critria of ...

Where ID Like Forms!frmReportCriteria!txtID
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 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