Solved

Reading data from a csv file into access table

Posted on 2014-11-08
6
323 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

776 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