VBA - a command button for both overwrite and append

Pergega

New Member
Joined
Mar 12, 2013
Messages
13
Hi

I have a command button on my spreadsheet, the function is to overwrite and append data from excel to 2 different tables in access.

I run the code there seems to be no error, but I check the database there is no new record or overwrites been added. Do you think you can help me to identify the error please?


Private Sub UpdateButton_Click()

'Table name from database

strTable = "TextID"

Call ConnectToDatabase
Set rs = New ADODB.Recordset
rs.Open strTable, cn, adOpenKeyset, adLockOptimistic, adCmdTable
' open a recordset
r = 7
Do While Len(Range("A" & r).Formula) > 1
' the start row in the worksheet
With rs
' add values to each field in the record
.Fields("col852") = Range("G" & r).Value
.Fields("col44") = Range("H" & r).Value
.Update ' stores the new record
r = r + 1 ' next row
End With
rs.MoveNext
Loop
rs.Close
Set rs = Nothing

strTable2 = "FoodCategory"
Set rs2 = New ADODB.Recordset
rs2.Open strTable2, cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 7
Do While Not Range("A" & r).Value = ""
' the start row in the worksheet
With rs2
' add values to each field in the record
.Fields("DisplayOrder") = Range("G" & r).Value
.Fields("BackColor") = Range("H" & r).Value
r = r + 1 ' next row
End With
rs2.MoveNext
Loop
rs2.Close
Set rs2 = Nothing
cn.Close
Set cn = Nothing
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,222,900
Messages
6,168,926
Members
452,227
Latest member
sam1121

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