473,480 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to verify if record already exist in another table

5 New Member
Hi,

I need some help with some VBA code using a DLookup. I am using a text box on a form to add a serial# to a table. When the serial# is entered I need to make sure that the serial number being added already exist in another table. If the record does not exist I need to display a message saying record does not exist and not allow the record to be added.

*Notes
The serial# is text not a number.
The serial number field in the table is called UNITID in both the 'Production' table and 'Consigned' table.
Jan 30 '16 #1
5 5805
zmbd
5,501 Recognized Expert Moderator Expert
+ Normalize your database and this situation doesn't occur
Database Normalization and Table Structures

However, if you insist on the current database design... and I really think you have a design issue:

(+) You can do this using recordsets too; however,
DCOUNT Function might be a better solution than DLOOKUP() - if the value isn't found the result is zero.

The MS link has several examples; however, the one thing of note there is that all of the examples show to build the criteria string within the DCOUNT().

However, building the string within the function makes troubleshooting most difficult as you cannot test for a properly formatted conditional string...
Their example:
Expand|Select|Wrap|Line Numbers
  1. =DCount("[OrderID]", "Orders", "[ShipRegion] = 'CA'")
How I would modify this:
Expand|Select|Wrap|Line Numbers
  1. Dim zCriteria As String
  2. ...
  3. zCriteria = "[ShipRegion] = 'CA'"
  4. =DCount("[OrderID]", "Orders",zCriteria )
The most common problem is a malformed conditional string. Using this method, you can insert a Debug.Print ZCriteria and output the resolved string to the immediate pane ( [ctrlG] ) to check the result!

Now you simply test for a non-zero count.
Jan 30 '16 #2
TXNEWT
5 New Member
Thanks, but this is not quite what im looking for. There was an old post on this site that i tried to bump because it was very close to what i was looking for, but it got deleted by the mods for bumping...

This was the question
"Good morning. I have two tables (Batches) and (Archive). (Batches) is a linked table that contains all of the existing data and the other (Archive) I am keying data into, through a form. I want to be able to key into a form that writes to (Archive), but before it saves or moves to the next field, I want to make sure that the data already exists in (Batches) and prompt the user if it doesn't. The reason being is that I don't want to save any records in (Archive) that don't exist in (Batches). Also, just to be clear. There is only one field [BatchID] that I want to be able to check between the two tables."

and this was the solution;

Expand|Select|Wrap|Line Numbers
  1.  Private Sub BatchID_AfterUpdate()
  2. Dim tmpBID As Long
  3.  
  4.    tmpBID = nz(DLookup("[BatchID]","Batches","[BatchID]=" & Me!BatchID),0)
  5.    If tmpBID = 0 Then
  6.       Msgbox "This BatchID does not exist in table Batches"
  7.    End If
  8.  
  9. End Sub
This is what i'm looking for, but i think the fact that this is declared as a 'Long' may be my issue. I tried changing to a string, but I don't think I am getting the syntax correct. Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. Private Sub UNITID_AfterUpdate()
  2.  
  3. Dim tmpBID As String
  4.  
  5.    tmpBID = Nz(DLookup("UNITID", "BARCODESUB", "UNITID= ' ME!UNITID' "), " ")
  6.  
  7.    If tmpBID = " " Then
  8.       MsgBox "This Serial# does not exist in proir production manifest!", vbOKCancel
  9.  
  10.  
  11.    End If
  12.  
  13. End Sub
When I run the program it gives me the msgbox even if the record exist.

Thanks!
JIN
Jan 30 '16 #3
zmbd
5,501 Recognized Expert Moderator Expert
Ah yes... we do try to keep threads/questions separate otherwise the context becomes too muddied for any use :) As a work around, we ask that a new thread be started and that a link to the old thread be inserted along with the new question/criteria.
(in this case the old thread is: Need to verify data exists in another table )

Now for the issue at hand:
Take a look at line 5 of your code:
tmpBID = Nz(DLookup("UNITID", "BARCODESUB", "UNITID= ' ME!UNITID' "), " ")

Your criteria is literally being returned as:
UNITID= ' ME!UNITID'
as written, this is impossible to trouble shoot as I've indicated in my prior post regarding building the string within the function.

Instead build your string first:
zstr = "UNITID= '" & ME!UNITID & "'"

Then insert into the function
tmpBID = Nz(DLookup("UNITID", "BARCODESUB", zstr), " ")

This is what I was talking about in my post for the modifications to the example given in my post.

NOW

IF you do not plan to use the [UNITID] for anything else in the code, then returning that value is not needed, instead a simple count returned is enough; thus, no need to use the NZ() function.

comme ça:
Expand|Select|Wrap|Line Numbers
  1. Private Sub UNITID_AfterUpdate()
  2.  Dim tmpBID As String
  3.  tmpBID = "UNITID= '" &  ME!UNITID & "'"
  4. 'Debug.print tmpBID
  5. '
  6.  If DCount("[UNITID]", "BARCODESUB",tmpBID)=0 then
  7.    MsgBox "This Serial# does not exist in prior production manifest!", vbOKCancel
  8.   End If
  9. End Sub
> Now one can simply uncomment line5 to check if there is a properly resolved string depending on the value of ME!UNITID.

In any case, you have nothing to lose by trying my suggestion.
Jan 31 '16 #4
TXNEWT
5 New Member
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
Feb 9 '16 #5
zmbd
5,501 Recognized Expert Moderator Expert
Related question moved to:
Check if Record already exists and prevent duplicated Record
Feb 10 '16 #6

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

Similar topics

0
1804
by: Ondernemer | last post by:
I looked everywhere (Google, manual, etc.) but can't find an answer to this question. If I do a "INSERT IGNORE INTO" query into a UNIQUE field and the word I'm trying to insert already exists,...
0
1874
by: EKL | last post by:
Hi, I'm making a sort of Customer and Orders database in MS Access 2003. My problem is that I wish to update the table "tblTransaction" based on changes made in the table "tblOrderDetails"....
2
1320
by: Darryl Neale | last post by:
I basically want to, in my report, print a record out of another table on my report depending on which one of the records in the table being viewed matches a second record in the other table, I...
8
2188
by: SBC News Groups | last post by:
I have a field on a form that when a user enters a number, I want to verify that the number is already in another table. For example: If a user enters 100 in the control Bidder, I want to check...
3
5178
by: kchang77 | last post by:
Good morning. I have two tables (Batches) and (Archive). (Batches) is a linked table that contains all of the existing data and the other (Archive) I am keying data into, through a form. I want...
6
3584
by: Helena666 | last post by:
Hi Its been a while since I have built a database using access and vba and am a bit rusty. I am using a command button on a form to write a record to a table, using an append query. However I need...
1
2125
by: Becky99 | last post by:
In the database I'm working on (2003) I have a client table that includes a client status field, and I have a linked notes table that includes the note entry date, note type, and note details. Each...
4
2329
by: timleonard | last post by:
Is it possible and if so, how? Would like to have code to verify that a file name in a same directory matches with the file name in a table/ field. If it matches then run the rest of the code, if...
2
2524
by: anoop s | last post by:
I want to delete records of one table that exist in another table. The tables have no primary key. It will take a lot of time compare field by field. Is there any method record by record comparison...
2
2132
by: Francesca | last post by:
Hi mysql and perl experts, I am new to both perl and mysql, and I am trying to populate a database that I had previously created in mysql. I am stuck on a step and don't really know how to move...
0
6908
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
7044
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
6929
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
5337
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
4481
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
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.