简体   繁体   中英

VBA [Access DB] - Merge Multiple Tables into one Single Table

I have a Macro that Scans through a Folder in a defined \\Dir. To grab a bunch of csv files that are generated daily. From here I import each csv file into my Access db as a new Table, I then remove the empty tables (the system generates the files regardless of whether there was any production or not). This all works fine (see screen shot below)导入表列表截图

From here I want to merge each table into one table that I can then pull into PowerBI to then develop some required production reports. Note: this will become a scheduled Process that will execute daily at a set time so I need to be able to add the imported table into a pre-existing table.

For some context:
I know this probably seems like a real round-a-bout way of getting the data needed. Basically this is only going to be a back-up interim solution. We have been given access to a direct data-feed from the vendor to build our own data-warehouse. Unfortunately the current data we have been provided does not line up at all with the csv dump that they have been providing daily - so we figure that there is some transformation happening to the data that we are waiting to get clarification on. In the meantime we have 1.5 weeks left to produce our first power BI report so we need a solution to fall back on just in-case.

Any help on how I can merge these tables or even a better way to import this data will be greatly appreciated.

Private Function ProcessFile(filename As String, pName As String, sName As String, Optional rName As String = "NULL")
    'Get Actual File Name
    ActualName = TargetPath & Dir(TargetPath & filename)
    
'Set Placeholder Char (To Keep RoomName to x4 Chars)
Dim placeholderchar As String: placeholderchar = "x"
If Len(rName) < 4 Then
    For i = 1 To 4 - Len(rName)
        rName = rName & placeholderchar
    Next i
End If

'Define New Table Name for csv File
Dim tblName As String: tblName = UCase(Replace(TargetDate, "-", vbNullString) & Left(sName, 3) & pName) & rName

'Create New Table from csv File
Call DoCmd.TransferText(TransferType:=acLinkDelim, TableName:=tblName, _
filename:=ActualName, hasfieldnames:=True)

'If Table is Empty; Delete Table & Exit Function
If DCount("*", tblName) = 0 Then: Call DoCmd.DeleteObject(acTable, tblName): Debug.Print ("Table Deleted: " & tblName): Exit Function

Debug.Print ("Table Added:   " & tblName)
End Function

This worked:

If all field names are identical, you could simply do at the end: CurrentDb.Execute "INSERT INTO MyWorkTable SELECT * FROM " & tblName – Andre 16 hours ago

What I am struggling with now is adding in the identifiers we need to be able to report on the Data, as stated the data comes from several plants and several different rooms/departments within those plants

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM