How Can I Send Data from Excel to Access

Skibum346

New Member
Joined
Sep 9, 2002
Messages
7
I have two sheets in a workbook with ranges that need to be sent to existing tables in an existing database. Manually I would use the "Data, Convert To MSAccess..." menu route but how can I do this with a macro?

Thanks in advance for any help.

Kindest

Frank
 

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)

ChrisUK

Well-known Member
Joined
Sep 3, 2002
Messages
675
Frank, try this code. Please not you will need to add a reference to the MS ActiveX library 2.5 in the tools / references section. This worked fine for me

Have fun.

Sub InsertData()

Dim SQLStatement
Dim ConnectString
Dim MyODBCConnection

'The name of my ODBC connection
MyODBCConnection = "UsersDB"

' Open connection
' You need to set the ADO reference up in the Tools/Refence menu (Microsoft Actice X data objects Library)
Set ConnectDB = New ADODB.Connection
ConnectString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=" & MyODBCConnection
ConnectDB.Open ConnectString

' Open recordset from database table
Set rsNames = New ADODB.Recordset
rsNames.CursorLocation = adUseClient

'rsNames.Open SQLStatement, ConnectDB, adOpenStatic, adLockReadOnly, adCmdText

' Setup a counter to run down the first 10 rows of sheet1
' getting data froom the first two columns
LineCount = 1

' Loop through the cells
Do While LineCount < 10
'Get values from worksheet
x = Worksheets("Sheet1").Cells(LineCount, 1).Value
y = Worksheets("Sheet1").Cells(LineCount, 2).Value

'Write to database
SQLStatement = "Insert into mytable values('" & x & "','" & y & "')"
rsNames.Open SQLStatement, ConnectDB, adOpenStatic, adLockReadOnly, adCmdText

LineCount = LineCount + 1
Loop

Set ConnectDB = Nothing
Set rsNames = Nothing

End Sub
 

Forum statistics

Threads
1,144,363
Messages
5,723,919
Members
422,527
Latest member
JayTheKaz

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