Using ADO to add data to fields with similiar name.

abduhsuryadi

New Member
Joined
Dec 23, 2012
Messages
2
I have a table, name myTable in access database in desktop\jwb.accdb. the table has more then 50 fields. the name of the fields: answer1, answer2,....answer50.
And I have data in excel column A1 to A50 that I want to add to these fields.

data in A1 to answer1
data in A2 to answer2
data in A3 to answer3

how can i make my code simple?
my current code is mostly like this:

Code:
Dim sSQL As String
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection

Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Environ("UserProfile") & "\Desktop\jwb.accdb;Persist Security Info=False"

Set rs = New ADODB.Recordset

sSQL = "myTable"
rs.Open sSQL, cn, adOpenKeyset, adLockOptimistic

rs.AddNew
    rs!Answer1 = Range("A1")
    rs!Answer2 = Range("A2")
    rs!Answer3 = Range("A3")
    'and soon....
rs.Update

rs.Close

Set rs = Nothing
cn.Close

Set cn = Nothing

thanks for the help
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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