Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Export Excel worksheet to Access using VBA


Export Excel worksheet to Access using VBA

Author
Message
sclward
sclward
Forum God
Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)

Group: Forum Members
Posts: 3, Visits: 14
I'm wanting to export an Excel worksheet to an existing Access table. I want to do this from within Excel using VBA and a user macro. I want to be able to append the existing records within the Access table. What's the best way to do this? Thanks.
paul daniel
paul daniel
Forum God
Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)Forum God (5.1K reputation)

Group: Forum Members
Posts: 19, Visits: 20
Try this code



Sub DAOFromExcelToAccess()

' exports data from the active worksheet to a table in an Access database

' this procedure must be edited before use

Dim db As Database, rs As Recordset, r As Long

Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb")

' open the database

Set rs = db.OpenRecordset("TableName", dbOpenTable)

' get all records in a table

r = 3 ' the start row in the worksheet

Do While Len(Range("A" & r).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("FieldName1") = Range("A" & r).Value

.Fields("FieldName2") = Range("B" & r).Value

.Fields("FieldNameN") = Range("C" & r).Value

' add more fields if necessary...

.Update ' stores the new record

End With

r = r + 1 ' next row

Loop

rs.Close

Set rs = Nothing

db.Close

Set db = Nothing

End Sub


.NET Application Development | C# Development
sclward
sclward
Forum God
Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)

Group: Forum Members
Posts: 3, Visits: 14
Thanks for the response. Seems we have a problem at a certain area while compiling. It does not like the Dim statement as far as Dim db As Database, rs As Recordset. Any ideas?
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search