Solved

Querying CSV file through VBA without knowing the header

Posted on 2013-06-04
4
2,173 Views
Last Modified: 2013-06-04
Hi,

I'm creating a macro that queries a CSV file that a user chooses, and saves the result of only the lines that meet a certain criteria.

The CSV file contains 7 columns, but 2 of which the headers are dates so they vary.  I need a way to be able to either find out the header names before running the query, or specify in the query through a header number.

e.g.
My file contains the following CSV file:
Curr,Days,1/1/2013,1/2/2013,Change,%Change
USD,1,100,100,0,0
JPY,1,1000,1000,0,0
....

Below is my code
objConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & dSource & ";" & _
        "Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
objConn.Open



SqlQry = "SELECT * FROM " & dfile & " " & _
            "WHERE ([% Change] >= 5 OR [% Change] <= -5) "

Rcdset.Open SqlQry, objConn

For i = 0 To Rcdset.Fields.Count - 1
    data_ws.Cells(1, i + 1).Value = Rcdset.Fields(i).Name
Next i

data_ws.Cells(2, 1).CopyFromRecordset Rcdset

Open in new window

So basically I'm looking for anything that has a 5% change, and I also need to look for any that didn't have data on today's column.  So in this case, it would be WHERE 1/2/2013 = '' but how can I chose this column/header when the value is constantly going to change?
0
Comment
Question by:iamnamja
[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
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39219160
Are the fields always in the same order? If so, perhaps specify HDR=No in the connection and use F4 for today's field and F6 for the % change field. Then you just need to skip the first record in your processing.
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 500 total points
ID: 39219178
Hi,

If you change the "HDR=Yes" part of the connection string to "HDR=No" you should be able to refer to the columns of data in your SQL SELECT statement as follows:

SELECT [F1], [F2], [F3], [F4],.... [F"N"] {where "N" is how many columns are present within the file}
FROM {filename}

(i.e. use the "SELECT [F{column number}]" syntax instead of "SELECT *")

However, if you wish to know the names of the headings to the columns you should either open the file once to retrieve the first few rows & write code to then extract the heading titles you require before re-querying the file contents (for a second time) with named headings...

...or import the first two records only (SELECT TOP 2 * FROM...) still with the "HDR=No" attribute within the connection string.  You can then review the resultset to gain the column headings.

BFN,

fp.
0
 

Author Closing Comment

by:iamnamja
ID: 39219189
Thanks while waiting, I did just this... I queried it twice. Once to get the header, and once to use the header to query it.  Thanks!
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39219257
Sorry I took seven minutes to reply! ;)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

688 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