Results 1 to 3 of 3
  1. #1
    belmontj is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    1

    import text file over 255 fields

    I’m reading in a comma delimited text file using the Open [filename]for Input. No problem reading this into new tables

    My problem is: How to append the fields as a new record (or row) rather than a new field.


    Here is an illustration of how it reads now; keep in mind this goes on for over 1000 fields, but are groups of 10 (each 10 can be its own Field)

    Fld1 Fld2 Fld3 Fld4 Fld5Fld6 Fld7 Fld8 Fld9 Fld10…Fld1400

    str1, str2, str3, str4, str5, str6, str7, str8, str9, str10,…str1400

    So my code now is something like this, which treats each comma as a new field, but how can I take groups of 10 (commas) and treat it as one field and repeat this until the last comma in the row, then append.

    Const strDelimiter As String = ","
    Set dbs = CurrentDb()
    Set rst = dbs.OpenRecordset("tst", dbOpenDynaset)
    Open "filename" For Input As #1
    While EOF(1) = False
    Line Input #1, strLine
    ' split the line into an array
    varArray =Split(strLine, strDelimiter)

    rst.AddNew

    i = 0
    j = 1
    Do Until i = (somenumber)
    rst.Fields("Field" & j).Value= varArray(i)
    i = i + 1
    j = j + 1
    Loop

    rst.Update

    Wend
    rst.Close
    Set rst = Nothing
    dbs.Close
    Set dbs = Nothing
    Close #1

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Try a nested loop

    For k = 1 to 10
    rst.Fields("Field" & j) = rst.Fields("Field" & j) & varArray(i)
    i = i + 1
    Next
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Interesting problem. I would try and devise a different table structure, but it was interesting.

    Here is the code I came up with:
    Code:
    Option Compare Database
    Option Explicit
    Option Base 1
    
    Public Sub test()
       Const strDelimiter As String = ","
       Const Groups As Integer = 10
    
       Dim dbs As DAO.Database
       Dim rst As DAO.Recordset
    
       Dim TF As Integer   'text file file number
       Dim varArray         'array name
       Dim UB As Integer   ' array upper bound
       Dim GroupNum As Integer  'how many groups
       Dim i As Integer  'index for array
       Dim j As Integer  'index for array
       Dim x As Integer  'for looping
       Dim k As Integer  'which field number
       Dim sString As String
       Dim sfilename As String
       Dim rsName As String   'recordset name or SQL
       Dim strInput As String
       '-----------------------
    
       'path to text file
       sfilename = "C:\Documents and Settings\usernamePath\Desktop\TextFileName.txt"
       rsName = "tst"
       '-----------------------
       
       Set dbs = CurrentDb()
    
       'open recordset
       Set rst = dbs.OpenRecordset(rsName, dbOpenDynaset)
    
       '  open text file
       '  Open "sfilename" For Input As #TF
       TF = FreeFile
       Open sfilename For Input As #TF
    
       'loop thru text file
       While Not EOF(TF)
          sString = ""
          '  read line from text file
          Line Input #TF, strInput
          ' split the line into an array
          varArray = Split(strInput, strDelimiter)
          UB = UBound(varArray)
          GroupNum = UB \ Groups + 1
    
          j = 0
          k = 1
          rst.AddNew
          For x = 1 To GroupNum
             i = 0
             sString = ""
             'build string
             Do Until i = (Groups)
                If (j + i) < (UB + 1) Then
                   sString = sString & varArray(j + i) & ","
                End If
                i = i + 1
             Loop
             sString = Left(sString, Len(sString) - 1)
             rst.Fields(k) = sString
             j = j + 10
             k = k + 1
          Next
          rst.Update
    
       Wend
       rst.Close
       Set rst = Nothing
       Set dbs = Nothing
       Close #TF
       MsgBox "Done"
    End Sub
    You need the blue line "Option Base 1"
    The RED text is what you can/should change.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import Text File
    By vvasudev in forum Import/Export Data
    Replies: 1
    Last Post: 02-17-2012, 01:57 AM
  2. Remove/Ignore beginning of text file on import
    By Insyderznf in forum Import/Export Data
    Replies: 9
    Last Post: 10-24-2011, 04:56 PM
  3. Automaically Import Text File in Access Form
    By ract123 in forum Import/Export Data
    Replies: 1
    Last Post: 06-13-2011, 09:06 AM
  4. Import Text File Size Limit
    By wfbp in forum Import/Export Data
    Replies: 1
    Last Post: 11-04-2010, 09:05 AM
  5. Import text file question
    By sumdumgai in forum Import/Export Data
    Replies: 14
    Last Post: 03-23-2010, 07:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums