?
Solved

Reading data from a csv file into access table

Posted on 2014-11-08
6
Medium Priority
?
329 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 51

Accepted Solution

by:
Gustav Brock earned 2000 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 51

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 51

Expert Comment

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

/gustav
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

801 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