Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Statment doesn't find existing record, when matching text field 1

Status
Not open for further replies.

ajharn

IS-IT--Management
Jun 8, 2002
71
0
0
US
Hi all,

I'm trying to run a simple check for an existing record to update, but for some reason my Select statement doesn't find the record with the matching text field in my Access table, even though it exists. If I switch over and use an integer field with the same select statement it finds the matching record no problem.

Is there some x factor to matching text fields that I am missing. I'm new to this so bear with me if I'm missing something obvious. Here's the code:
Code:
sSql = "SELECT * From DictationLog WHERE DictationLog.[OJobNumber]=" & sJobNum
Set rCurrRec = dLog.OpenRecordset(sSql)

The string variable sJobNum absolutely perfectly matches the OJobNumber (text, 9) field in the Access DB. It won't find a match even if I hard code the string in place of that variable.

However, if I switch the statement to evaluate against a different field, which is an Integer field and feed it a matching integer variable, it finds the match with no problem.

Any advice appreciated!

Thanks,
AJ

 
You need to use a delimiter for your data. Like this:

[tt]
sSql = "SELECT * From DictationLog WHERE DictationLog.[OJobNumber]=[!]'[/!]" & sJobNum & "[!]'[/!]"
[/tt]

Now... your query should work just fine unless your sJobNum contains a single quote. If it does, you'll get a different error. To handle this problem, you need to replace a single quote to 2 single-quotes, like this...

Code:
sSql = "SELECT * From DictationLog WHERE DictationLog.[OJobNumber]=[!]'[/!]" & [!]Replace([/!]sJobNum[!], "'", "''")[/!] & "[!]'[/!]"

Ok.... you might be thinking, "I can't have single quotes in my Job Number". And you may be right. But, I would still encourage you to use replace and make it a habit so that when you are dealing with other types of string data, you won't forget to do this.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Yep. That did the trick. I'll implement the Replace logic too. I'm with you on that one.

Thanks very much, George. Tek-Tips FTW.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top