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

Check if Record already exists and prevent duplicated Record

5
>>Split from: How to verify if record already exist in another table <<

Thanks ZMBD!

Sorry for the delayed response. Building the string first worked like a champ.

Now that this part is working, any advise on how to stop the record from being added when this condition is not met. As it is now I get the message box, but the record still gets added to the table.

Justin


Thanks ZMBD, That works but only for the first record. After that it acts the same as when it was in the after update event.

What would cause this?
Feb 9 '16 #1
4 4257
zmbd
5,501 Expert Mod 4TB
basically, move this check to the before_update event. :)
BeforeUpdate Event [Access 2003 VBA Language Reference] old link; however, the method hasn't changed. :O)
See if the record is present, if so, then cancel=true and me.undo.

This can be done at the form (Form.BeforeUpdate Event (Access2013)), record, control levels.
Feb 9 '16 #2
zmbd
5,501 Expert Mod 4TB
Sorry,
Should have said to use the "BeforeInsert" method.
This way, before the record is even inserted into the table the code should run, check for prior records, set CANCEL=True to cancel the insert. Follow the above link, very short example at the bottom of the page and the page has a decent explanation of the event.
Feb 10 '16 #3
TXNEWT
5
This sounds like its exactly what I need, but I don't see the BeforeInsert as an option in the property list for my test box?

Also, I am a bit confused on put all the code together.

Here is what I currently have, Can you help?

Expand|Select|Wrap|Line Numbers
  1. Private Sub UNITID_BeforeUpdate(Cancel As Integer)
  2. Dim tmpBID As String
  3.  
  4. zstr = "UNITID= '" & Me!UNITID & "'"
  5.  
  6. tmpBID = Nz(DLookup("UNITID", "BARCODESUB", zstr), " ")
  7.  
  8.  
  9.    If tmpBID = " " Then
  10.       MsgBox "This Serial# does not exist in proir production manifest!", vbDefaultButton1
  11.  
  12.  
  13.  
  14.    End If
  15.  
  16. End Sub
Feb 10 '16 #4
zmbd
5,501 Expert Mod 4TB
+ The before insert is a form level event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer) 
+ Just thought of a snag here, if the primary key is an autonumber field, then the Before_Insert event will
trigger before we get the data entered into the control.
- The autonumber field is going to increment as soon as the insert event succeeds - and will trigger the event before you have a chance to enter anything into the other fields. AFAIK, there's no way to stop this outside of the form_before_insert event - and that's the snag.


+ Let's go back to your code in the control's before_update event. You have about 95% of it done.

Expand|Select|Wrap|Line Numbers
  1. Private Sub UNITID_BeforeUpdate(Cancel As Integer)
  2.     Dim zStr As String
  3. On Error GoTo zerror
  4.     zStr = "UNITID='" & Me!UNITID & "'"
  5.     '
  6.     If (DLookup("UNITID", "BARCODESUB", zStr) & "") = "" Then
  7.         MsgBox "This Serial# does not exist in proir production manifest!"
  8.         Me.Undo
  9.         Cancel = True
  10.     End If
  11. zerrrtrn
  12.     Exit Sub
  13. zerror:
  14.     MsgBox Prompt:="ErrS: " & Err.Source & vbCrLf & "ErrN: " & Err.Number & vbCrLf & "ErrD: " & Err.Description
  15. Resume zerrrtrn
  16. End Sub
This should stop any record inserts for the missing serial number. The above is a melding of code I already use in a production database (I check to see if the item has already been entered into the tracking database) and your code. This stops the insert; however, keep in mind that the autonumber will increment and is one reason why the autonumber shouldn't be used for anything meaningful outside of being a unique record identifier.

I also suggest that if you haven't done so, you set the option explicit as shown in the trouble shooting section of > Before Posting (VBA or SQL) Code:
<alt><F11> to open the VBA editor...
Menu>Tools>Options>
>Editor tab, UNCHECK "auto Syntax Check" you're vba code will still highlight if in error, just no annoying message boxes while you type!
>> Check mark the "Require Variable Declaration" - you will need to explictly declare variables used in the code.
*Everything else on this tab check mark*, I prefer the tab step to be either 2 or 4 spaces.
>The other tabs' default values are fine for most people.

This will help you with trouble shooting in the future :)
Feb 11 '16 #5

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

Similar topics

4
by: Jim in Arizona | last post by:
I'm wanting to do a simple controlled voting page. I too our webserver off anonymous and everyone who accesses the website is a domain authenticated user. I've already done some control structure...
11
by: simonmarkjones | last post by:
Hello there! I'm Still pretty new to access only been using it for a couple of days. If a record has been partially filled out or even completly filled out and then the form is closed or the...
10
by: Geoff Jones | last post by:
Hi I'm trying to drop a table by using: Dim cmd As New OleDbCommand("DROP TABLE IF EXISTS books", myconnection) cmd.ExecuteNonQuery() but I get a syntax error: "Syntax error in DROP TABLE...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
2
by: kkleung89 | last post by:
Basically, here's what's happening with the program. I have a table of Customers and a table of Pets, with the latter containing a field linking it to its customer of ownership. I have a form...
59
oll3i
by: oll3i | last post by:
how to check if an object already exists and return reference to that object
6
by: deejow | last post by:
Hi, I want Access to return an error message that says an identical entry already exists. I do not want to set the field to 'no duplicates' because it is a name field and it is possible to have...
0
by: skavinkar | last post by:
While installing application i want to prevent overwriting my DB file if it already exists at specified location. As i am new to using Installshield could any please guide. Tried setting Never...
2
by: madhaviS | last post by:
Hiii, I have data like a sbelow. emp_id name marks status 1 raga 23 null 1 raga 23 null There is no primary key for the table. Now i want the result like
1
by: Nurgle | last post by:
I'm sorry this is so long. I've edited a couple of times now, and cut it by 1/3 but haven't come up with a way to shorten it more without losing important bits. I don't even know what to search...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
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...

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.