Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

apply header names in text file to variable names

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
muybn - 13 Jun 2007 09:48 GMT
I'm wanting to use a CSV file (supplied to me by a list company) which I will
write to a database and from which I will generate another CSV file with less
fields. I have the code for writing it to the database, but I want to test
fields in each row for certain conditions before I do that, to see which
records I want to exclude due to these conditions. Code below shows my
vehicle for looping through the records in the original CSV:

   'open text file to get values of variables
   Open strPath & "listings.csv" For Input As #1
     Do While Not EOF(1)
        Line Input #1, strScrap
       arrFieldNames = Split(strScrap, ",")
       strLead_ID = arrFieldNames(0)
       'evaluate field values here
     Loop
   Close #1

What I need to do in addition, however, is somehow assign the field names
found in the first row to the variables of all subsequent rows. In other
words, the first value in the header row found will be "Lead_ID"; then to the
value delimited in the same position in the second row, I want to assign to
the variable "strLead_ID." I want to do this a row at a time because I need
to evaluate for certain conditions in each field that will make me exclude
that record.

I want to use this naming method in case the order of the data is changed,
in which case the prior variable value str1 won't necessarily be found in
that same position later, thus better to name it according to the
corresponding header row position.

Any ideas on how to do this wi..

Signature

Bryan

Klaus Linke - 13 Jun 2007 17:10 GMT
Hi again Bryan,

You might read the first record separately, to get the headers.
In the example below, I use a collection so I can get the index (column)
from the header string.
Maybe there are more elegant ways to do it...

One thing: I'd get the list delimiter from the system, because it varies
from country to country.
Say in Germany, it's a semicolon instead of a comma.

  Dim strScrap As String
  Dim arrFieldNames As Variant
  Dim arrHeaderNames As Variant
  Dim strLead_ID As String
  Dim sLS As String ' list separator
  sLS = Application.International(WdInternationalIndex.wdListSeparator)

  'open text file to get values of variables
  Open "C:\listings.csv" For Input As #1

  Line Input #1, strScrap
  arrHeaderNames = Split(strScrap, sLS)
  Dim colHeader As New Collection
  Dim i As Long
  For i = LBound(arrHeaderNames) To UBound(arrHeaderNames)
     colHeader.Add Item:=Trim(STR(i)), key:=arrHeaderNames(i)
  Next i

  Do While Not EOF(1)
     Line Input #1, strScrap
     arrFieldNames = Split(strScrap, sLS)
     ' The Val isn't strictly needed because of implicit conversion...
     strLead_ID = arrFieldNames(Val(colHeader.Item("strLead_ID")))
     'evaluate field values here
  Loop
  Close #1

Maybe you'd get better answers in one of the general VBA or Office.VBA
groups, or in the Excel.VBA group, since the question does not deal
specifically with Word.

BTW, in the code I posted for reading a file with the
Scripting.FileSystemObject, you'd need to set a reference to the Microsoft
Scripting Runtime in "Tools > References...".
Your code looks great... I just often prefer to read the whole file into a
string, because it seemed to be faster in some applications I had, or
because the structure was too messy.
The VBA Open then had problems because it runs out of string space, unless
one creates a large enough string first.
It also seemed slower, though I'm not 100% sure.

Regards,
Klaus
muybn - 13 Jun 2007 17:43 GMT
Thanks, Klaus. The line
      strLead_ID = arrFieldNames(Val(colHeader.Item("strLead_ID")))
produced this error:
Run-time error '5':
Invalid procedure call or argument

What should I do differently?

Signature

Bryan

> Hi again Bryan,
>
[quoted text clipped - 50 lines]
> Regards,
> Klaus
Klaus Linke - 13 Jun 2007 18:18 GMT
> Thanks, Klaus. The line
>       strLead_ID = arrFieldNames(Val(colHeader.Item("strLead_ID")))
[quoted text clipped - 3 lines]
>
> What should I do differently?

Sounds like the first record does not really have a field with the content
"strLead_ID", as I understood it should?
In any case, you might want to add some error-handling code that warns you
when the CSV file does not have headers as you expect it.

Klaus
muybn - 13 Jun 2007 19:42 GMT
The field name was actually "Lead_ID" and "strLead_ID" was the variable name.
No matter, I made the changes. The error message came because I had replaced
the commas with tabs, so once I reversed that change, it ran OK.

I'm trying to set up a Select Case statement with (argh!) 45 different cases
since there are 45 different column names for which I need to match values
and assign each value to the corresponding variable. I'm a little confused as
to what I should use after Select Case; I know it should be a variable but
I'm not seeing where to pull it from and all my guesses haven't produced what
I would expect. This is the code I have so far:

   strLS = Application.International(WdInternationalIndex.wdListSeparator)
   'open text file to get values of variables
   Open "C:\movingboxes\listings.csv" For Input As #1
       Line Input #1, strScrap
       arrHeaderNames = Split(strScrap, strLS)
       For intCnt = LBound(arrHeaderNames) To UBound(arrHeaderNames)
           colHeader.Add Item:=Trim(str(intCnt)), Key:=arrHeaderNames(intCnt)
       Next intCnt
       Do While Not EOF(1)
           Line Input #1, strScrap
           arrFieldNames = Split(strScrap, strLS)
           'the Val isn't strictly needed because of implicit conversion...
           Select Case 'some variable name
               Case "Lead_ID"
                   strLead_ID = arrFieldNames(Val(colHeader.Item("Lead_ID")))
                   'MsgBox strLead_ID
               '44 other case statements!
           End Select
       Loop
   Close #1

Signature

Bryan

> > Thanks, Klaus. The line
> >       strLead_ID = arrFieldNames(Val(colHeader.Item("strLead_ID")))
[quoted text clipped - 10 lines]
>
> Klaus
Klaus Linke - 13 Jun 2007 21:10 GMT
> I'm trying to set up a Select Case statement with (argh!) 45 different
> cases since there are 45 different column names for which I need to
[quoted text clipped - 3 lines]
>                     strLead_ID =
> arrFieldNames(Val(colHeader.Item("Lead_ID")))

I don't understand where "some variable name" is coming from.
You set up the collection with the headers to be able to get at some
specific variable without knowing the column, just from the information from
the header row.
So you probably don't need any "Select Case"?

As for the error handling...
You might set up an error handler for the run-time error 5 that you get when
you don't have some expected header in the CSV file.

Regards,
Klaus
muybn - 13 Jun 2007 21:54 GMT
I wrote "some variable name" since a Select Case requires a variable after
it. I'm thinking that Select Case would be necessary for the purpose of
excluding records with certain "unworthy" fields. Let me know what you think
of the following, especially if you think I'm taking the long way around and
could write something more efficiently. Thanks again for your excellent help
and for sticking with me while learning what to do.

I made it work like this:

   Open strPath & "listings.csv" For Input As #1
       Line Input #1, strScrap
       arrHeaderNames = Split(strScrap, strLS)
       For intCnt = LBound(arrHeaderNames) To UBound(arrHeaderNames)
           colHeader.Add Item:=Trim(str(intCnt)), Key:=arrHeaderNames(intCnt)
       Next intCnt
       intCnt = 0
       Do While Not EOF(1)
           Line Input #1, strScrap
           arrFieldNames = Split(strScrap, strLS)
           With Selection
               Select Case arrHeaderNames(intCnt)
                   Case "Lead_ID"
                       strLead_ID =
arrFieldNames(Val(colHeader.Item("Lead_ID")))
'...[other cases]...
              End Select

Signature

Bryan

> > I'm trying to set up a Select Case statement with (argh!) 45 different
> > cases since there are 45 different column names for which I need to
[quoted text clipped - 16 lines]
> Regards,
> Klaus
Klaus Linke - 13 Jun 2007 22:27 GMT
I still think you don't need "Select Case" in this case:

           arrFieldNames = Split(strScrap, strLS)
           strLead_ID = arrFieldNames(Val(colHeader.Item("Lead_ID")))
           ' ... and similar lines for the other (44?) fields and their
headers.

The idea here is that colHeader.Item("Lead_ID") is the number of the column
that contains "Lead_ID" (in the header row).
So you can directly fech the value from the array arrFieldNames and stick it
in strLead_ID.

If you then want to check the fields for validity, you might use "If ...
then" or "Select Case", but for the values of your variables directly rather
than for the column number they came from:
If strLead_ID = "00000" then ...
or
Select Case Val(strLead_ID)   ' assuming here the ID represents a number
   Case 0 ' zero, or not a number at all
      ' do something
   Case 1 to 1000, 2001 to 3000
      ' do something else
   ...
End Select

Regards,
Klaus
muybn - 14 Jun 2007 01:20 GMT
Yes, I notice what you mean. I've scaled down my "Cases" to some seven of the
45 variables--those which might define the particular record as "invalid."
Now I'm on my way. I've appreciated learning about how to work with files
that you don't actually "open" in Word so that I don't have to rely so much
on mailmerge and searching. Thanks again for all your assistance. \

Signature

Bryan

> I still think you don't need "Select Case" in this case:
>
[quoted text clipped - 23 lines]
> Regards,
> Klaus
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.