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
Solved

Reading data from a csv file into access table

Posted on 2014-11-08
6
324 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
  • 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 49

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 49

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 49

Expert Comment

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

/gustav
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

861 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