Solved

Pass Function info As Query Criteria

Posted on 2007-03-26
13
160 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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