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

    File Selector And Import Selected File


    I am wanting to create a File Selector and import the selected file into access table. I have this syntax, but it does not like my TransferSpreadsheet syntax. What do I need to alter in order for the selected spreadsheet to be imported into a table named "Test"? I also have the filter set to *.xls* as it could be a .xls or a .xlsx

    Code:
    Set f = Application.FileDialog(3)
    
    
    With f
        .AllowMultiSelect = False
        .Title = "Please Select the Excel File To Import"
        .Filters.Add "Excel Files", "*.xls*"
    
    
        If .Show = True Then
            For i = 1 To f.SElectedItems.Count
                selectedfile = f.SElectedItems(i)
                DoCmd.TransferSpreadsheet(acImport, , "Test", selectedfile, True)
            Next
        Else
            MsgBox "You clicked Cancel in the file dialog box."
        End If
        
    End With

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What does the variable contain at that point? What does it "not like", and how? I don't use the parentheses, but they may be okay.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    The variable contains the full file path
    C:\TestData\TestWorkbook.xlsx

    I get a compile error of 'Syntax Error' on the DoCmd line

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Have you tried taking the parentheses out?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    looks like you may need to specify the TransferType (the 2nd parameter) - the default may not be the right format for your file - see this link

    https://msdn.microsoft.com/en-us/lib.../ff196017.aspx

    Also, try removing the brackets - they imply a value will be returned so you need to make it equal to something. Nevere tried it but perhaps it returns a Boolean e.g.

    dim OK as variant
    OK=DoCmd.TransferSpreadsheet(acImport, , "Test", selectedfile, True)

  6. #6
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Since I can import either a .xls or a .xlsx is there a way for me to check the file extension so I know to use acSpreadsheetTypeExcel9 or use acSpreadsheetTypeExcel12?

    Was following this logic --
    http://access-excel.tips/access-vba-cocmd-transferspreadsheet/

    I altered my syntax to this, but still get the same error
    Code:
    
    
    Code:
     selectedfile = f.SElectedItems(i)
                ExtFind = Split(selectedfile, ".")
                If ExtFind = ".xls" Then
                    param = acSpreadsheetTypeExcel9
                ElseIf ExtFind = ".xlsx" Then
                    param = acSpreadsheetTypeExcel12
                End If
                DoCmd.TransferSpreadsheet(acImport, param, "Test", selectedfile, True)

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I got it - thanks for the assistance, removing parens was very ehlpful

    Code:
    Dim ExtFind As String
    Dim param As String
    selectedfile = f.SElectedItems(i)
    ExtFind = Split(selectedfile, ".")
    If ExtFind = ".xls" Then
    	param = acSpreadsheetTypeExcel9
    ElseIf ExtFind = ".xlsx" Then
    	param = acSpreadsheetTypeExcel12
    End If
    DoCmd.TransferSpreadsheet(acImport, param, "Test", selectedfile, True)

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 07-23-2013, 11:57 AM
  2. Import Excel file based on a date and time in file name
    By nhylan in forum Import/Export Data
    Replies: 4
    Last Post: 04-16-2013, 03:26 PM
  3. Trying to import a CSV file
    By itm in forum Access
    Replies: 1
    Last Post: 04-13-2012, 06:00 PM
  4. Import Specific cells from Excel based on selected file.
    By Only4Access in forum Programming
    Replies: 5
    Last Post: 02-29-2012, 02:32 AM
  5. Import a .sql file?
    By Alan in forum Import/Export Data
    Replies: 1
    Last Post: 06-03-2009, 07:52 PM

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