473,466 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Prevent duplicate number (integer) entry before update

2 New Member
I am allocating a unique number to a patient and want to prevent duplicate entry before update
Dec 30 '12 #1

✓ answered by Seth Schrock

You would want this in the form's BeforeUpdate event:

Expand|Select|Wrap|Line Numbers
  1. If DCount(Expr:="*", Domain:="your form's record source", Criteria:="UniqueNumberField=" & Me.UniqueNumberField) <> 0 Then
  2.      Cancel = True
  3.      MsgBox "Number already exists"
  4. End If

4 3544
Seth Schrock
2,965 Recognized Expert Specialist
You would want this in the form's BeforeUpdate event:

Expand|Select|Wrap|Line Numbers
  1. If DCount(Expr:="*", Domain:="your form's record source", Criteria:="UniqueNumberField=" & Me.UniqueNumberField) <> 0 Then
  2.      Cancel = True
  3.      MsgBox "Number already exists"
  4. End If
Dec 30 '12 #2
waqasi
2 New Member
Thank you very much it is working. I was using bit more complex code and giving error message type missmatched, code is as follow

Expand|Select|Wrap|Line Numbers
  1. Private Sub hospitalno_BeforeUpdate(Cancel As Integer)
  2.  
  3.     Dim SID As Long
  4.     Dim stLinkCriteria As Long
  5.     Dim rsc As DAO.Recordset
  6.  
  7.     Set rsc = Me.RecordsetClone
  8.  
  9.     SID = Me.hospitalno.Value
  10.     stLinkCriteria = "[hospitalno]=" & "'" & SID & "'"
  11.  
  12.     'Check patientdetails table for duplicate Hospital Number
  13.     If DCount("hospitalno", "tablepatientdetails", _
  14.               stLinkCriteria) > 0 Then
  15.         'Undo duplicate entry
  16.         Me.Undo
  17.         'Message box warning of duplication
  18.         MsgBox "Warning Hospital Number " _
  19.              & SID & " has already been entered." _
  20.              & vbCr & vbCr & "You will now been taken to the record.", _
  21.                vbInformation, "Duplicate Information"
  22.         'Go to record of original Hospital Number
  23.         rsc.FindFirst stLinkCriteria
  24.         Me.Bookmark = rsc.Bookmark
  25.     End If
  26.  
  27.     Set rsc = Nothing
  28. End Sub
Dec 30 '12 #3
Seth Schrock
2,965 Recognized Expert Specialist
Glad it helped. If you would select my answer as the best answer, I would appreciate it.

In future questions, it would be helpful for those of us who are trying to help you if you would give what you have tried along with any error messages (exact number and text) and the line of code that is selected when you hit debug on the error. Also, when you are posting code (VBA or SQL), please place your code in the code brackets. The button that looks like <CODE/> ( to the right of the bold, italics, underline buttons and to the left of the undo and redo buttons) will place the code tags in your post for you.
Dec 30 '12 #4
zmbd
5,501 Recognized Expert Moderator Expert
waqasi:

A few things to mention:

(-) Seth was kind enough to provide you with a coded answer; however, Bytes is not a code nor homework service. Please read the FAQ and posting guidelines before re-posting your question.

(-) Post #3: As Seth pointed out, but let me emphasize, that posting comments such as "it doesn't work" or "there's an error" does not help us help you. Instead we need the specifics as to what you expected and what happened. In the case an error, you must post the EXACT title, error message, and the error number. We must have all three parts as this help us to determine if the error is from the OS or the program.

(-) We also ask that you do the basic trouble shooting on your system. We can not perform such as our systems simply are not yours.
You should have both options set.
You should compile your code.

(-) I have also deleted a post that directly asked for code which is not normally allowed. Please refer the site FAQ and the forum sticky posting guidelines.
Dec 31 '12 #5

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

Similar topics

0
by: mmazid | last post by:
I would like to know how to prevent entering the same data as a primary key to an MS Access database and return with an error message confirming the data already exists in ASP.Net (VB.Net). ...
19
by: eric.nave | last post by:
this is a slight change to a fequently asked question around here. I have a table which contains a "sortorder" column where a user can specify some arbitrary order for records to be displayed in. ...
11
by: ariel81 | last post by:
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the...
2
by: eazyone | last post by:
I can prevent a duplicate ID from being entered, but can't figure out how to go to that specific record. I keep getting an error message on: Me.Bookmark = rsc.Bookmark Its not opening and...
6
by: Arthur Dent | last post by:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the same event handler on an event? e.g... I have some object which raises an event, and some other class which consumes...
6
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: CODE <?php $c=OCILogon("scott", "tiger",...
1
by: chicago1985 | last post by:
I have a unique constraint in my Oracle table for 3 fields. If I enter duplicate info on the table using Oracle client I will get an Ora message error ORA-00001 that tells me it is a duplicate entry...
0
by: jehrich | last post by:
Hi Everyone, I am a bit of a hobby programmer (read newbie), and I have been searching for a solution to a SQL problem for a recent pet project. I discovered that there are a number of brilliant...
4
by: neelsfer | last post by:
Hi This code worked until i changed the "RaceNo" file to text from number format. What brackets or code should i change to make it work? This is to prevent duplicate entries. thx for helping...
3
by: jacc14 | last post by:
Hi When I enter a job number i have set up the following Dim PID As String Dim stLinkCriteria As String Dim rsc As DAO.Recordset Set rsc = Me.RecordsetClone
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.