Solved

Issue with Microsoft Access .FindFirst and Empty Strings

Posted on 2013-10-22
8
851 Views
Last Modified: 2013-10-23
I am trying to use the VBA .FindFirst method in Microsoft Access 2010. In the code shown below, the first "findVendor" line finds a match. The second, commented out line, does not find a match.
     In my table, both FirstNm and MidNm are null. I've converted the values to empty strings in strFirstNm and strMidNm. What could be different about the fields such that one returns a match and the other doesn't? Ideally, I'd like to check for all 3 names in one query, but for now am just trying to get MidNm to match correctly.
     Is there something I'm missing about using an equality check for empty strings? is there a better way to do this?
     I do have a unique ID, but in this case I can't check for it because I don't know it at the time this code is running.
     Thanks for any suggestions!
                                 

Set rsVendor = CurrentDb.OpenRecordset("tblVendor", dbOpenDynaset)
findVendor = "[LastNm] = '" & strLastNm & "' And [FirstNm] = '" & strFirstNm & "'" 'findVendor = "[LastNm] = '" & strLastNm & "' And [MidNm] = '" & strMidNm & "'"
With rsVendor
       .FindFirst (findVendor)
        If .NoMatch Then
                     ….Do Stuff
        End If
End With
rsVendor.Close
0
Comment
Question by:oneDayAtaTime
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39591957
A lot of people don't have middle names, and that field should be either NULL, or if you are certain that they have no middle name, then many systems use "NMN" to represent "No Middle Name".

Instead of setting rsVendor to reference the table, create a query (qryVendor) that converts [MidNm] using: NZ([MidNm], "NMN")

Then, in your find first, use something like:

'findVendor = "[LastNm] = '" & strLastNm & "' And [MidNm] = '" & NZ(strMidNm, "NMN") & "'"
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39591970
Keep it simple,
... does it work just for the First and Last name?
(I prefer the more segmented syntax)
   
    findVendor="[LastNm] =" & "'" & strLastNm & "'" & " And " & "[FirstNm] =" & "'" & strFirstNm & "'"

If not, then why bother adding them all into a bigger expression...
Make sense?

Perhaps another Expert can spot what I may be missing though


But again, I get something that works first, then build up from there.
This way you know exactly where the problem is...

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39591983
Sorry!

 Ignore my posts above  I thought that was one long expression
;-)
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39591992
I am sure fyed can get you sorted...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:hnasr
ID: 39592010
List few input records.
0
 

Author Comment

by:oneDayAtaTime
ID: 39592477
fyed,
     In the example I'm using (that's failing), the First Name and Middle Name are both null. I convert them to empty strings using the Nz function before I get to the .FindFirst. I did this because I know that nothing equals null, so I thought an empty string would be better.
     I'd rather not use a dummy value for middle name, because I grab these fields later and print them on a form as the person's name. If middle (or first) is blank, it doesn't print.
    I guess I'm just trying to figure out the difference - why a blank First Name works fine and finds a match, but a blank Middle Name does not. For example, say I have vendors named Smith (no first or last name), John Smith, and John H Smith. I want them all to resolve properly. Right now I don't seem to be able to use the middle name (if it's blank).
                      Joyce
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39592547
Because you may actually have a blank value in the field.  You cannot actually achieve this by typing a blank in a field, but if you write an action query (Insert or Append) query you can actually write a zero length string to a field.

See the query in the attached file.  In the first query (qry_Name), I tested for IsNull and [LastNm] = "".  You will see that the last name is NULL in record 2, but a ZLS (zero length string) in #3

"Right now I don't seem to be able to use the middle name (if it's blank). "

Actually, if it is blank (zero length string), then it should work, but FindFirst is not going to work with a NULL in that field.
Nulls-and-ZLS.accdb
0
 

Author Comment

by:oneDayAtaTime
ID: 39593681
Thanks, fyed! That was it! I was so focused on my syntax that I neglected to think about the logic underneath! Sigh....
     I performed an update query to make all the possible blanks in First Name/Middle Name into empty strings. Now I can use .FindFirst and it works regardless.
     Thanks for the advice and for the troubleshooting tip! Next time I have issues, I will know to check the fields to see exactly what I have.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now