Does "If exists (select..." Exist? (1 Viewer)

GregoryWest

Registered User.
Local time
Today, 08:21
Joined
Apr 13, 2014
Messages
161
I am trying to do something like this:
IF EXISTS (SELECT 1 FROM Table1 WHERE Column1 = 1) BEGIN -- UPDATE QUERY END ELSE BEGIN -- INSERT QUERY END
ENDIF

Does this work in Access 2013 VBA? What I need to do is check to see if a hashcode already exists in the database. If it does update, if it doesn't create new record.
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:21
Joined
Jan 23, 2006
Messages
15,380
You could create a unique index on your hashcode, then Access will give a duplicate can't be added type message that you can intercept.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:21
Joined
Oct 29, 2018
Messages
21,485
Hi. T-SQL code is different from VBA. You would have to convert what you have to make it work in Access. I usually use the DCount() function to test for duplicates.
 

GregoryWest

Registered User.
Local time
Today, 08:21
Joined
Apr 13, 2014
Messages
161
Not looking for duplicated persay. What is happening is; I get a new records from the mainframe system. I then have to 'import' these records into my tables. Problem is some of the records might already exist as is, others might exist but have been changed (Treat as new in this case). So what I do is create a hash of the records, and store this hash along with the data. Later I can check the hash on the incoming record, if exists do one thing, other wise do something else.


Just having a bear of a time doing the 'check' The reset is fairly trivial.
 

Micron

AWF VIP
Local time
Today, 09:21
Joined
Oct 20, 2018
Messages
3,478
Suggest that during the import process, run append query - trap error for violation and just ignore it.
Then run update against same target table. Regardless if target values are the same or not, update won't error. You don't need to identify which records already exist unless you have some other reason for doing so.
 

June7

AWF VIP
Local time
Today, 05:21
Joined
Mar 9, 2014
Messages
5,486
So you need to run two queries: INSERT and UPDATE or try what is called an UPSERT action that accomplishes both. Review https://stackoverflow.com/questions/6199417/upserting-in-ms-access

How are you accessing the mainframe data? Do you have a link to table or worksheet or CSV?

Unless there is some other unique record identifier, possibly build a query on the new data that calculates the hash then join that query to your local table on the hash fields.
 
Last edited:

GregoryWest

Registered User.
Local time
Today, 08:21
Joined
Apr 13, 2014
Messages
161
I get sent a CSV file every year, or when requested.


So you need to run two queries: INSERT and UPDATE or try what is called an UPSERT action that accomplishes both. Review https://stackoverflow.com/questions/6199417/upserting-in-ms-access

How are you accessing the mainframe data? Do you have a link to table or worksheet or CSV?

Unless there is some other unique record identifier, possibly build a query on the new data that calculates the hash then join that query to your local table on the hash fields.
 

GregoryWest

Registered User.
Local time
Today, 08:21
Joined
Apr 13, 2014
Messages
161
DCount works like a charm!! Thanks for the suggestion.


Hi. T-SQL code is different from VBA. You would have to convert what you have to make it work in Access. I usually use the DCount() function to test for duplicates.
 

Users who are viewing this thread

Top Bottom