Solved

Pass Function info As Query Criteria

Posted on 2007-03-26
13
163 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

813 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

11 Experts available now in Live!

Get 1:1 Help Now