473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

FindFirst with AND not working on single record

18
I'm using Access 2003 with MS XP.

Have table/records with FirstName (text field), LastName (text field), plus other fields. User enters FirstName, LastName and other info in form. Before updating table based on form input, I check to see if a record already exists for a person with that FirstName and LastName. If so, I give an error message and don't let the record be created. Pertinent part of form's BeforeUpdate code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate (Cancel as Integer)
  2. Dim FN As String
  3. Dim LN As String
  4. Dim rsc As DAO.Recordset
  5. Set rsc=Me.RecordsetClone
  6. FN=Me.FirstName
  7. LN=Me.LastName
  8. rsc.FindFirst "[LastName] = 'LN' And [FirstName] = 'FN'"
  9. If Not rsc.EOF Then
  10.   Me.Undo
  11.   MsgBox "This person has already been entered."
  12.   rsc.Close
  13.   Set rsc=Nothing
  14.   Exit Sub
  15. End If
  16. End Sub
Table contains following records (only FirstName and LastName shown)

Jane Doe
John Smith
Sally White
Sam Johnson

case 1) If user enters Frank Jones, code works (i.e., allows record to be created).

case 2) If user enters Sally White, code works (i.e., gives error "This person has already been entered." and doesn't create record.

case 3) If user enters Sam Doe, code doesn't work (i.e., gives error "This person has already been entered." and doesn't create record.

In case 3) the code apparently finds Sam as FirstName in one record and Doe as LastName in a different record and decides it meets the criteria.

How do I write the code so FirstName and LastName must match in the same record before meeting the criteria (and generating the error message)?

Thanks for the help.

Janice
Sep 17 '09 #1

✓ answered by Megalog

@Megalog
Sorry, line 10 above should be:

Expand|Select|Wrap|Line Numbers
  1. rsc.FindFirst "[LastName]= '" & LN & "' And [FirstName] = '" & FN & "'" 

6 2655
missinglinq
3,532 Expert 2GB
Try replacing

Expand|Select|Wrap|Line Numbers
  1. rsc.FindFirst "[LastName] = 'LN' And [FirstName] = 'FN'"
with
Expand|Select|Wrap|Line Numbers
  1. rsc.FindFirst "[LastName]= '" & Me.LastName & "' And [FirstName] = '" & Me.FirstName & "'"
Linq ;0)>
Sep 17 '09 #2
JHite
18
Ok. I tried your suggestion, but got the very same result as with my code. Case 3) still doesn't work correctly.

Are there any more suggestions?

Is there a better way to find duplication between records than using FindFirst?

Thanks again.

Janice
Sep 17 '09 #3
ajalwaysus
266 Expert 100+
I may be over simplifying this but try this..

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate (Cancel as Integer)
  2.  
  3.   if nz(DLookUp("FirstName", "Table1", "FirstName = '" & me.FirstName & "' AND LastName = '" & me.LastName.value & "'"), "") <> "" Then
  4.        MsgBox "This person has already been entered."
  5.        Cancel = True
  6.   End If
  7.  
  8. End Sub
  9.  
This is a quick and dirty way, I will post another idea when I have more time.

I also must express my concern of using First name Last Name as a unique identifier, plenty of people have the same first and last name.

-AJ
Sep 17 '09 #4
Megalog
378 Expert 256MB
The reason the code isnt working is because you're not testing for the find results, you're simply looking to see if you're at EOF. Nothing is causing the recordset to advance, so you end up staying on the first record.

Try this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Dim FN As String
  3.     Dim LN As String
  4.     Dim rsc As DAO.Recordset
  5.     Set rsc = Me.RecordsetClone
  6.  
  7.     FN = Nz(Me.FirstName, "Null")
  8.     LN = Nz(Me.LastName, "Null")
  9.  
  10.     rsc.FindFirst "[LastName]= '" & Me.LastName & "' And [FirstName] = '" & Me.FirstName & "'" 
  11.  
  12.     If Not rsc.NoMatch Then
  13.         Me.Undo
  14.         MsgBox "This person has already been entered."
  15.     End If
  16.  
  17.     Set rsc = Nothing
  18. End Sub
Sep 17 '09 #5
Megalog
378 Expert 256MB
@Megalog
Sorry, line 10 above should be:

Expand|Select|Wrap|Line Numbers
  1. rsc.FindFirst "[LastName]= '" & LN & "' And [FirstName] = '" & FN & "'" 
Sep 17 '09 #6
JHite
18
Thanks to all. I used Megalog's (corrected) code and it worked. Thanks for explaining about the EOF check. I didn't think of that in relationship to FindFirst.

In response to AJ's comment, I realize two different people can have the same first and last name. This is a small DB for use at my church and I will include instructions on how to delineate two people with the same first and last names (e.g., adding a middle initial to one of the first names or using a nickname for one of the first names).

Thanks again to all.

Janice
Sep 17 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike | last post by:
Hi I've written a module to find the first occurrence of a record in a query (that is my forms datasource) that matches four specified criteria. No matter what I try it doesn't seem to find ...
10
by: Steve Jorgensen | last post by:
Microsoft acknowledges a bug with this, but the bug is worse than they say. It turns out that if you use the Like operator without a wildcard on a snapshot type recordset, all sorts of wierd,...
1
by: cnlai | last post by:
Snippets -------- Dim mydb As Database, myset As DAO.Recordset Set mydb = CurrentDb Set myset = mydb.OpenRecordset("abc") myset.FindFirst (" = 'S02'") '<< problem here Reference: MS DAO...
4
by: psydork | last post by:
My first time to this group so please be kind. Anyway I have this questionnaire that has two parts (2 tables). I created a form for each of the tables and I am trying to make it so that when I...
2
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong)...
0
by: JC | last post by:
I have a list box loaded with Student Name, Social Security. When the user click on a specific record, the following code is assigned to the lstStudent1_AfterUpdate procedure. Private Sub...
3
by: wvhines | last post by:
Hello: I am an ambitious novice...I have a main form "frmGeneralContracting" that has a subform on it called "frmsubDeliverables" and I am trying to restrict editing if the current user's login...
2
by: Denise | last post by:
Front end is Access 2002, back end is linked Oracle tables. My users need to describe things in feet and inches and want to use the standard ' and " abbrevations. On a testing form I go to a...
25
by: Rick Collard | last post by:
Using DAO 3.6 on an Access 2002 database, I'm getting unexpected results with the FindFirst method. Here's the simple code to test: Public Sub FindIt() Dim db As Database, rs As Recordset...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.