Solved

Reading data from a csv file into access table

Posted on 2014-11-08
6
319 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 39

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now