View Full Version : Using "and" to link 2 criterias in VBA??? (probably easy)


vavroom
01-31-2002, 09:02 AM
I made a temporary search form and have two potential criterias, one is Contact Name, the other is Staff.

I used an unbound form with two combo boxes in it. I then used an option frame giving the choice to search by Contact Name, or by Staff. I used the wizards to get the proper string for each of those two criterias.

My problem is now to provide the option to search with both fields as criterias. I suspect my problem is one of using the appropriate quotation mark, apostrophe, whatever. I just cant' make it happen. Could you enlighten me as the proper way to join the following two bits with an "AND"?

stLinkCriteria = "[fldStaff]=" & Me![cmbStaff]

stLinkCriteria = "[fldPeopleID]=" & Me![cmbName]

I tried the following, but it's not working, as it's asking me for input on both Me![cmbName] and Me![cmbStaff]
stLinkCriteria = "'[fldPeopleID]=' & Me![cmbName] And '[fldStaff]=' & Me![cmbStaff]"

I searched in the Help files, but I can't make sense of what the proper order of things should be.

Jack Cowley
01-31-2002, 11:36 AM
Assuming both fields are text:

stLinkCriteria = "[fldPeopleID]= '" & Me![cmbName] & "' And [fldStaff]= '" & Me![cmbStaff]& "'"

vavroom
01-31-2002, 11:42 AM
Yes, both are text! Thank you :-)

Now, if we have a third field, that's a date...

stLinkCriteria = "[Date]=" & "#" & Me![txtDate] & "#"

How would I "mix n match"?

In the end, I want to be able to say:
name and staff
name and date
staff and date
name, staff and date

Thanks.

I must admit, I have yet to figure out when to use quotes, apostrophes, and the general syntax of these things... I can't seem to find anywhere it explains it to me like I'm a 2 year old :-)

Jack Cowley
01-31-2002, 11:45 AM
Before you go any further in the direction you are going I would suggest your implement this article...

http://support.microsoft.com/support/kb/articles/Q210/2/42.ASP?LN=EN-US&SD=gn&FR=0&qry=QBF&rnk=2&src=DHCS_MSPSS_gn_SRCH&SPR=ACC2000

vavroom
01-31-2002, 11:49 AM
Jack, thanks for pointing me to that article. I haven't got time to read it in depth just now, but I think it will be highly helpful.
Thanks a bunch :-)

Jack Cowley
01-31-2002, 11:55 AM
Take you time, but I think that this approach is much more flexible than the way that you are going....

Good luck!

Pat Hartman
02-01-2002, 05:35 AM
What no one pointed out specifically was what exactly was wrong with your initial code string. The answer to that was - the placement of the quotes was incorrect. You need to get the "And" inside the quote string since it is part of the literal value you want to pass to VBA and you need to get single quotes around the text variable values.

stLinkCriteria = "[fldPeopleID]='" & Me![cmbName] "' And [fldStaff]= '" & Me![cmbStaff] &"'"

You want to end up with something that looks like:

[fldPeopleId] = 'MyLastName' And [fldStaff] = 'YourName'

It is a good idea when having difficulty in creating this type of string to insert a break point or a debug.print statement in the code so that you can actually see the string you are sending.