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
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Watch MrExcel Video

Forum statistics

Threads
1,122,672
Messages
5,597,488
Members
414,146
Latest member
marginmakerb

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
Top