VBA Syntex for Like in DCount

Hello,

I need some help in the syntex for the following:  

If DCount("*", "MGNameAddressPhone", "Like '*" & Me.txtSearch & "'") = 0 Then

Else

DoCmd.ApplyFilter "", "MGNameAddressPhone", Like ""*"" & me.txtSearch & ""*""", ""
Ernest GroggSecurity Management InfoSecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
That should work fine as long as no one types a ' or " in the txtSearchControl.

Only thing is you might want:

DCount("*", "MGNameAddressPhone", "Like '*" & Me.txtSearch & "*'")

so if the search string appears anywhere in a field, you get a value.

Jim.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
BTW, when your getting syntax errors, a good thing to do is this:

 Dim strWhere as string

 strWhere = "Like '*" & Me.txtSearch & "*'"
 If DCount("*", "MGNameAddressPhone", strWhere)

You can then put a break point on the DCount() and inspect the string that's getting passed before it executes.   Really helps to see the finial result and makes it easy to spot mistakes.

Jim.
0
Ernest GroggSecurity Management InfoSecAuthor Commented:
hmmm...it tells me that I am missing an operator in the expression 'Like '*mysearchstring*'
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ernest GroggSecurity Management InfoSecAuthor Commented:
sorry i didn't see the second post...let me see this...
0
Ernest GroggSecurity Management InfoSecAuthor Commented:
yeah in the Like portion.
0
Rey Obrero (Capricorn1)Commented:
what is the value of "Me.txtSearch"

you need in the criteria section of dcount() the name of the field

"[fieldName] like  '*" & Me.txtSearch & "*'"


DCount("*", "MGNameAddressPhone","[fieldName] like  '*" & Me.txtSearch & "*'")
0
mbizupCommented:
In addition to Jim's comments, Your criteria is incomplete:

If DCount("*", "MGNameAddressPhone", "Like '*" & Me.txtSearch & "'") = 0 Then


You need to specify the field that you are applying the criteria to.  The syntax should be something like this:

If DCount("*", "MyTableName", "MyFieldName Like '*" & Me.txtSearch & "'") = 0 Then
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Good gosh....looks like I need another cup of coffee; totally missed the field not being there!

Jim.
0
Ernest GroggSecurity Management InfoSecAuthor Commented:
Ahh...I see..Yeah...is there a way to search every field instead of just one field?  that is what I was trying to accomplish.

Thanks!
0
Rey Obrero (Capricorn1)Commented:
yes there is..
you have to build your search string using vba.. something like this

dim strSearch as string

strSearch="[Field1] Like '*" & me.txt1 & "' AND [field2] Like '*" & me.text2 & "*'"  

then use it in dcount

DCount("*", "MGNameAddressPhone", strSearch)


.
0

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
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
No, there is no easy way to do that.  With the criteria argument, you must  name the fields individually.

Jim.
0
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.