Problems with UPDATE and INSERT from DAO

pbassett

Active Member
Joined
May 5, 2004
Messages
358
I wish to check from Excel if a record is in Access and either Update that record or Insert a new record based on the results. The SELECT works fine getting the count, but the INSERT and UPDATEs are giving me an error "Object required" :(

Here is the code:

Dim rs As DAO.Recordset
Dim strSQL As String
Dim db As DAO.Database
Set db = Workspaces(0).OpenDatabase(myPathname)

Set rs = Nothing
Set rs = db.OpenRecordset(SelectSQL, dbOpenDynaset)
If rs.RecordCount <> 0 Then ' update existing record
DoCmd.RunSQL (UpdateSQL)
Else ' insert new record
DoCmd.RunSQL (InsertSQL)
End If

My UpdateSQL variable is
UPDATE [Planned Resources] SET Jun = 0 WHERE PIW = '261' AND Category = 'Billing Ops'
which runs OK directly in Access.

Thanks,
Pete
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I think that the problem is that the DoCmd statements are not pointed at Access. Not tested but you could try:-

Dim rs As DAO.Recordset
Dim strSQL As String
Dim db As DAO.Database
Set db = Workspaces(0).OpenDatabase(myPathname)

Set rs = Nothing
Set rs = db.OpenRecordset(SelectSQL, dbOpenDynaset)
If rs.RecordCount <> 0 Then ' update existing record
db.DoCmd.RunSQL (UpdateSQL)
Else ' insert new record
db.DoCmd.RunSQL (InsertSQL)
End If

HTH

Peter
 
Upvote 0

Forum statistics

Threads
1,221,783
Messages
6,161,936
Members
451,730
Latest member
BudgetGirl

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