Results 1 to 8 of 8
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    VBA To Import Text File

    I have a text file that has no delimitation, but I have queried MSysIMEXSpecs and gained the start position as well as the width of each column in my text file that I need to import. Question being, is it possible to take this start position and width position from MSysIMEXSpecs and create VBA that will import the text file?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Yes.
    Open the text file for input,
    read a line, parse the line, loop until EOF

    You could use the Mid() function since you know the width of each column.
    Could set up a loop calculating the start position if the width is fixed.

    Would need to see a couple of lines from the text file........

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by ssanfu View Post
    Yes.
    Open the text file for input,
    read a line, parse the line, loop until EOF

    You could use the Mid() function since you know the width of each column.
    Could set up a loop calculating the start position if the width is fixed.

    Would need to see a couple of lines from the text file........
    Attached is a workbook filled with garbage data to show the structure of the txt fileAttachment 24403

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Didn't get a workbook, just a text file.
    Can't tell from the garbage file.... How many columns? Width of each column?

    Column start
    Column Width






    Have you started any VBA code????

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I do not have any VBA syntax yet, was not sure how to code this process...Below is what the table shows, more than just column start and width but should help

    And this is for Access 2013 not 2000 like my header info shows.
    Code:
    Attributes    DataType    FieldName    IndexType    SkipColumn    SpecID    Start    Width 
    0    10    1    0    FALSE    2    225    15 
    0    10    10    0    FALSE    2    360    15 
    0    10    11    0    FALSE    2    375    15 
    0    10    12    0    FALSE    2    390    15 
    0    10    13    0    FALSE    2    405    15 
    0    10    14    0    FALSE    2    420    15 
    0    10    15    0    FALSE    2    435    15 
    0    10    16    0    FALSE    2    450    15 
    0    10    17    0    FALSE    2    465    15 
    0    10    18    0    FALSE    2    480    15 
    0    10    19    0    FALSE    2    495    15 
    0    10    2    0    FALSE    2    240    15 
    0    10    20    0    FALSE    2    510    15 
    0    10    21    0    FALSE    2    525    15 
    0    10    22    0    FALSE    2    540    15 
    0    10    23    0    FALSE    2    555    15 
    0    10    24    0    FALSE    2    570    15 
    0    10    25    0    FALSE    2    585    15 
    0    10    26    0    FALSE    2    600    15 
    0    10    3    0    FALSE    2    255    15 
    0    10    4    0    FALSE    2    270    15 
    0    10    5    0    FALSE    2    285    15 
    0    10    6    0    FALSE    2    300    15 
    0    10    7    0    FALSE    2    315    15 
    0    10    8    0    FALSE    2    330    15 
    0    10    9    0    FALSE    2    345    15 
    0    10    100    0    FALSE    2    68    60 
    0    10    101    0    FALSE    2    128    30 
    0    10    102    0    FALSE    2    158    13 
    0    10    103    0    FALSE    2    203    22 
    0    10    104    0    FALSE    2    188    15 
    0    10    105    0    FALSE    2    171    6 
    0    10    106    0    FALSE    2    17    13 
    0    10    107    0    FALSE    2    1    16 
    0    10    108    0    FALSE    2    30    38 
    0    10    109    0    FALSE    2    177    11

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I created a form and added a button.
    The code if for the click event of the button. (I threw this together - seems to work)
    Code:
    Private Sub cmdParseFile_Click()
        '*******************************
        ' requires a reference set to Microsoft Office xx.0 Object Library
        '*******************************
    
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
        Dim fso, MyFile
        Dim fDialog As Object
        Dim TextLine As String
        Dim FileName As String
    
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    
        With fDialog
            .AllowMultiSelect = False
            .Title = "Select a File To Copy"
            .Filters.Clear
            .Filters.Add "Text", "*.txt"
            .Filters.Add "Text", "*.csv"
    
            .InitialFileName = Application.CurrentProject.Path
            .Show
            If .SelectedItems.Count = 0 Then
                MsgBox "No file selected."
            Else
                FileName = fDialog.SelectedItems(1)
            End If
        End With
    
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        ' Open the file for input.
        Set MyFile = fso.OpenTextFile(FileName, ForReading)
    
        ' Read from the file
        Do While MyFile.AtEndOfStream <> True
            TextLine = MyFile.ReadLine
    
            '------------------------------------
            'your code to parse the data goes here
            
            Debug.Print TextLine
            '------------------------------------
    
        Loop
        MyFile.Close
        Set MyFile = Nothing
        Set fDialog = Nothing
        MsgBox "Done"
    End Sub
    Don't know what you want to do with the data, soooo.........

    Have fun

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I am wanting to take the file and import it into an access table.

    Thank you for the syntax, I will test this evening when I can get back to my computer.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You still have to write code to parse the line and append the data to the correct table..

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

Similar Threads

  1. Import text file to table
    By anishkgt in forum Access
    Replies: 11
    Last Post: 10-19-2014, 03:52 PM
  2. import text file with text date
    By rody in forum Import/Export Data
    Replies: 5
    Last Post: 09-01-2014, 12:50 AM
  3. import text file over 255 fields
    By belmontj in forum Programming
    Replies: 2
    Last Post: 04-29-2012, 01:53 PM
  4. Import Text File
    By vvasudev in forum Import/Export Data
    Replies: 1
    Last Post: 02-17-2012, 01:57 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