delete a record in Access based on a reference in Excel Field

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
774
Office Version
  1. 365
Platform
  1. Windows
Good afternoon

I have written some VB code to open a database look at a specific table and if the reference is in my spreadsheet reference "UniqRef" I want it to delete it from the table so I can upload the amended version.
I used some code I have used previously that finds the row with the values and deletes it so I can re update it. Instead of deleting the information i get a "Run-time error '-2147217904 (80040e10)': No value given for one or more required parameters" which I havent seen before.
I have checked the database and can see the reference "AY100531MA2703AG" in the table as well as seeing it identified in the code stepping through as being the same

Code:
Dim oRs As Object
Dim sConn, strSQL, UniqRef As String

StrDBPath = "\\rbsres01\shareddata\ctcbir\Everyone\Business Unit Admin – AD1000\MI Folder\Businessline Quality SharePoint\2023-03-20CallQualityDatabaseHR.accdb"
    sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                 "Data Source=" & StrDBPath & ";" & _
                                 "Jet OLEDB:Engine Type=5;" & _
                                 "Persist Security Info=False;"
                             
Set oConn = CreateObject("ADODB.Connection")
Set oRs = CreateObject("ADODB.Recordset")

oConn.Open sConn

strSQL = "DELETE * FROM ObservationTable WHERE [UniqRef] IN (" & UniqRef & ");"

oConn.Execute strSQL

oConn.Close
 
Set oConn = Nothing
Set oRs = Nothing
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This worked for my test database: single table named "ObservationTable" with 1 column "UniqRef" and 4 rows of data. Two rows had "AY100531MA2703AG" and 2 didn't. After running, the desired rows were eliminated from the table.

I got the same error you did with brackets and no single quotes in the parentheses. Removing brackets only still produced same error. Adding single quotes did the trick. So, after playing around, this was the successful line:
VBA Code:
strSQL = "DELETE * FROM ObservationTable WHERE UniqRef IN ('" & UniqRef & "');"
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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