Solved

Pass Function info As Query Criteria

Posted on 2007-03-26
13
161 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 119

Expert Comment

by:Rey Obrero
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
 
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 119

Expert Comment

by:Rey Obrero
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 119

Expert Comment

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

Accepted Solution

by:
mbizup earned 500 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 119

Expert Comment

by:Rey Obrero
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now