Import Excel Sheet Command button (1 Viewer)

docrennoc

New member
Local time
Today, 08:14
Joined
Sep 26, 2018
Messages
4
I will preface this by saying this is my first attempt at access coding ever! I am trying to work this out in Access 2010. I need an automated process so that different users can select tables from different places and it go to the correct table without any worries. I have made the code work for the dialog box to appear and select a file. I'm lost on the next part. Please spell this out for me if possible. THANKS!!
 

isladogs

MVP / VIP
Local time
Today, 14:14
Joined
Jan 14, 2017
Messages
18,209
docrenoc

Welcome to AWF
You had piggy-backed onto an 11 year thread.
I've moved your post to its own thread as you are more likely to get answers
Not sure the title is appropriate - if you want it changed, we can do so.
 

docrennoc

New member
Local time
Today, 08:14
Joined
Sep 26, 2018
Messages
4
Thank you!

here is the code I am dealing with for reference.

Code:
Private Sub cmdSelectExcelFile_Click()
    Dim dlg As FileDialog
    Dim FileSelected As String
    Set dlg = Application.FileDialog(msoFileDialogFilePicker)
    With dlg
        .Title = "Select the Excel file to import"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "xlsx Files", "*.xlsx", 1
        .Filters.Add "All Files", "*.*", 2
        If .Show = -1 Then
            FileSelected = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
End Sub
 
Last edited by a moderator:

isladogs

MVP / VIP
Local time
Today, 14:14
Joined
Jan 14, 2017
Messages
18,209
I've put your code in code tags (# button on post text window) to make it easier to read

This is fairly standard code for selecting files - as written it will allow .xlsx or any other file type. Is that what you want?

Have you tried the code & if so what issues do you have with it?
 

docrennoc

New member
Local time
Today, 08:14
Joined
Sep 26, 2018
Messages
4
I think that I will need to limit the file types to xls and xlsx, that is not a problem though. The next thing I need the code to do is take the selected file and append it to a table in the database, "Table - Referrals Fillable"
 

docrennoc

New member
Local time
Today, 08:14
Joined
Sep 26, 2018
Messages
4
So this is the final code. I managed to get this to work in the way I needed.

Private Sub cmdSelectExcelFile_Click()
Dim dlg As FileDialog
Dim FileSelected As String
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
With dlg
.Title = "Select the Excel file to import"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "xlsx Files", "*.xlsx", 1
.Filters.Add "All Files", "*.xls*", 2
If .Show = -1 Then
FileSelected = .SelectedItems(1)
Else
Exit Sub
End If
End With
Dim importfile As String
importfile = FileSelected
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Table - Referrals Fillable", importfile, True
End Sub
 

isladogs

MVP / VIP
Local time
Today, 14:14
Joined
Jan 14, 2017
Messages
18,209
You forgot the code tags.

As written, this will also allow other Excel file types like .xlsm

You can combine file types on the same line
Suggest you replace both filter.add lines with
Code:
.Filters.Add "Excel files","*.xls;*.xlsx"

OR if you want any xls type file then use
Code:
.Filters.Add "Excel files","*.xls*"
 

Users who are viewing this thread

Top Bottom