How do I use File Dialog box to import a file

S

scorpiorc

Hi, I'm trying to make a file dialog box which will allow the user to select
an Excel file to import into the table "Begin" in Access. I'm getting an
error at the point where the import process should begin. Can anyone help
with the code? Here is the code I have so far:

Option Compare Database
Option Explicit

Private Sub cmdFileDialog_Click()

Dim fDialog As Office.FileDialog
Dim varFile As Variant

' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Don't allow the user to make multiple selections in the dialog box.
.AllowMultiSelect = False

' Set the title of the dialog box.

.Title = "Select File to Import"

' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "Excel Files", "*.XLS"
.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then


Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With

DoCmd.TransferSpreadsheet acImport, 8, "Begin", varFile, True, ""

End Sub
 
B

Brendan Reynolds

Your code never assigns any value to the 'varFile' variable. You declare it
at the start of the code, then you attempt to use it in the call to
TransferSpreadsheet at the end, but nowhere in between is any value ever
assigned to the variable.

I don't use the Office.FileDialog object, but my guess, from looking through
the properties it makes available, would be that your probably need to add
something like the following to your code ...

If .Show = True Then

'Add this line to your existing code. SelectedItems collection
appears to be 1-based.
varFile = .SelectedItems(1)

Else

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top