473,405 Members | 2,167 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,405 software developers and data experts.

Detect Matching Record

52
Hi,

I have been trying to adapt this code for my database. I'm using MS Access 2007, and most front end users are in 2010.

The name of the underlying table is tblStudents, and the record source is qryMainDataEntry (which links two tables).
The Form is called frmMainDataEntry. The name of the field holding the data is called StudentName and it has the same name in the table and the form (I know it probably shouldn't).

The code I have tried to apply in the Form is this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub StudentName_BeforeUpdate(Cancel As Integer)
  2.  
  3. If DCount("StudentName]", "tblStudents", "[StudentName]= '" & Me![StudentName] & "'") > 0 Then
  4.   MsgBox "Name Is Already In Database!"
  5.  
  6. End If
  7. End Sub

I suspect the code is confused between the tables and forms, and have tried to differentiate them with tblStudents_StudentName and frmMainDataEntry_StudentName - but that isn't doing the trick.

The code in this thread seems to apply to data entry in the table, and maybe that is part of the issue?

I would like this code to just return a message that the student's name already exists in the database as soon as the user moves out of that field, not when the whole form has been completed. I need to allow duplicates.

Thank you in anticipation of your help. I hope I have provided enough clear information.

regards,
Marc

(Original hijacked thread - Want Access to notify me if a record already exists.)
Nov 9 '13 #1

✓ answered by NeoPa

A couple of small changes then. The return value of the MsgBox() is no longer required and the message doesn't need to prompt the operator to choose. Try this :
Expand|Select|Wrap|Line Numbers
  1. Private Sub StudentName_BeforeUpdate(Cancel As Integer)
  2.     Dim strTmp As String
  3.  
  4.     With Me.StudentName
  5.         strTmp = Replace("[StudentName]='%N'", "%N", .Value)
  6.         'If record not found then all is fine.  Nothing further to check.
  7.         If IsNull(DLookup(Expr:="[StudentName]" _
  8.                         , Domain:="[tblStudents]" _
  9.                         , Criteria:=strTmp)) Then Exit Sub
  10.         strTmp = Replace("Name (%N) already exists in database", "%N", .Value)
  11.         'Notify the operator of the clash.
  12.         Call MsgBox(Prompt:=strTmp _
  13.                   , Buttons:=vbOkOnly Or vbQuestion _
  14.                   , Title:=.Name)
  15.     End With
  16. End Sub

6 2150
NeoPa
32,556 Expert Mod 16PB
A very well presented question. These must not be added into existing threads though. I've sorted that for you.

Back to the question, which as I say is well presented and that always makes working on them a lot easier for us.

The code looks fundamentally fine to me. It's missing some logic, but should work assuming all the objects it relates to are correctly named. Can you say what actually happens? Just knowing it doesn't do exactly what you expect is not much to work with. What does it do?

Later, we'll sort out setting the Cancel value depending on the response from the user via the MsgBox() call, but let's get the basics sorted first.
Nov 9 '13 #2
NeoPa
32,556 Expert Mod 16PB
Right, I had to do something earlier so didn't quite finish looking into this.

It seems you were actually missing a "[" character from line #3 of your code. I have redone this for you, using DLookup() instead of DCount(), and expanded the MsgBox() part to do what you need. I guess you'll find this easy enough to review, but let me know if you have any questions.
Expand|Select|Wrap|Line Numbers
  1. Private Sub StudentName_BeforeUpdate(Cancel As Integer)
  2.     Dim strTmp As String
  3.  
  4.     With Me.StudentName
  5.         strTmp = Replace("[StudentName]='%N'", "%N", .Value)
  6.         'If record not found then all is fine.  Nothing further to check.
  7.         If IsNull(DLookup(Expr:="[StudentName]" _
  8.                         , Domain:="[tblStudents]" _
  9.                         , Criteria:=strTmp)) Then Exit Sub
  10.         strTmp = "Name (%N) already exists in database%L%LOK to continue anyway?"
  11.         strTmp = Replace(Replace(strTmp, "%N", .Value), "%L", vbNewLine)
  12.         'If the operator wants to continue in spite of the clash then exit.
  13.         If MsgBox(Prompt:=strTmp _
  14.                 , Buttons:=vbYesNo Or vbQuestion _
  15.                 , Title:=.Name) = vbNo Then Exit Sub
  16.         'Cancel this update.
  17.         Cancel = True
  18.     End With
  19. End Sub
Nov 9 '13 #3
mjvm
52
Hi NeoPa,

Thank you so much for your help. I'm glad my question was clear - I was a bit nervous, it's been a while since I have tried to do any code in my db. Thanks for the tip on starting a new question too.

Thank you for the code - it is picking up if a student name is already entered, so the field names are accurate; and does nothing if a new name is entered.

I would like to change the yes/no buttons to just one "OK" button that acknowledges the message and closes the box. The user then can decide whether to look for the duplicate.

I already have a button on the form labelled 'Check for this student' - which pulls up all records with that name combined with the date of birth so that the user can check for history - but they won't do it every time.

I'm not sure how to modify the code to make this happen?

I think I keep everything up to this point, but when I try to edit the buttons part, I start mucking up your good work :):

Expand|Select|Wrap|Line Numbers
  1. strTmp = "Name (%N) already exists in database%L%LOK to continue anyway?"
  2.         strTmp = Replace(Replace(strTmp, "%N", .Value), "%L", vbNewLine)
  3.         'If the operator wants to continue in spite of the clash then exit.
  4.         If MsgBox(Prompt:=strTmp _
  5.                 , Buttons:=vbYesNo Or vbQuestion _
  6.                 , Title:=.Name) = vbNo Then Exit Sub
  7.         'Cancel this update.
  8.         Cancel = True
  9.     End With
Thank you again for your time - I am most appreciative of your generosity.
Nov 10 '13 #4
NeoPa
32,556 Expert Mod 16PB
A couple of small changes then. The return value of the MsgBox() is no longer required and the message doesn't need to prompt the operator to choose. Try this :
Expand|Select|Wrap|Line Numbers
  1. Private Sub StudentName_BeforeUpdate(Cancel As Integer)
  2.     Dim strTmp As String
  3.  
  4.     With Me.StudentName
  5.         strTmp = Replace("[StudentName]='%N'", "%N", .Value)
  6.         'If record not found then all is fine.  Nothing further to check.
  7.         If IsNull(DLookup(Expr:="[StudentName]" _
  8.                         , Domain:="[tblStudents]" _
  9.                         , Criteria:=strTmp)) Then Exit Sub
  10.         strTmp = Replace("Name (%N) already exists in database", "%N", .Value)
  11.         'Notify the operator of the clash.
  12.         Call MsgBox(Prompt:=strTmp _
  13.                   , Buttons:=vbOkOnly Or vbQuestion _
  14.                   , Title:=.Name)
  15.     End With
  16. End Sub
Nov 10 '13 #5
mjvm
52
Thanks NeoPa - that is perfect.

Thanks so much for your help and support. I apologise for the delay in getting back to let you know it worked- I got caught up in so many other things!!!!

All the best,
marcella
Nov 13 '13 #6
NeoPa
32,556 Expert Mod 16PB
No worries :-)

I'm very pleased that was a help.

-Adrian.
Nov 14 '13 #7

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

Similar topics

5
by: yvan | last post by:
Approximately once a month, a client of ours sends us a bunch of comma-delimited text files which I have to clean up and then import into their MS SQL database. All last week, I was using a Cold...
1
by: David C. Barber | last post by:
I'm trying to determine if any matching records exist on a LIKE query performing a partial match of last names to a remote back-end database in the most efficient manner possible. LAN Traffic...
6
by: kaosyeti | last post by:
hey.... i have a duplicate record issue i could use some help with. on a form that has 2 unbound controls, txtboxyear and cboxmonth, together will automatically fill in an invisible txtboxdate...
1
by: zoro25 | last post by:
Hi, I'm pretty new to Access and I'm not too sure how to handle the following. There is an atual DB in access and I need to add a search tool to it. More exactly, we have a field name and a...
17
NeoPa
by: NeoPa | last post by:
I rarely use forms to manage my data so I expect this is easier than I found it (I hate it when OPs say that :D) I have a simple table (laid out below) with lists of customers for various jobs...
6
by: Lewis Wood | last post by:
I am self taught and struggling with a particular problem. I have created a database to record postcard details using Access 2000. My main screen (POSTCARD_FORM) shows details of the card and...
3
by: Phil Stanton | last post by:
I have a form based on a complex query (Lots of tables) If I delete a record, everything appears to be OK. Get the message "Youa are about to delete 1 record ....". I say yes. The record count...
1
by: Coll | last post by:
I'm working on a database that someone else created. I have a form - frm_main that is based on a query that is based on two tables. The join is such that the first table tbl_JCN has all records...
23
by: csolomon | last post by:
Hello: I have a form that allows me to add new records. In the onCurrent event, I have: Private Sub Form_Current() 'Requery Job Number Me.cbojobNumber.Requery 'Requery DM_Mix Combo...
19
by: slenish | last post by:
Hello, what i am trying to do is I have a form set up where you enter in 3 types of information a name, errcode and a number. What I want to do is is when you enter in the record and then hit the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.