Solved

Querying CSV file through VBA without knowing the header

Posted on 2013-06-04
4
1,949 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

929 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

11 Experts available now in Live!

Get 1:1 Help Now