A very easy problem to solve....I know...Help (1 Viewer)

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
Hey guys I was wondering I have a search code and one string that says.
where = where & " AND [Productno] Like '" + Me![Productno] + "*" + "'"

How can I change this so that whatever the user inputs will be machted to any part of the field Productno, as it is now it just serches in the beginning of the field....PLease help me......very grateful for replies
 

Chris RR

Registered User.
Local time
Today, 02:11
Joined
Mar 2, 2000
Messages
354
Put an asterisk on both sides of the search field:

where = where & " AND [Productno] Like '*" + Me![Productno] + "*'"
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
It doesn't work, but thanks for the advice....whatcould be wrong?
 
K

Ken Grubb

Guest
I take it you're using a Function. Could you post a larger code snippet. I threw together this Query that works.

PARAMETERS EnterSearchCriteria Text;
SELECT tblProducts.Productno
FROM tblProducts
WHERE tblProducts.Productno Like "*" + EnterSearchCriteria + "*";

HTH,

Ken Grubb
Burlington, NC, USA
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:11
Joined
Feb 28, 2001
Messages
27,199
Try

where = where & " AND [Productno] like ""*" & Me![Productno] + "*"""

This would be the correct syntax if [Productno] is a text field rather than a number.
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
Here you go ´Ken

Private Sub btnRunQuery_Click()
On Error GoTo Err_btnRunQuery_Click


Dim MyDatabase As Database
Dim MyQueryDef As QueryDef
Dim where As Variant

Set MyDatabase = CurrentDb()
On Error Resume Next
' Delete the existing dynamic query; trap the error if the query does
' not exist.
'If ObjectExists("Queries", "qryDynamic_QBF") = True Then
MyDatabase.QueryDefs.Delete "qryDynamic_QBF"
MyDatabase.QueryDefs.Refresh
On Error GoTo 0

where = Null
'where = where & " AND [Productno]= '" + Me![Productno] + "'"
where = where & " AND [Productno] like ""*" & Me![Productno] + "*"""
'where = where & " AND [Denomination]= '" + Me![Denomination] + "'"
where = where & " AND [Denomination] Like '" + Me![Denomination] + "*" + "'"
'where = where & " AND [Supplier]= '" + Me![Supplier] + "'"
where = where & " AND [Supplier] Like '" + Me![Supplier] + "*" + "'"




Set MyQueryDef = MyDatabase.CreateQueryDef("qryDynamic_QBF", _
"Select ProductHyper,Alteration,Material,Denomination,Supplier,Date from Productnr1 " & (" where " + Mid(where, 6) & ";"))

DoCmd.OpenQuery "qryDynamic_QBF"


Exit_btnRunQuery_Click:
Exit Sub

Err_btnRunQuery_Click:
MsgBox Err.Description
Resume Exit_btnRunQuery_Click

End Sub



[This message has been edited by OliviaS (edited 08-10-2001).]
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
No, wait doc_mans code is working!!!!, but I forgot that my numbers(textfield productno) looks like this 400 62 00-00(space inbetween the digits) and if I type 62(I have to wright space then 62 and all the numbers containing "space 62" will come up). But I'm so glad that it finally works, Man y thanks to all you guys =)
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
hey guys I have one more thing that nobody has answered to and that is that I wish to add one searchfield to the form, could you say what to do just by looking at the code?........This is my last question promise =)
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
And one thing I have three fields but when I put doc_mans code in the other fields which by the way both are textfields I get a runtime error saying: " Run time error '3075'
Syntax error(missing operator)in query expression '[Productno]like"* AND[Denomination]like"*00*" AND[Supplier]like"*'

I have no Idea why it is not working, the productno search isn't working either...=(
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
it seems like it only works as long as you use the code on one field?....but the other two fields aren't working at all

[This message has been edited by OliviaS (edited 08-10-2001).]
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:11
Joined
Feb 28, 2001
Messages
27,199
Here is the basic principle.

When you are comparing two fields in a dynamic SELECT such as you are building, your issue will be that the quotes are going to be complex. Any numeric sequence that contains punctuation - other than leading sign, possible single embedded dot (as decimal point) or comma (decimal point if you are using the alternative number delimiters) - is not a number. It is a text string. You have to treat it as such.

When you build a "like" sequence, you can use wildcards such as "*" to represent arbitrary strings. However, with concatenation, you run into problems sometimes because of the need to get the dynamic field value into place in your SELECT clause.

The trick is to remember simple rules of thumb.

1. Inside a quoted string, to show a quote takes two quotes.

2. If the source of the search value is fixed in size, a "like" search by itself might not be right. Further number-muddling might be in order.

So here is a fragment based on stQry as the string in which I am building my SELECT clause based on the contents of an unbound text box, call it [LookFor], on a form as the source and [LookIn], a bound item, as the thing that holds what I wanted to find.


stQry = stQry & "AND [LookIn] LIKE ""*"
stQry = stQry & Trim$([LookFor])
stQry = stQry & "*"""

The first line adds in AND [LookIn] LIKE "*

The next line adds in a compressed version of the [LookFor] data.

The third line adds in *" to finish the line.

You are left with

AND [LookIn] LIKE "*value*"

The numeric fields are a bit easier. For these, you cannot do a "like" search but you could do other searches including a "between" search. Look that one up if you need it.

Let us take the same fields but make [LookIn] a LONG INTEGER. Let us say we want values equal to [LookFor], again a form's text box.

stQry = stQry & " AND [LookIn] = "
stQry = stQry & Trim$([LookFor])

You just added AND [LookIn] = number-string

Since [LookIn] is numeric, you don't need quotes. But the text box on the form is still a TEXT box, not a number box. The trick here is that SQL will detect a numeric field reference and will make the correct conversions at run-time on number-string.

Remember, if [LookIn] can ever contain ANY UNUSUAL PUNCTUATION AT ALL, it is not a number and cannot be searched as such.

The error you report is probably due to unbalancing your double-quotes. The way to test this is to hand-build a string following these rules:

Base rule: Quotes come in pairs. They GO in pairs, too. VBA gets upset when they don't.

1. Any time you concatenate with "XYZ", take off the leading and trailing quotes and insert the rest, which in this case would be XYZ.

2. Any time your remainder from step 1 contains the sequence "" (i.e. AFTER you have removed the outermost quotes), remove one of the embedded quote marks and store the other.

So stQry = """XYZ""" leaves stQry containing "XYZ"

and stQry = "XYZ" leaves stQry with XYZ

But stQry = ""XYZ"" leaves VBA guessing. (Count the quotes to see why.)

Olivia, please check pairings on your use of quotes. That is what the message is telling you. And I have never had success trying to use ' in place of " anytime.

And thanks for your kind words, but please limit your replies via e-mail. If you were to check my address, you would note it is part of the USA MILNET. It is OK for a short note and no harm is done, but long messages to me or anything that included a program fragment would probably trip every alarm in the house.

So I'm not saying don't mail me. But keep it brief and make any major communications through the forum.

Hope the above helps you, Olivia.
 

OliviaS

Registered User.
Local time
Today, 08:11
Joined
Jul 19, 2001
Messages
16
Thank you very much for your help...I appreciate it very ver much but oyu see dear friend I'm not a programmer I'm just a summerworker at a company and I therefor don't know how to program just a little bit, so don't take this the wrong way but I didn''t understand anything, I'm so stupid I know but really I thought this was going to be easy as you can see in my subject but it sounds so hard...couldn't I just exactly like your piece of code just paste it in behind all the fields....I'm so troubled I have no idea what to do....but you truly are great Mr...and I'm so sorry if I caused any trouble to you..very sorry....hope you are willing to hepl me just a bit more......I really tried to delete some quotes but it didn't work....so please help.......I'm really desperate.

Olivia Svensson from Sverige
 

Users who are viewing this thread

Top Bottom