Importing Two Sheets From Same Excel File w/ File Picker (1 Viewer)

donut

New member
Local time
Today, 12:10
Joined
Oct 17, 2017
Messages
2
Hi,

I'm learning VBA/Access coding as I go at work, hit a wall in my current project and looking for some help.

I have a workbook that is updated biweekly, it has a number of worksheets but I only need to import two of them into my Access database into two separate tables. One will always be the first sheet in the workbook, the other may not have a set position but will have a set name.

I use a file picker module I found the code for online to select the Excel file needed, as it is unknown who will be maintaining it later on or where they will store the files. I want to make this as "one-click" as possible.

So there's two avenues I can go down, I'm not sure which is easier/possible

1) Import both sheets at once and populate the appropriate tables
2) Somehow save the full file path from the File Picker, that I can store as a string and then call in a second DoCmd.TransferSpreadsheet line. This is what I have been trying and failing at.


Here's my applicable code

File Picker Module, I added the myfile part to try and get the file path:
Code:
Option Compare Database

'--------------------------------------------------
' File Browse Code
'--------------------------------------------------
'NOTE: To use this code, you must reference
'The Microsoft Office 14.0 (or current version)
'Object Library by clicking menu Tools>References
'Check the box for:
'Microsoft Office 14.0 Object Library in Access 2010
'Microsoft Office 15.0 Object Library in Access 2013
'Click OK
'--------------------------------------------------
Function selectFile()
    Dim fd As FileDialog
    Dim myfile As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
 
    With fd
        If .Show Then
            selectFile = .SelectedItems(1)
             myfile = .SelectedItems(1)           
       Else
            'stop execution if nothing selected
            End
        End If
    End With
 
    Set fd = Nothing
End Function

Button to import the Excel File
Code:
Public Sub testbutton_Click()
On Error GoTo testbutton_Click_Err

    'load updated data spreadsheet (always 1st tab)
    DoCmd.TransferSpreadsheet acImport, 8, "NewData", selectFile(), True
   
    'load change log sheet
    DoCmd.TransferSpreadsheet acImport, 8, "UpdateInfo", myfile, True, "Update Info!"

testbutton_Click_Exit:
    Exit Sub

testbutton_Click_Err:
    MsgBox Error$
    Resume testbutton_Click_Exit

End Sub

As it is now, I get an error "The action or method requires a File Name Argument" which I thought I had done. I have tried instead of "myfile" fd.selectFile and fd.SelectedItems(1) but those did not work either. I do not want another File Select popup as that might be confusing to the end user.

I feel like I'm missing something really simple and obvious :(
 

Ranman256

Well-known member
Local time
Today, 12:10
Joined
Apr 9, 2015
Messages
4,337
link attach both sheets as separate external tables.
then run 2 append queries, 1 for each table.
 

Mark_

Longboard on the internet
Local time
Today, 09:10
Joined
Sep 12, 2017
Messages
2,111
As a thought, have you tried putting the two together?

Code:
Function selectFile()
    Dim fd As FileDialog
    Dim myfile As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
 
    With fd
        If .Show Then
            selectFile = .SelectedItems(1)
             myfile = .SelectedItems(1)           

             'load updated data spreadsheet (always 1st tab)
             DoCmd.TransferSpreadsheet acImport, 8, "NewData", myfile, True
   
             'load change log sheet
             DoCmd.TransferSpreadsheet acImport, 8, "UpdateInfo", myfile, True, "Update Info

       Else
            'stop execution if nothing selected
            End
        End If
    End With
 
    Set fd = Nothing
End Function
This way you KNOW what should be in myfile. You may want to double check it with a msgbox though.
 

donut

New member
Local time
Today, 12:10
Joined
Oct 17, 2017
Messages
2
link attach both sheets as separate external tables.
then run 2 append queries, 1 for each table.

I don't know how to implement this by using a file picker. The new file will have a different name each time (date run) and saved in different locations depending on who is importing it.


As a thought, have you tried putting the two together?

This worked :) I had to shove it in the main Button code and not a module but it worked! I think it had to do that my programming theory is pretty weak and functions vs modules vs subs is all moonspeak to me. Looks like I should do some reading up!
 

Mark_

Longboard on the internet
Local time
Today, 09:10
Joined
Sep 12, 2017
Messages
2,111
Very glad I could help.

Worry not about the verbiage, concentrate on what needs to be done.

Think it through and it becomes far more clear. If you ask for a file name, what do you do IF they give you one? Well, you go ahead and transfer your data! If they do not (else) you simply say thank you for your time and be on your merry way. In the code I had posted I left else because you may find you need to remind users they did not actually select something.

You can put a
Code:
msgbox "No file selected, aborting"
in there if your users start complaining "But its not working!! I'm SURE I selected the file!".
 

Users who are viewing this thread

Top Bottom