Appending multiple tables

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,
I have the following situation:
1) Need to extract all the .xlsx files from a folder
2) Append all of them in one table
3) After having all the data in this table, need to start appending only the most recent file

I do have the vba code to get the most recent file and the other vba module to get all files, however not sure what the best approach to combine all of them in one table in access. If I use append query do I have to append one by one? Hopefully not :-P

VBA code to extract the most recent .xlsx file

VBA Code:
Option Explicit

Option Compare Database

'==========================================

' Import Data From Excel With DoCmd

'==========================================



Sub importDataFromExcelDoCmd()

' Set variables

Dim strTableName As String

Dim strFileName As String

Dim VarFileName As String

Dim blnHasHeadings As Boolean

Dim MyPath As String

Dim MyFile As String

Dim LatestFile As String

Dim LatestDate As Date

Dim LMD As Date



'********************Getting the latest file from the folder**************************************



MyPath = "Z:\Supply Chain\Demand Planning\Coles\SY35 Total Manassen\"

If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"

MyFile = Dir(MyPath & "*.xlsx", vbNormal)

If Len(MyFile) = 0 Then

MsgBox "No files were found...", vbExclamation

Exit Sub

End If



Do While Len(MyFile) > 0

LMD = FileDateTime(MyPath & MyFile)

If LMD > LatestDate Then

LatestFile = MyFile

LatestDate = LMD

End If

MyFile = Dir

Loop

VarFileName = (MyPath & LatestFile) 'Latest file



'*****************Setting the data in Access*************************



' Set data

strTableName = "tbl0ColesPromoData"

strFileName = VarFileName

blnHasHeadings = True

' Import data

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, strTableName, strFileName, blnHasHeadings



End Sub

VBA code to extract all the .xlsx files

VBA Code:
[/B]
Sub ImportfromPath(path As String, intoTable As String, hasHeader As Boolean)



Dim fileName As String



'Loop through the folder & import each file

fileName = Dir(path & "\*.xlsx")

While fileName <> ""

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, intoTable, path & fileName, hasHeader

'check whether there are any more files to import

fileName = Dir()

Wend



 End Sub
[B]
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Upvote 0
Not sure I follow this one. Haven't used transferspreadsheet for a looonngg time, but IIRC it just appends to a table, so why would you need any append query? Please advise if the transfer method doesn't append but over-writes instead.
 
Upvote 0
Not sure I follow this one. Haven't used transferspreadsheet for a looonngg time, but IIRC it just appends to a table, so why would you need any append query? Please advise if the transfer method doesn't append but over-writes instead.
Hi @Micron,

I'd like to give a go with IIRC appending to a table, do you have any examples, to ilustrate that?
 
Upvote 0
You realize that IIRC is short for "If I Recall Correctly"? If so, then I'm not sure what you're asking for.
It's like
- LOL, which I probably don't have to explain
- AFAIC as far as I'm concerned
- AFAIK as far as I know
:)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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