Importing Data From Legacy System Into Access 2007, Access 2007 (1 Viewer)

Lateral

Registered User.
Local time
Today, 08:34
Joined
Aug 28, 2013
Messages
388
Hi guys,

I'm trying to help a friend who has very old legacy system written in some ancient language that hasn't been supported for decades. He has purchased some new software to replace the old system but the old historical data could not be imported into the new system so he keeps the old system around so that he can access it when he needs to.....one day it's going to die and it will cause him lots of issues..

My friend operates an automotive mechanical repair workshop and the old system system is basically an invoicing system.

I essentially want to import a subset of the data into Access 2007 and build him a simple to use interface so that he can more easily find his historical data.

I have figured out that the data can be read directly by Excel and whilst I don't have a problem importing most of the data, I am having a problem figuring out how to import the individual invoices as they are all stored as separate files in a single folder under a unique file name.

I have found a file that contains a list of all invoices with the customer number etc and this is easy to import into Access.

I have attached one of the invoice files so that you can see what it looks like. the first record I would think would be used as a Header record to match field names. The file is called "A0011204.ONQ" and can be opened directly by Excel.

I want some code, probably VBA, that will skip though all of the files in the folder, import the data into a single table called "tInvoices" and add the file name into a field called "InvoiceNumber" of each record. This way, if the original invoice has 20 lines in it, "tInvoices" will also contain 20 separate records for each line plus the additional field called "InvoiceNumber" that would be set to the name of the file...for example "A0011204".

As this type of code is above my "pay scale", I'm hoping somebody can help me solve this issue.

Thanks for any help you can provide me.

Cheers
Greg
 

Attachments

  • A0011204.zip
    1.1 KB · Views: 60

Anakardian

Registered User.
Local time
Today, 17:34
Joined
Mar 14, 2010
Messages
173
I have used something like this to import from excel files:
Code:
Option Compare Database
        Public strPathOriginal As String 'The original path to teh file location
        Public strPathWorking As String 'The path to temporary working location of the file being worked upon
        Public strPathTrouble As String 'The path to the folder for trouble files
        Public strPatheArchived As String 'The path to teh file location for archived files
        Public strFile As String 'The current file name being worked with
        Public strPath As String
        Public strPathFileOriginal As String 'The full string of the original file and location
        Public strPathFileWorking As String 'The full string of the working file and location
        Public strPathFileArchive As String 'The full string of the working file and location
        Public strPathFileTrouble As String 'The full string of the trouble file and location
        Public fs As Object
        Public strTable As String
        Public blnHasFieldNames As Boolean
        Public blnEXCEL As Boolean
        Public blnReadOnly As Boolean
        Public objExcel As Object
        Public objWorkbook As Object
        Public objWorksheet As Object
        Public VesselName As String
        

Public Function ImportData()
        'Create excel object
        On Error Resume Next
        Set objExcel = GetObject(, "Excel.Application")
        If Err.Number <> 0 Then
            Set objExcel = CreateObject("Excel.Application")
            blnEXCEL = True
        End If
        Err.Clear
        On Error GoTo 0
                
        ' Change this next line to True if the first row in EXCEL worksheet has field names
        blnHasFieldNames = False

        ' Replace C:\Documents\ with the real path to the folder that contains the EXCEL files
        strPathOriginal = "C:\Documents\" 
        
        'Set temporary local storage for importing data from 
        strPathWorking = "C:\temp\"
        
        'Set archive location
        strPathArchived = "your archive location"
        
        'Set trouble file location
        strPathTrouble = "your problem files location"
        
        ' Replace tablename with the real name of the table into which the data are to be imported
        strTable = "ImportTable"
        
        'Get the list of files in the directory
        strFile = Dir(strPathOriginal & "*.xlsx")
        
        
        Do While Len(strFile) > 0
        strPathFileOriginal = strPathOriginal & strFile
        strPathFileWorking = strPathWorking & "Working.xlsx"
        strPathFileArchive = strPathArchived & strFile
                
        'Get the vessels name from the file name
        FirstFind = InStr(strFile, "-")
        SecondFind = InStr(FirstFind + 1, strFile, "-")
        VesselName = Trim(Mid(strFile, FirstFind + 1, SecondFind - FirstFind - 1))
        
        'MsgBox (strFile) 'Used for testing only
        
        'if vessel exists, run import routine, if not move to trouble folder
'Convert this to check if your customer exists
        If DCount("[VesselID]", "VesselData", "[VesselName] = '" & VesselName & "'") > 0 Then
        
            'copy file to working directory
                Set fs = CreateObject("Scripting.FileSystemObject")
                fs.CopyFile strPathFileOriginal, strPathFileWorking
                Set fs = Nothing
                
            'Use import function for excel operation
                ImportFromExcel
        
            'remove working copy
                Kill strPathFileWorking
                
            'copy file to archive directory
                Set fs = CreateObject("Scripting.FileSystemObject")
                fs.CopyFile strPathFileOriginal, strPathFileArchive
                Set fs = Nothing
                
            'remove original copy after copying it to the archive, remove if you do not want to delete old files
                Kill strPathFileOriginal
        
            strFile = Dir()
        
        Else
            'set trouble file location
                strPathFileTrouble = strPathTrouble & strFile
            
            'copy file to trouble directory
                Set fs = CreateObject("Scripting.FileSystemObject")
                fs.CopyFile strPathFileOriginal, strPathFileTrouble
                Set fs = Nothing
                
            'remove original copy after copying it to the archive, remove if you do not want to delete old files
                Kill strPathFileOriginal
        
            strFile = Dir()
        End If
        
        
        Loop




End Function


Public Function ImportFromExcel()
        Dim SheetVersion As String
        
        'Switch off warnings
        DoCmd.SetWarnings False
        
        'rename sheet with data
                Set objWorkbook = objExcel.workbooks.Open(strPathFileWorking, updatelinks:=0)
                objWorkbook.Sheets("Environmental data input").Name = "Working"
                SheetVersion = objWorkbook.Sheets("Working").range("G1").Value
                objWorkbook.Save
                objWorkbook.Close
                objExcel.Quit
                
                
        'Import excel sheet
                DoCmd.TransferSpreadsheet _
                acImport, _
                acSpreadsheetTypeExcel9, _
                strTable, _
                strPathFileWorking, _
                False, _
                "Working!A6:BL500"

After this point I am running some cleaning and conversions to be able to put the data in the right spots.
At the end I run the respective append and/or update queries.

I recieved my files on e-mail and had to dump the files into a folder for processing. To see if I had anytihng lying around I emptied the input folder.
I assume you do not want to remove the old files so take care to comment out those lines.

One handy feature is that it moves files that it cannot process into a specific folder so I can check for trouble.

As always, make sure to modify the code to something that fits your purpose.
 

Lateral

Registered User.
Local time
Today, 08:34
Joined
Aug 28, 2013
Messages
388
Thanks Anakardian

I managed to get things working by using a solution using Excel to merge the files first. Thanks for your help.

Cheers
Greg
 

Users who are viewing this thread

Top Bottom