Solved

Importing an Excel file into a table via VBA code

Posted on 2014-09-03
5
319 Views
Last Modified: 2014-09-05
I have this line of code which imports an Excel file into a table via VBA code.  

DoCmd.RunSavedImportExport "Import-Field Notes"

It works fine as long as the Excel file name is always the same because the saved import is always looking for that file name.  But the Excel file name will always be different even though the structure of the file will always be the same.

The user is selecting the Excel file via:

    Dim fd As FileDialog, SelectFolderOrFile
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .InitialFileName = Environ("userprofile") & "\Documents\"
        If .Show Then
            Me.txtFileName = .SelectedItems(1)
        End If
    End With

Open in new window


My question is this... what can I replace the code...

DoCmd.RunSavedImportExport "Import-Field Notes"

with that will accept the file name, no matter what it is?
0
Comment
Question by:SteveL13
  • 3
5 Comments
 
LVL 35

Expert Comment

by:PatHartman
ID: 40301043
Use the TransferSpreadsheet method.  You can specify a variable file name and you can also specify an Import/Export spec name if you need to.
0
 

Author Comment

by:SteveL13
ID: 40301079
Using the "wizard" (please see screenshot), when I get to [FileName] how do I specify the file name being imported if the file name is different each time (although the structure is the same every time)...

Example
--Steve
0
 
LVL 1

Accepted Solution

by:
Matthew Ozog earned 500 total points
ID: 40301109
Use your code from above to get the filename:

 Dim fd As FileDialog, SelectFolderOrFile
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .InitialFileName = Environ("userprofile") & "\Documents\"
        If .Show Then
            Me.txtFileName = .SelectedItems(1)
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tblNotes, .SelectedItems(1)...
        End If
    End With
0
 

Author Comment

by:SteveL13
ID: 40305788
Sorry late getting back to this but the code is not working.  Here is what I have:

       
Dim fd As FileDialog, SelectFolderOrFile
            Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .InitialFileName = Environ("userprofile") & "\Documents\"
            If .Show Then
            Me.txtFileName = .SelectedItems(1)
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTempFieldNotesImport", .SelectedItems(1), True, "A1:B63"
            End If
        End With

Open in new window


When I run the code I get an error:

Field 'NotesID' doesn't exist in destination table 'tblTempFieldNotesImport.'

The Excel file has just two columns.  The table tblTempFieldNotesImport has just two fields and they are named Field 1 and Field2.

????
0
 

Author Comment

by:SteveL13
ID: 40305876
I got it.  I just had to change the field names in the table to F1 and F2.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question