Question : Importing Text files into Access VBA

hello,

I am trying to import text files into Access using VBA (I have a button on a form).

My specifcation requires that text files must have pipe delimiters and 37 fields (unlimited rows).

I created and saved a spec in order to recogise the Pipe and am using the following code:

DoCmd.TransferText acImportDelim, "textnoheader", "Importedtable", [function that returns path to file], False

I then use

FLDChk = CurrentDb.TableDefs("mstrimportexcel").Fields.Count
If FLDChk <> 37 Then

To check if there are not 37 fields.

Unfortunatly this always proves positive even if there are more or less fields - the spec had 37 fields, i did this so i could set them all at Text and bring in all 37 (if i try and set it as less than 37 and bring in 37, it just cuts out any fields over the number set in the Spec!!!)

does anyone know a more reliable way to determine how many fields there are before importing so I can generate a wanring message and prevent the impoprt - i assume this can be done, as the manual wizard does a field count before importing?

Answer : Importing Text files into Access VBA

You beat me but only half-way :-)

Function CountFields(strFilePath As String)
Dim strFields() As String
Dim strFile As Object
Set strFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilePath, 1)
strFields = Split(strFile.ReadLine, "|")
CountFields = UBound(strFields()) + 1
strFile.Close
Set strFile = Nothing
End Function
Random Solutions  
 
programming4us programming4us