Queries in Excel VBA, delete and insert - please help

bhavankars

New Member
Joined
Dec 29, 2004
Messages
20
I am trying to connect to Access database from Excel VBA.
And then delete a row if it exists.
Then insert a row in the table.

I have some code from different examples: I have tried to use it, but unfortunately it breaks at: " With rstEQP.Seek = sEQPID" down below.

Please help!!!


Dim rstEQP As Recordset
Dim sEQPID As Long
Dim sAccName As String

sEQPID = ActiveWorkbook.Sheets("Access Export").Cells(2, 127)
sAccName = ActiveWorkbook.Sheets("Access Export").Cells(2, 1)


Set rstEQP = DB.OpenRecordset("Master")
DeleteRecord rstEQP, sEQPID

rstEQP.Close
DB.Close


isql = "Insert into Master (EQP ID, Account Name) values " & sEQPID & "' , " & sAccName & ") ;"

a = MsgBox("PB: Do you really want to close the workbook?", vbYesNo)
If a = vbNo Then Cancel = True

End Sub

Sub DeleteRecord(rstEQP As Recordset, sEQPID As Long)

With rstEQP.Seek = sEQPID
If rstEQP.NoMatch Then
MsgBox "No EQP #" & sEQPID & " in table!"
Else
rstEQP.Delete
MsgBox "Record for EQP #" & sEQPID & _
" deleted!"
End If
End With

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Is your Access object library loaded in VBA?

In VBA select view object browser
Right-click next to question mark
Select references
Tick Microsoft Access

Regards,

Peter
 
Upvote 0
Thanks Peter, I have done that.

But I am stuck in the deleterecord function. I get an error when it is trying to execute : "With rstEQP.Seek = sEQPID
" line.

Please help!
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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