Syntax error insert value if does not exist in table

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to write a query that will add a value to a table in Access if the value does not already exist.

Table is: tbl_Client which only contains fields Client_ID and Client_Name

From online reading, I've written in Access query:
Code:
IF NOT EXISTS(SELECT * FROM tbl_Client WHERE Client_Name = 'test')
  INSERT Values('test') INTO tbl_Client.Client_Name

However, this errors with: "Invalid SQL statement; expected DELETE, INSERT, PROCEDURE, SELECT or UPDATE"

Can someone help correct syntax please?

TIA,
Jack
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I dont know SQL that well but heres a very similar problem

 
Upvote 0
This can be done in a series of two queries (or in a single nested query):
1st query: Do an unmatched query (to find records that exist in one object that don't exist in the other)
2nd query: Do an append query to add the records from the first unmatched query to your final object
 
Upvote 0
Hi Joe, yes I split into two queries, it was more to see if I was on right lines with SQL (never touched it before!) and if it could be optimised into a single (nested) query.

As is, the main macro:
Code:
Sub Create_New_Client()
    
    Dim NCN As String: NCN = wMain.Range("New_Client_Name").Value
    
    If Check_Database Then
        Add_New_Name NCN
        Make_New_Forecast_File Make_New_Folder(NCN), NCN, wMain.Range("New_Forecasting_Year").Value
    Else
        NCN = "Client " & NCN & " already exists!" & vbCrLf & vbCrLf & "Please contact Admin"
        MsgBox NCN, vbExclamation + vbOKOnly, "Client Name Already Exists"
    End If
    
End Sub
With:
Code:
Private Function Check_Database() As Boolean
    
    With CreateObject("ADODB.Connection")
        .Open Database_Params
        Check_Database = .Execute(Test_Name(wMain.Range("New_Client_Name").Value)).EOF
        .Close
    End With
    
End Function
And:
Code:
Private Sub Add_New_Name(ByRef new_name As String)

    With CreateObject("ADODB.Connection")
        .Open Database_Params
        .Execute Replace("INSERT INTO tbl_Client(Client_Name) VALUES('@');", "@", new_name)
        .Close
    End With

End Sub
Thanks for replies and suggestions, I have this working as I need now :)

Cheers,
Jack
 
Upvote 0
Your sql doesn't look like Access SQL by the way. You might have been looking at an example for SQL server.
 
Upvote 0
Hi xenou, I was looking at several sites and mixing search terms of SQL and Access SQL whilst trying to get something to work, not surprised by your comment!

Missed out query:
Code:
Replace("INSERT INTO tbl_Client(Client_Name) VALUES('@');", "@", new_name)
Similar to Test_Name above, except to insert, so only using 2 queries in this.
 
Upvote 0
Hey xenou sorry for late reply and yes working as per #4
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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