How to check csv header before Access 2000 import?

machine_run
machine_run used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support
Commented:
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 Liason
Most Valuable Expert 2012

Commented:
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?

Author

Commented:
JC, just trying to compare the header of the new file with what I expected to see.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
So comparing a "Reference" header to the current header?  Correct?
Most Valuable Expert 2014
Commented:
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
        Else
            If UBound(mysplit) > 1 Then
                'do my checking here
            End If
        End If
    Next x
    If f.AtEndOfStream Then
        Exit Do
    End If
Loop


And close up at the end
f.Close
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 (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
I use Nick's method, but not the file system object.   I just use an Open and a Input in VBA.

Jim.

Author

Commented:
Jim,
Would you have some example code showing that?
Thanks
IT / Software Engineering Consultant
Top Expert 2016
Commented:
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)
   objFile.Close
   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
   Next
   
   ' Header matches
   FileHeaderIsValid = True

End Function

Open in new window

~bp

Author

Commented:
THANKS!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial