Results 1 to 6 of 6
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    Browse for excel files and append to Access table.

    I have an Access 2007 database called BusRoutes with two tables in it called Routes and Stops. I also have several excel spreadsheets that are based on an excel template that are populated by the drivers. These spreadsheets should always have the same format but not the same file names. I am trying to create a button that will perform the following actions:


    Allow the user to navigate to the excel spreadsheet, select the “Bus Routes” tab, and append all the data to the “Routes” Access table.


    Navigate to the same excel spreadsheet and select the “Bus Stop” tab and append all the data to the Access “Stops” table.


    The excel spreadsheets could be stored anywhere. I have no control over where on the driver’s computer he or she stores the excel spreadsheets.


    The spreadsheet names will differ as their naming conventions’ are based on the route number and school year they are driven. The template is macro enable so will always end .xlsm.


    The excel spreadsheets header names exactly match the corresponding table field names they are getting imported into. Tab names and header info are not alterable in the spreadsheet.


    Oh and "e-mail_add" is a hyperlink is that makes any difference.


    I know that I have to use TransferSpreadsheets Action to append the data but I am at a loss as to how to navigate to the spreadsheet, select the individual tabs, and pass this variable to the TransferSpreadsheets action.


    I tried this as an attempt to at least try to return a file name value but bombed out at the second line with following error: Run time error-2147467259(80004005)': Method 'FileDialog' of aobject'_Application' failed.

    Code:
    Set dlg = Application.FileDialog(msoFileDialogFilePicker)
    
    dlg.Title = "Select Excel Spreadsheet to import"
    dlg.AllowMultiSelect = False
    dataPath = dlg.SelectedItems(1)
    Me!browseDataPath = dataPath
    MsgBox("File is " & dataPath, vbOKOnly, "Check file name")
    
    End If
    Also tried :



    Code:
    Private Sub Command0_Click()
        Dim strFilter As String
        Dim strInputFileName As String
    
        strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsm)", "*.xlsm")
    
        strInputFileName = ahtCommonFileOpenSave( _
                                Filter:=strFilter, _
                                OpenFile:=True, _
                                DialogTitle:="Choose an excel macro enabled file...", _
                                Flags:=ahtOFN_HIDEREADONLY)
    
        If Len(strInputFileName) > 0 Then
          MsgBox ("file name is " & strInputFileName)
        Else
            'No file chosen, or user canceled
        End If
    
    End Sub
    Bombed out at ahtAddFilterItem. Sub or Function not defined.

    Help anyone?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The file picker can pick the file but it cannot select the worksheet tab. http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    If the tab names never change, can include them in the range reference. Cell references must be included in the range. This might also be an issue if the range of data cells is always different. What would be the maximum?
    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Try:
    Code:
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    Dim vrtSelectedItem As Variant, strFile As String
    fd.AllowMultiSelect = False
    With fd
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                strFile = vrtSelectedItem
            Next vrtSelectedItem
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Routes", strFile, True, "Bus Routes!A1:A10"
        End If
    End With
    Set fd = Nothing
    Figuring out which category of spreadsheet is selected is also an issue. Is there any consistency in the workbook names?
    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
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    I copied your code and ran it but got the compile-time error "User-defined types not defined" on this line:
    Dim fd As FileDialog

    Ideas as to why? In VB window I went to Tools->References and have the first 4 boxes checked ( VB for Applications, MS Access 12.0 Object Library, OLE Automation, and MS Office 12.0 Access database engine Object Library.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Microsoft Office x.0 Object Library seems to make it work.
    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.

  5. #5
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    That did the trick. As you mentioned, the code works great when the range is static; however, the Bus Stops tab will have the same number of columns, but the rows will vary from 1 record to several hundred. When I declare a large range, I get an error message telling me that not all the data will be appended (which is correct as the range is larger than the records) but the error message is annoying and may freak out the user. Is there a work around when you don't know the number of records (i.e. the number of Excel rows?)

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Maybe error handler code could deal with the error.

    Otherwise, would require opening the Excel file as a VBA object and manipulating the file to discover the last occupied row.
    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.

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

Similar Threads

  1. Combine Multiple Access Files into One and Append Tables
    By KLTurner in forum Import/Export Data
    Replies: 10
    Last Post: 01-02-2014, 11:38 AM
  2. Replies: 3
    Last Post: 12-18-2013, 02:23 PM
  3. Replies: 10
    Last Post: 12-28-2012, 02:06 PM
  4. Replies: 1
    Last Post: 06-27-2012, 07:39 AM
  5. Replies: 1
    Last Post: 02-21-2011, 09:55 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