VBA / SQL - detect if record exists in database table?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some SQL embedded in my VBA that takes the values entered in a form by the user and uploads them to a database. I want to add some basic error checking that will generate an error if they attempt to add a record that already exists.

This is the relevant part of my code:
Code:
Dim conn As New ADODB.Connection
Dim Centralconn As String
Set rs = New ADODB.Recordset

Centralconn = "Provider=SQLOLEDB; Data Source=stavos\moonrise; Initial catalog=budgets; User ID=user; Password=password; Trusted_Connection=No"
conn.Open Centralconn

conn.Execute "IF NOT EXISTS (SELECT TOP 1 FROM Reporting.Annual_Budget WHERE [Company_Code] = '" & stxt_bud_company_code & "' AND [Year] = '" & stxt_bud_year & "' AND [Project_Type] = '" & stxt_bud_projecttype & "' AND [Department] = '" & stxt_bud_department & "') INSERT INTO Reporting.Test_Annual_Budget ([Company_Code], [Year], [Department], [Project_Type], [Currency], [Value], [Create_Date_Time], [Created_By]) " & _
"VALUES ('" & stxt_bud_company_code & "', '" & stxt_bud_year & "', '" & stxt_bud_costcentre & "', '" & stxt_bud_projecttype & "', '" & stxt_bud_currency & "', '" & stxt_bud_value & "', '" & bud_Create_Date_Time & "', '" & bud_Create_User & "')"
conn.Close
Set conn = Nothing

As it stands, it will upload the record if it doesn't already exist but do nothing if it does - which is good. But how do I get a message to pop up to say "record already exists, nothing uploaded"?

Thanks for reading.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You could create a recordset using the first part of your query. If the recordcount is 0, then run the insert part, otherwise pop up your message.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,749
Messages
6,174,275
Members
452,553
Latest member
red83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top