473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Query criteria as function

SELECT * FROM tblLabor WHERE tblLabor.UserGroup=SetUserGroupCriteria()

I have something similar to this in a query that I'm using. I want SetUserGroupCriteria() to return something along the lines of
[Like "CA*" and Not like "RN*"] Unfortunately SetUserGroupCriteria() returns a string that looks like ["Like "CA*" and Not like "RN*""], and the keyword Like gets thrown in front of this whole mess automatically, thus making it say:
Like "Like "CA*" and Not like "RN*"". Needless to say, this is not desirable. Is there anything I can do to correct his?
Nov 2 '06 #1
20 5033
VALIS
21
SELECT * FROM tblLabor WHERE tblLabor.UserGroup=SetUserGroupCriteria()

I have something similar to this in a query that I'm using. I want SetUserGroupCriteria() to return something along the lines of
[Like "CA*" and Not like "RN*"] Unfortunately SetUserGroupCriteria() returns a string that looks like ["Like "CA*" and Not like "RN*""], and the keyword Like gets thrown in front of this whole mess automatically, thus making it say:
Like "Like "CA*" and Not like "RN*"". Needless to say, this is not desirable. Is there anything I can do to correct his?
Hi Sandro997,
Is SetUserGroupCriteria used anywhere else?
If it isn't it might easier to refine this than mess around re-formatting the text string. Could you paste the code for the function SetUserGroupCriteria()?

Thx
Nov 2 '06 #2
NeoPa
32,556 Expert Mod 16PB
If you post the code for SetUserGroupCriteria()
then we can tell you.
It will propably be reasonably straightforward.
Nov 2 '06 #3
Hi Sandro997,
Is SetUserGroupCriteria used anywhere else?
If it isn't it might easier to refine this than mess around re-formatting the text string. Could you paste the code for the function SetUserGroupCriteria()?

Thx
Ya, this function is used all over the place.

Public Function SetUserGroupCriteria() As String
Select Case GetUserGroupByUserID(fGetUserID())
Case "UserType1" SetUserGroupLikeCriteria = "Not like ""CA*"" and Not like ""RN*"""
Case "UserType2" SetUserGroupLikeCriteria = "Not like ""LS*"" and Not like ""FM*"""
Case "UserType3" SetUserGroupLikeCriteria = "Not like ""FM*"""
Case "UserType4" SetUserGroupLikeCriteria = "Like ""*"""
Case "UserType5" SetUserGroupLikeCriteria = "Like ""CA*"" and Like ""FM*"""
End Select
End function

There is a fair bit of logic in there
Hope this helps
Nov 2 '06 #4
NeoPa
32,556 Expert Mod 16PB
The format you are looking for will only work from within design view of a query.
In SQL you must use something like :-
Expand|Select|Wrap|Line Numbers
  1. WHERE [Field] Not Like 'CA*' And [Field] Not Like 'RN*'
Here is your function recoded, but you will need to adjust the calling code with this in mind.
Expand|Select|Wrap|Line Numbers
  1. Public Function SetUserGroupCriteria(strField as String) As String
  2.     Select Case GetUserGroupByUserID(fGetUserID())
  3.     Case "UserType1"
  4.         SetUserGroupLikeCriteria = "([" & strField & "] Not Like 'CA*' And [" & strField & "] Not like 'RN*')"
  5.     Case "UserType2"
  6.         SetUserGroupLikeCriteria = "([" & strField & "] Not Like 'LS*' And [" & strField & "] Not like 'FM*')"
  7.     Case "UserType3"
  8.         SetUserGroupLikeCriteria = "([" & strField & "] Not Like 'FM*')"
  9.     Case "UserType4"
  10.         SetUserGroupLikeCriteria = "([" & strField & "] Like '*'"
  11.     Case "UserType5"
  12.         SetUserGroupLikeCriteria = "([" & strField & "] Like 'CA*' And [" & strField & "] like 'FM*')"
  13.     End Select
  14. End function
Nov 2 '06 #5
It is the case that the query is in design view. I simply represented the SQL equivalent, since text is easier to display.
Nov 2 '06 #6
I've modified the SQL to something akin to:
SELECT * FROM tblLabor WHERE SetUserGroupCriteria()
and SetUserGroupCriteria() returns "([UserGroup] Like 'skizzle*')" This is for testing purposes, there is no row in the db where this should match. Unfortunately, this returns every record in the table. If I replace it with
SELECT * FROM tblLabor WHERE ([UserGroup] Like 'skizzle*'), then it is fine(ie. returns empty set). This kinda baffles me. I think it is relevant to note that this is an access query, as in it is saved as qryGetLaborStuff, and I am working the SQL through this medium, if I convert to design mode this turns into
Field: SetUserGroupCriteria()
Criteria: <> False
Nov 2 '06 #7
NeoPa
32,556 Expert Mod 16PB
Very strange!
Could you post the code that builds up the SQL string.
Also, can you post the actual SQL string that you attempt to execute.
(Don't forget to TAG around the code - It's easier to work with if not 'doctored' by the web page parser.)
Nov 2 '06 #8
Hmm, could you clarify your request? The code that builds the SQL string...my SQL string exists in an access query. I'm not certain if there is a misunderstanding here, but an access query does not consist of a coding language. It is a GUI for creating queries. The actual string is static, not dynamic, within this query 'file'. The string I am literally using is
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblLabor WHERE SetUserGroupCriteria()
Unfortunately this query never exists in code, such as the VBA language...
Nov 2 '06 #9
Sorry, repost, edit is disabled.

Hmm, could you clarify your request? The code that builds the SQL string...my SQL string exists in an access query. I'm not certain if there is a misunderstanding here, but an access query does not consist of a coding language. It is a GUI for creating queries. The actual string is static, not dynamic, within this query 'file'. The string I am literally using is
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblLabor WHERE SetUserGroupCriteria()
  2. it should be equivalent to
  3. SELECT * FROM tblLabor WHERE ([UserGroup] Like 'skizzle*')
  4. What it is equivalent to is
  5. SELECT * FROM tblLabor WHERE (([UserGroup] Like 'skizzle*') <> False)
  6.  
I think...

Unfortunately this query never exists in code, such as the VBA language...
Nov 2 '06 #10
NeoPa
32,556 Expert Mod 16PB
So, you run the query natively?
We're not talking about building up the SQL in code then executing it with a DoCmd.RunSQL type command?
In that case I can work with your last post.
Nov 2 '06 #11
That is correct, there is no DoCmd.RunSQL command, it is native. It's actually the record source of a combobox, in a saved query.

Question, have you been able to reproduce my problem on your own machine?
Nov 2 '06 #12
NeoPa
32,556 Expert Mod 16PB
[quote=Sandro997]Question, have you been able to reproduce my problem on your own machine?/QUOTE]
No, sorry.
I've only just got the info & have life :-(
Nov 2 '06 #13
I'm using Access 2003...are you using the same version?
Nov 3 '06 #14
NeoPa
32,556 Expert Mod 16PB
I'm using Access 2003...are you using the same version?
2K @work & 2K3 @home.
Do you have any reason to believe it will make a difference?
I actually do most of my working in my head so it shouldn't make too much difference ;)
Nov 3 '06 #15
I'm just trying to work out why you can't reproduce my problem. I'm thinking it's a bug in version 2003. Try following these steps.

1. Create query: save as qryTheTest
2. In SQL view of this query type: SELECT * FROM [some non-empty table you have] WHERE SetUserGroupCriteria()
3. Create a Function in one of your modules called SetUserGroupCriteria()
4. Populate that function with:
Expand|Select|Wrap|Line Numbers
  1. Public Function SetUserGroupCriteria() As String
    SetUserGroupLikeCriteria = "[some valid string field in your table] Like ""NONEXISTENTSTRING"""
    End function
5. Place a breakpoint in that function to confirm it is being called, and is returning: "[Field] Like "NONEXISTENTSTRING""
6. In qryTheTest activate datasheet view
7. I predict you will get a list of every record in the table you specified, when you should have received an empty set
Nov 3 '06 #16
NeoPa
32,556 Expert Mod 16PB
This is a generic problem anyway.
Now I understand what you're trying to do I know where it's at.
When you say 'WHERE Function()' in a query, it does not, as you expect, resolve the function into a part of the SQL.
Instead, it gets the return of the function and tries to resolve that to a boolean (TRUE or FALSE).

That's why all the questions earlier about the code - this concept can work in code ONLY.

Within a query, as you have it, your only option, as far as I see it, is to write a function that returns TRUE or FALSE for every record (as the query is called per record line of the dataset (In VBA you can call the function once and add the result into the string that LATER is executed as SQL instructions).
Nov 3 '06 #17
Ahh ya, that's what I thought. I was just really hoping that there was some workaround. Thanks for your effort though.
Nov 3 '06 #18
NeoPa
32,556 Expert Mod 16PB
No problem Sandro.
It's nice dealing with someone who's able to understand what you're trying to say ;).
Nov 3 '06 #19
Eh? Are you saying you could understand me or vice versa?...or both?
Nov 3 '06 #20
NeoPa
32,556 Expert Mod 16PB
Eh? Are you saying you could understand me or vice versa?...or both?
You seemed able to make intelligent replies to my posts - I'd gathered that you understood most of what I was saying.
I think I understood most of what you were saying so that's a 'Both'.
Everyone should be happy :)
Nov 3 '06 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reggie | last post by:
Hi and TIA, I have the criteria for one of my fields set to the return value of this function. When I view the result in the immediate window it appears to be the exact value I would use if I...
5
by: Steve | last post by:
I need help with a function to be used as the criteria for a query. The field in the query is InventoryStatus: - . The function is built around a Select Case set of about twelve cases. Function...
1
by: KLAU | last post by:
I have a field that retrieves information from an expression in a query. I have used a DLookup function to get the calculated field from the query. However, the relationship is 1-to-many so one...
6
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3...
4
by: paii, Ron | last post by:
I am trying to use the following "Elookup" function on a query that gets 2 parameters from an open form. ?eLookup("", "qryWOLPCMatEst") Returns the following error. Too few parameters....
4
by: Judy | last post by:
I'm using Access 2003 and was wondering if it is possible to have a paramater selection within a crosstab query so that I wouldn't need to build a new table. I have a select query that I'm using...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
10
by: Daniel | last post by:
In Microsoft Access I can write a query that includes the criteria: Between Date()-7 And Date() to retrieve the records from a table that fall within the last week. I'm trying to write a...
11
by: Purdue02 | last post by:
I am trying to pass a global variable to criteria in a query using the below code, but the query is returning no results. I have the function ReturnStrCriteria() included in the query's criteria....
17
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.