How to a function from a query

Is there a way to call a function from a query criteria , for example let assume we have this function below:

Function pBuildSQLWhereFormat(pvarFldValue As Variant) As String
    If Not IsDate(pvarFldValue) Then
        ' For example, some default value.
        pvarFldValue = Date
    pBuildSQLWhereFormat = "'" & Format(DateValue(pvarFldValue), "yyyy\/mm\/dd") & "'"
End Function

Below is a parameter query form

Between [Forms]![frmsalesorder].[txtstartdate] And  [Forms]![frmsalesorder].[txtstartEnddate]

What if I call the function above like below:

  pBuildSQLWhereFormat(Between [Forms]![frmsalesorder].[txtstartdate]) And pBuildSQLWhereFormat( [Forms]![frmsalesorder].[txtstartEnddate])

Am right or wrong????????????


Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Yes, you can call functions.   They must be declared public to do so.  

  As far as the statement, you were close.   But rather than pass the whole WHERE, you'd do the fields individually:

 Between  pBuildSQLWhereFormat([Forms]![frmsalesorder].[txtstartdate])   AND pBuildSQLWhereFormat( [Forms]![frmsalesorder].[txtstartEnddate])

A couple of notes when calling functions:

1. If the function has no parameters, it gets called once for the entire query.
2. If it has parameters, it gets called for each row.
3. If it is used in a criteria expression, it's called twice for each row.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertConsultingCommented:
Alas wrong, because "Between [Forms]![frmsalesorder].[txtstartdate]) And pBuildSQLWhereFormat( [Forms]![frmsalesorder].[txtstartEnddate]" isn't a valid VBA instruction.

Plus, if you give a valid instruction as parameter, the function will recieve its result (in your case, probably a boolean).

Ehence why you should avoid variant type as parameters (unless no other choice is possible).
Ganesh GuruduSenior ConsultantCommented:

--Then use below code to convert required format.

--you may need to check is the input is not null and valid date or not . or use exception block
--raise_application_error(-20001, :txtstartdate);

v_DATE_START := TO_DATE(:txtstartdate, 'YYYY/MM/DD');
v_DATE_END := TO_DATE(:txtstartEnddate, 'YYYY/MM/DD');

select * from table_name where column_name between v_DATE_START  and v_DATE_END ;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.