Search Box in form gives error

M

mark.godecke

Hello,

I am an Access beginner trying to create a form to search a database.
Users enter the client's last name, and then a second form called
"Contacts" will open with their information. I got that working easily
enough with the wizard, but I would like the users to be able to enter
just the first part of a client's name, instead of having to match it
exactly.

This is what I have so far, based on other posts to this forum.

stDocName = "Contacts"

stLinkCriteria = "[LastName]=" & Me![SearchBox] & "*"""
DoCmd.OpenForm stDocName, , , stLinkCriteria

When I try and use the button this is attached to, I get the error
"Syntax Error in String in Query expression '[LastName]=SMITH*"" Where
SMITH is can be any name I enter in the Search Box.

I have also tried the same code substituting "LIKE" instead of "="
after stLinkCriteria, but then I get a "Type mismatch" error. Can
someone tell me what I'm doing wrong?

Thank you in advance for your assistance.
 
J

Jeff L

Since your Last Name is a string, you need to have single quotes around
it. So it would be:
stLinkCriteria = "[LastName] = '" & Me![SearchBox] & "'"
or if you want to do the like statement
stLinkCriteria = "[LastName] Like '" & Me![SearchBox] & "*'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top