Results 1 to 5 of 5
  1. #1
    dancg68 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2022
    Posts
    1

    Allow user to pick worksheet

    I am trying to create a new form where the user can choose an Excel worksheet to import into the database. I found code on several pages to let the user choose the file but not the specific worksheet. Below is the code I'm using (thanks to Programming Made EZ on YouTube). I cannot find any code that lets the user choose the worksheet, but found references to creating a listbox or combobox to list the worksheets, but I don't know how to make that work. The workbook and worksheet name changes depending on the data needing to be imported.

    Private Sub btnBrowseUpdate_Click()


    Dim diag As Office.FileDialog
    Dim item As Variant

    txtFilename = ""
    lbxSheets = ""

    Set diag = Application.FileDialog(msoFileDialogFilePicker)
    diag.AllowMultiSelect = False
    diag.Title = "Select an Excel spreadsheet"
    diag.Filters.Clear
    diag.Filters.Add "Excel Spreadsheet", "*.xls, *.xlsx"

    If diag.Show Then
    For Each item In diag.SelectedItems
    Me.txtFilename = item
    Next
    End If

    End Sub

    Private Sub btnImport_Click()
    Dim FSO As New FileSystemObject

    If Nz(Me.txtFilename, "") = "" Then
    MsgBox "Please select a file"
    Exit Sub
    End If

    If FSO.FileExists(Me.txtFilename) Then
    ExcelImport.importExcelSpreadsheet Me.txtFilename, FSO.GetFileName(Me.txtFilename)
    Else
    MsgBox "File Not Found"
    End If

    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    You have code for listing worksheets but don't know how to make work - why? Exactly what are you stuck on? What is that code?

    I have never seen ExcelImport command.

    Do you want to import to existing table or create a new table?

    In future, please post code, especially lengthy snips, between CODE tags to retain indentation and readability.
    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
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm not familiar with that command either. With "standard" Excel automation, it's pretty easy. This uses a textbox named txtSheet (previous declarations, etc not shown):

    Code:
        strWorksheet = Forms!frmMain.txtSheet
        Set xlCaesars = CreateObject("excel.application")
    
        xlCaesars.Workbooks.Open (strPathCaesars)
    
        Set xlSheetCaesars = xlCaesars.Worksheets(strWorksheet)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Here is a sample file I recently created for someone on this forum (https://www.accessforums.net/showthr...665#post492665) that allows you to browse for an Excel workbook and link all sheets to the Access db and display them on a tabbed form, I hope it would give you some hints to do what you need.

    Cheers,
    Vlad
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Here's another approach that doesn't require a combobox or listbox, however, if there are a lot of sheets can be impractical. I have done this to build a list of about 20 items. More than that and gets too long for display.
    Code:
    Sub ImportSheet()
    Dim diag As Office.FileDialog
    Dim strW As String, strS As String, x As Integer, sResponse As String
    Dim xlApp As Excel.Application, xlWb As Excel.Workbook, xlSht As Excel.Worksheet
    
    
    Set diag = Application.FileDialog(msoFileDialogFilePicker)
    diag.AllowMultiSelect = False
    diag.title = "Select an Excel spreadsheet"
    diag.Filters.Clear
    diag.Filters.Add "Excel Spreadsheet", "*.xls, *.xlsx, *.xlsm"
    
    
    If diag.Show Then
        x = 1
        strW = diag.SelectedItems(1)
        Set xlApp = CreateObject("Excel.Application")
        Set xlWb = xlApp.Workbooks.Open(strW)
        For Each xlSht In xlWb.Worksheets
            strS = strS & x & " : " & xlSht.Name & vbCrLf
            x = x + 1
        Next
    End If
    sResponse = "?"
    Do While sResponse = "?"
        sResponse = InputBox("Enter number to select sheet" & vbCrLf & strS, "Select Worksheet", "?")
        If sResponse Like "[1-" & x - 1 & "]" Then
            strS = Split(strS, vbCrLf)(sResponse - 1)
            strS = Split(strS, " : ")(1)
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Test", strW, , strS & "$"
        ElseIf sResponse <> "" Then
            MsgBox "Not a valid entry. Enter a number from list or Cancel."
            sResponse = "?"
        End If
    Loop
    
    End Sub
    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. FYI: User pick list example
    By ranman256 in forum Tutorials
    Replies: 1
    Last Post: 09-01-2016, 06:09 AM
  2. Replies: 3
    Last Post: 08-29-2016, 08:00 PM
  3. User pick location to save expoerted data
    By MrDummy in forum Import/Export Data
    Replies: 6
    Last Post: 03-29-2016, 03:04 PM
  4. Can't get VBA to pick up duplicate dates
    By Remster in forum Programming
    Replies: 1
    Last Post: 04-11-2012, 06:34 AM
  5. How to pick the n-th row from a query
    By Johnny C in forum Queries
    Replies: 2
    Last Post: 07-27-2010, 05:14 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