Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 557
Default Export Data from Excel into existing Table in Access

Hi all, I got Access Database on path "C:\Data\Rec.mdb". I got one
table in that database with the name "Table1" with five columns in
it. I want macro in excel which should delete all the records in
"Table1" and then copy Range("A2:E100") of Sheet2 and paste it into
"Table1". (Note: headings in Range("A1:E1") of Sheet2 are exctly
match with the "Table1" headings). Please can any friend help me on
this. I dont know if its possible or not!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Export Data from Excel into existing Table in Access

this should work.
Option Explicit
Sub UploadDataToAccess()
'Needs reference the Axtive X Library 2.0 or higher
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String, sConn
Dim iRow As Integer
Dim ws As Worksheet

Set ws = Worksheets("Sheet2")
sConn = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Data\Rec.mdb;Persist Security Info=False"

Set cn = New ADODB.Connection
With cn
.Open sConn
sSQL = "DELETE Table1.* FROM Table1;"
Set rs = New ADODB.Recordset
With rs
.Open sSQL, cn
End With
Set rs = Nothing

sSQL = "Table1"
Set rs = New ADODB.Recordset
With rs
.Open sSQL, cn, adOpenKeyset, adLockOptimistic, adCmdTable
iRow = 2
Do While Len(ws.Range("A" & iRow).Formula) 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
'add values to each field in the record
.Fields("Field1") = ws.Range("A" & iRow).Value
.Fields("Field2") = ws.Range("B" & iRow).Value
.Fields("Field3") = ws.Range("C" & iRow).Value
.Fields("Field4") = ws.Range("D" & iRow).Value
.Fields("Field5") = ws.Range("E" & iRow).Value
' add more fields if necessary...
.Update ' stores the new record
End With
iRow = iRow + 1 ' next row
Loop
.Close
End With
Set rs = Nothing
.Close
End With
Set cn = Nothing
End Sub

"K" wrote:

Hi all, I got Access Database on path "C:\Data\Rec.mdb". I got one
table in that database with the name "Table1" with five columns in
it. I want macro in excel which should delete all the records in
"Table1" and then copy Range("A2:E100") of Sheet2 and paste it into
"Table1". (Note: headings in Range("A1:E1") of Sheet2 are exctly
match with the "Table1" headings). Please can any friend help me on
this. I dont know if its possible or not!
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Access Table to Excel Don[_27_] Excel Programming 2 September 27th 06 03:39 PM
Macro to Export Selected fields to an Existing Access Database ernie Excel Programming 1 March 13th 06 05:01 PM
Data From Excel Range To Existing Access Table Jason Excel Programming 16 December 2nd 05 09:14 AM
vb code to export data into existing Access table desperate Excel Programming 2 November 19th 03 05:05 PM
Export Data to Access Table Pete T[_2_] Excel Programming 1 October 10th 03 11:47 PM


All times are GMT +1. The time now is 09:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"