apend an access table from excel

Joe C

Well-known Member
Joined
Oct 17, 2002
Messages
841
Can I append an acces table through vba.

I have retireved queries through ado and want to create a similar report but I need the user to append table for lastest monthly data each time user runs.
Is it possible to append the table using VBA ADO or someway. I would rather not have the person have to open access themself.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Re: apend a access table from excel

Hi

Assumptions:
Access database path & name: Y:\Work\db1.mdb
Table Name: Table1
Fields: ID, Field1

Then you can use the following code to update from Excel to Access:

Rich (BB code):
Sub t4()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
With cnn
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Y:\Work\db1.mdb;User Id=admin;Password=;"
    .CursorLocation = adUseServer
    .Open
End With

rst.Open "Table1", cnn, adOpenForwardOnly, adLockOptimistic, adCmdTable
For Each cell In Range("A2:A4")  'assumes IDs are in A2:A4 and Field1 names in B2:B4
  With rst
      .AddNew
      !ID = cell.Value :     !Field1 = cell.Offset(,1).Value
      .Update
  End With
Next cell
End Sub
 
Upvote 0

Joe C

Well-known Member
Joined
Oct 17, 2002
Messages
841
Re: apend a access table from excel

Many thanks Going to give it a try this morning.

Usually my tries go well into the night, but at least I am having fun.
 
Upvote 0

Joe C

Well-known Member
Joined
Oct 17, 2002
Messages
841
ADVERTISEMENT
Re: apend a access table from excel

i think I have this working just want to make sure.
This is adding a new record for each?
 
Upvote 0

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Re: apend a access table from excel

The code I gave was adding a new record for each cell in range A2:A4 (ie 3 records in total). Two fields were added for each record.
 
Upvote 0

Joe C

Well-known Member
Joined
Oct 17, 2002
Messages
841
ADVERTISEMENT
Thats what I thought. If there were more fileds I would just add them.
But it does not make sure the record is nuique?
 
Upvote 0

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
No, the code performs no checking of the records that are added - this would have to be coded in if you need to do this. Something to be aware of is that if you have an Autonumber field in your data table in Access, then I'm pretty sure you can't use the AddNew method to add a new record (you have to go another route and use an INSERT SQL statement).
 
Upvote 0

Forum statistics

Threads
1,195,640
Messages
6,010,868
Members
441,571
Latest member
stolenweasel

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