Results 1 to 4 of 4
  1. #1
    Gloverb06 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    2

    Import Specs and Text Files

    I currently have code in place below that allows the user to click a button and then a file dialog box appears where they can pick a single text file. The text file then goes through an import spec in access and gets saved to a table. I am having trouble with what I want to do next, and cannot find much help so far.

    Problems:

    1) Problem with FileName=getFileName – Run time error 2522 the action or method requires a file name argument. This happens when the user does not pick a text file and cancels.

    2) How can the program read through the user selected text file before the import and read through the text line by line
    a. If any line in a specific position(position 35 & 36) in the text file reads ZZ do the ZZ IMPORT (put all lines that have ZZ in this position of text file into a separate table)
    b. If any line in a specific position(position 35 & 36) in the text file reads UP do the UP IMPORT (put all lines that have UP in this position of text file into a separate table)

    3) How can I name the tables that are output after the import after positions 9-18 of the first line of the text file?

    4) How to export all access tables to excel that the user selects in access (I envision a export to excel button), not just a list of specific tables, but all the ones that the user selects
    a. How to name the excel document after the same name of the table?



    Code I have so far for user to select file and use the import.

    Function getFileNameUpImport() As String
    Dim fDialog As Object
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    Dim varFile As Variant


    With fDialog


    .AllowMultiSelect = False
    .Title = "Select File to Import :"
    .InitialFileName = "I:\FTP2LAN\DRS3240R" 'This is probably going to change to a sharepoint address

    If .Show = True Then
    For Each varFile In .SelectedItems
    getFileName = varFile
    Next
    End If

    'This should be a part of the if statment; if position X is UP then use this import, etc...
    DoCmd.TransferText _
    TransferType:=acImportFixed, _
    SpecificationName:="UP IMPORT", _
    TableName:="newTable", _
    FileName:=getFileName, _
    HasFieldNames:=True

    End With

    End Function


    This code is what I have so far to export the access tables. I know it is wrong, because I want to be able to export the tables that the user selects in access.

    Private Sub Command0_Click()

    strPath = "H:\UserData\Desktop\newTable.xlsx"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryEscByDate", strPath
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryCreatedByDate", strPath
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryClosedByDate", strPath
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryCreatedByUsers", strPath
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qrySummaries", strPath

    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Some thoughts.................

    The "TransferText" command is a "ALL of Nothing" type of command. All of the text file gets imported into a table. There is no provision to import 1 text file into multiple tables.

    However, you CAN use VBA to parse the text files and do pretty much anything you want.... it just takes code.
    I have code to pick a CSV file from a folder open it and process it line by line. The CSV file has approx 12,150 lines in it each month

    Responses:
    1) Problem with FileName=getFileName – Run time error 2522 the action or method requires a file name argument. This happens when the user does not pick a text file and cancels.
    1) You need error handling


    2) How can the program read through the user selected text file before the import and read through the text line by line
    a. If any line in a specific position(position 35 & 36) in the text file reads ZZ do the ZZ IMPORT (put all lines that have ZZ in this position of text file into a separate table)
    b. If any line in a specific position(position 35 & 36) in the text file reads UP do the UP IMPORT (put all lines that have UP in this position of text file into a separate table)
    2) You could import to a table, then use queries to append records to specific tables. Or, using the VBA method, you might have to read through the file multiple times but it can be done.


    3) How can I name the tables that are output after the import after positions 9-18 of the first line of the text file?
    3) Not sure what you mean...


    4) How to export all access tables to excel that the user selects in access (I envision a export to excel button), not just a list of specific tables, but all the ones that the user selects
    a. How to name the excel document after the same name of the table?
    4) Use a multi-select list box to select the tables, and use code to loop through the list box.
    a. Could try the Rename command.


    where they can pick a single text file
    Code:
    For Each varFile In .SelectedItems
             getFileName = varFile
           Next
        End If
    If you can select only one file, why the For...Each loop? Doesn't hurt, but it is a waste of time.


    This is my basic code to select a file (modified for your example).
    Code:
    Private Sub btnImportFile_Click() 
    '
    ' Needs a reference set to 'Microsoft Office xx.0 Object Library
    '
        
        On Error GoTo Err_btnImportFile_Click
    
        Dim fd As FileDialog
        Dim sFile As String
    
        'file picker
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .Title = "Select a Text File!"    '1) To set the caption of the dialog box, set the Title property
    
            .InitialFileName = "I:\FTP2LAN\DRS3240R\"    '2) Set the oddly named InitialFileName property to determine the initial folder selected
            .InitialView = msoFileDialogViewDetails        '3) Set the InitialView property to control how your files appear on screen (as a list, icons, etc.)
            .Filters.Clear        '4) To set the filters (you can have as many as you like) first clear any existing ones, then add them one by one
            .Filters.Add "Text Files", "*.txt"
            .FilterIndex = 1        ' if there's more than one filter, you can control which one is selected by default
            .ButtonName = "Choose a Text file"        '5) Set the ButtonName property to control the text on
            If .Show = -1 Then
                sFile = .SelectedItems(1)        
                'display name and path of file chosen
                '            MsgBox strFileName
                
                'Do something
                DoCmd.TransferText _
                        TransferType:=acImportFixed, _
                        SpecificationName:="UP IMPORT", _
                        TableName:="newTable", _
                        FileName:=sFile, _
                        HasFieldNames:=True
                        
            Else
                MsgBox "You chose cancel"       'didn't choose anything (clicked on CANCEL)
            End If
        End With
    
    
    Exit_btnImportFile_Click:
        Set fd = Nothing
        Exit Sub
    
    Err_btnImportFile_Click:
        MsgBox Err.Description
        Resume Exit_btnImportFile_Click
    
    End Sub

  3. #3
    Gloverb06 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    2
    So I now know what you mean by TransferText is an all or nothing type of command. I came up with code to say if at position x of a text file = 'zz' then use a specific import spec, but it imports every single record not just the ones with 'zz' at the specific position. So I am considering doing a query to take the records from the original table(That uses an import spec) where a specific field = 'zz' and put them in a new table. Many of the fields(positions of the text file) are shared no matter what is in position 9 & 10 of the text file, but the problem I am running into is the fields definitions are not the same for each line. Example: if line 1 has 'up' at position 9 & 10, then positions 345-380 could mean something completely different from a line that has 'zz' at position 9 & 10. Let me know if this does not make sense. Also how do you put your code in a nice scroll down window so it doesn't take up so much room. Thanks

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also how do you put your code in a nice scroll down window so it doesn't take up so much room
    In the Quick Reply menu, click on the hash (#) sign. Paste the VBA between the "code" tags.



    Many of the fields(positions of the text file) are shared no matter what is in position 9 & 10 of the text file, but the problem I am running into is the fields definitions are not the same for each line
    So your options are:
    1) Import the CSV file into a table, then use queries to copy/append records to different tables
    2) Write lots of VBA code to determine what is in positions 9 & 10, then append to the "proper/correct" table. I've done this when I had to do calculations on each line, then write to a specific CSV file (1 of 3) that was eventually imported into Excel.

    Sorry I can't be more specific - I have no idea what your CSV file looks like. But you seem to be making progress....

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

Similar Threads

  1. Deleting old import specs
    By Paintballlovr in forum Import/Export Data
    Replies: 8
    Last Post: 03-12-2015, 09:25 AM
  2. Replies: 1
    Last Post: 10-24-2014, 05:55 AM
  3. Import Specs gone after splitting database
    By thart21 in forum Import/Export Data
    Replies: 1
    Last Post: 07-16-2011, 04:06 PM
  4. Text Import Specs
    By orcinus in forum Access
    Replies: 3
    Last Post: 06-23-2010, 11:50 AM
  5. Cannot import excel or text files
    By donald_s in forum Access
    Replies: 2
    Last Post: 04-13-2010, 11:48 PM

Tags for this Thread

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