Microsoft Access Forums

Go Back   Microsoft Access Forums > Access Forums > Import/Export Data

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-26-2010, 08:28 AM
johnson johnson is offline Windows XP Access 2007 (version 12.0)
Novice
 
Join Date: Jan 2010
Posts: 2
johnson is on a distinguished road
Question Verify CSV Field Names Before Importing

I've posted this question elsewhere, but no one seems to know how to fix the code...


Before importing from a CSV, I'm trying to verify:

1) How many fields are present before importing a csv file
2) If the number of fields is correct, are the field names correct as well

The CSV file uses tab as the delimiter with no text qualifier with field names in the first row. If the CSV is verified as correct, the append query is run, transferring the data from the CSV that is a linked table.

This is the code so far, and it doesn't work

Code:
Function testCoRecordsCSV()
On Error GoTo testCoRecordsCSV_Err
Dim intFile As Integer
Dim strBuffer As String
Dim strFile As String
Dim varFields As Variant
 
strFile = "c:\company\corecords.csv"
 
    If Len(Dir(strFile)) > 0 Then
 
    intFile = FreeFile()
    Open strFile For Input As #intFile
    Line Input #intFile, strBuffer
    Close #intFile
    varFields = Split(strFile, Chr(9))
    If UBound(varFields) <> 9 Then
        MsgBox "The file does not have 10 fields in it"
    Else
         If varFields(0) <> "Test1" Or _
            varFields(2) <> "Test2" Or _
            varFields(3) <> "Test3" Or _
            varFields(4) <> "Test4" Or _
            varFields(5) <> "Test5" Or _
            varFields(6) <> "Test6" Or _
            varFields(7) <> "Test7" Or _
            varFields(8) <> "Test8" Or _
            varFields(9) <> "Test9" Or _
            varFields(10) <> "Test10" Then
            MsgBox "The ten field names do not match"
   Else
       CurrentDb.Execute "APPEND_A_1_corecords", dbFailOnError
       MsgBox "File Appended"
 
    End If
  End If
End If
 
testCoRecordsCSV_Exit:
    Exit Function
 
testCoRecordsCSV_Err:
    MsgBox Error$
    Resume testCoRecordsCSV_Exit
 
    End Function
There are no errors, it just doesn't get past the first If statement. UBound always returns 0.

I've attached a zip of the database (Access 2007) and a sample csv file. Could someone take a look and see if they can tell why this isn't working? The database looks for the linked file in C:\company\

Thank you

EDIT:

I've changed
Code:
     varFields = Split(strFile, ",")
to
Code:
     varFields = Split(strBuffer, ",")
And now receive the error, "Subscript out of range"
Attached Files
File Type: zip importcheck.zip (21.8 KB, 1 views)

Last edited by johnson; 01-26-2010 at 09:04 AM. Reason: update status of problem
Reply With Quote
  #2  
Old 01-26-2010, 12:06 PM
johnson johnson is offline Windows XP Access 2007 (version 12.0)
Novice
 
Join Date: Jan 2010
Posts: 2
johnson is on a distinguished road
Default

This is all resolved.
Reply With Quote
  #3  
Old 01-26-2010, 03:53 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

Would you like to share your solution and then follow the link in my sig and mark this thread as Solved?
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
Reply

Bookmarks

Tags
csv import fields

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating field names in table without typing GHanover Database Design 2 12-14-2009 02:13 AM
Changing field names in queries scottsoo9 Queries 1 12-01-2009 02:47 PM
Sum of two field names in one record in the same table. Mr. LL Access 5 03-31-2009 07:16 PM
Quick way to stuff field names into text field kfinpgh Programming 0 01-04-2007 10:13 AM
How to extract names from 1 large field Tepin Access 1 12-04-2006 10:14 AM


All times are GMT -8. The time now is 02:41 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.