Solved

Pass Function info As Query Criteria

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

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

735 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