How to check csv header before Access 2000 import?

A company leaves a csv file on a server every night. A script downloads it and then an Access 2000 MDB attempts to import it with an Import Specification.

However, the company may change the header at any time, including changing the order of the fields, names of the fields, or adding or deleting fields. This happens rarely.

What is a way to check the header of the csv file first, perhaps by comparing it to a saved version?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Make a .bat file of something like this...

set goodhdr=col1,col two,col3,col four
set csvfile=chkthis.csv

find /i "%goodhdr%" "%csvfile%"
if %errorlevel% equ 1 echo differs& goto :eof

echo Launch access here

Open in new window

Jeffrey CoachmanMIS LiasonCommented:
And what if they are different...?
...then what?

Are you comparing all subsequent files to one standard (reference) file, ...or are you wanting to just compare the file being imported to the previous file?
machine_runAuthor Commented:
JC, just trying to compare the header of the new file with what I expected to see.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Jeffrey CoachmanMIS LiasonCommented:
So comparing a "Reference" header to the current header?  Correct?
Not the header per se, but you can certainly open the file and look at it's contents in VBA, and do some checking.

'You get a filesystemobject on the go
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")

'Get a textstream going
Dim f As TextStream
Set f = fs.OpenTextFile("c:\SomeVeryNicePlace\MyFile.csv", ForReading, TristateFalse)

'Fix the path to what you need via FileDialog or hard-code it
'instead of "c:\SomeVeryNicePlace\MyFile.csv"

'Then you figure out what line and column you want to read and do some checking
'You do that with a Do While and ReadLine
'I read a line into Split() based on the delimiter and then can do stuff with that row and column by index
'Each ReadLine issued moves you one carriage return further in the file.
'Mine are at max 5 columns wide
Dim x as long
Dim mysplit() As String
Do While Not f.AtEndOfStream
    mysplit = Split(f.ReadLine, vbTab)
    For x = 1 To 5
        If f.AtEndOfStream Then
            Exit Do
            If UBound(mysplit) > 1 Then
                'do my checking here
            End If
        End If
    Next x
    If f.AtEndOfStream Then
        Exit Do
    End If

And close up at the end
Set f = Nothing
Set fs = Nothing

In my case, there can be three different formats, and each has something tell-tale in the first 10 rows that flag them.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
I use Nick's method, but not the file system object.   I just use an Open and a Input in VBA.

machine_runAuthor Commented:
Would you have some example code showing that?
Bill PrewIT / Software Engineering ConsultantCommented:
Here is a sample approach in a VBA function that you could work in to your Access process, see if this makes sense.  Basically you specify the desired column titles in the function, and then it reads the first line of the specified file, compares the titles, and returns a true or false indicating if they matched.

Option Explicit

Sub Tester()
   MsgBox FileHeaderIsValid("c:\temp\foo.txt")
End Sub

Function FileHeaderIsValid(ByVal strFile As String) As Boolean

   ' Text file I/O constants
   Const ForReading = 1
   Const ForWriting = 2
   Const ForAppending = 8

   Dim strDelim As String
   Dim objFSO As Object
   Dim objFile As Object
   Dim arrMaster As Variant
   Dim arrHeader As Variant
   Dim i As Integer

   ' Specify CSV delimiter here, and master column names in CSV
   strDelim = ","
   arrMaster = Array("col1", "col2", "col3")

   ' Create filesystem object
   Set objFSO = CreateObject("Scripting.FileSystemObject")

   ' Open CSV file for reading
   Set objFile = objFSO.OpenTextFile(strFile, ForReading, False)
   ' Split First line by CSV delimiter, close file
   arrHeader = Split(objFile.ReadLine, strDelim)
   Set objFile = Nothing
   Set objFSO = Nothing
   ' Make sure the same number of columns are defined in the header as the master
   If UBound(arrHeader) <> UBound(arrMaster) Then
      FileHeaderIsValid = False
      Exit Function
   End If
   ' Make sure each column header title matches the master
   For i = 0 To UBound(arrHeader)
      If LCase(Replace(Trim(arrHeader(i)), """", "")) <> LCase(arrMaster(i)) Then
         FileHeaderIsValid = False
         Exit Function
      End If
   ' Header matches
   FileHeaderIsValid = True

End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
machine_runAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.