Solved

Reading data from a csv file into access table

Posted on 2014-11-08
6
326 Views
Last Modified: 2014-12-31
I would like to build a database application that I can use to manipulate data read from a csv file.  The csv file is a data dump from a Wonderware HMI application.  It contains all of the tags used in the application grouped by a keyword.  As the csv file is read, the application will need to determine if the row of data is a header row or a data row.  If it is a header row, the first column name will begin with a keyword.  I would like to use this keyword to determine the table that the subsequent rows should be imported into.  I have attached a spreadsheet that shows the raw csv file and an edited to show the logic that I have been using.  I have to do this process for every project that I work on, so it would save me a huge amount of time if I could automate this.
ee.xlsx
0
Comment
Question by:AaronGreene1906
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:als315
ID: 40430919
It is not a csv file, you should read it line by line from vba. Can you upload original source file and structure of access table?
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40431064
Yes, use the CSV as is.
Read it line by line.
For each line, either switch the table to append to, or read the fields of the line into a new record of the currently chosen table.

Here's the basics for reading a file into a table.
Adjust it to your needs.

     Dim dbs As DAO.Database
     Dim rst As DAO.Recordset
     Dim strRecordSource As String       'Source for recordset, can be SQL, table, or saved query
     Dim intFileDesc As Integer            'File descriptor for output file
     Dim strSourceFile As String           'Full path of source file
     Dim strTextLine As String             'Input buffer
     Dim varField1 As Variant               'Extracted Field1 from buffer
     Dim varField2 As Variant               'Extracted Field1 from buffer
     Dim varField3 As Variant               'Extracted Field1 from buffer
' Add fields as needed.
'     Dim varField99 As Variant               'Extracted Field99 from buffer

     Set dbs = CurrentDb
     
     intFileDesc = FreeFile
     Open strSourceFile For Input As #intFileDesc
     Do While Not EOF(intFileDesc)       ' Loop until end of file.
         Line Input #intFileDesc, strTextLine                  'Read line into buffer
         ' If a header line, set recordset to include the table to append date:
         ' strRecordsource = "Select Top 1 From tblSomeTable"
           Set rst = dbs.OpenRecordset(strRecordSource, dbOpenDynaset)

         ' Else

         ' <  use string handling to extract the fields from strTextLine.
         '    Use Val() to convert fields to numbers when needed.
         '    and place them in varField1, varField2, & varField3         >
         rst.AddNew        
           rst!Field1 = varField1
           rst!Field2 = varField2
           rst!Field3 = varField3
         rst.Update
       ' End If
     Loop
     Close #intFileDesc    'Close file.
     rst.Close              'Close the recordset
     Set rst = Nothing
     Set dbs = Nothing     'Garbage handling before we exit the function

 /gustav
0
 

Author Comment

by:AaronGreene1906
ID: 40440850
I'm not sure that I follow your line of thought.  What is the intFileDesc variable used for?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40441317
That's the file handler. You can have more than one file open, then you'll have to differentiate between them.

/gustav
0
 

Author Comment

by:AaronGreene1906
ID: 40442010
Okay, I see what you are doing there.  Now, I will need to take the data read from the csv and manipulate it so I can add it to the target tables.  I would like to use the split function to break the strTextLine instead of using a bunch of text functions.
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40442016
That's a viable option. The code origins from the time before Split.

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question