Solved

Select file for upload from browser in access2010

Posted on 2013-12-19
5
337 Views
Last Modified: 2014-09-13
I have code that let's the user select files from the browser but then populates a list box  but I need to have the user only select ONE file, have this populate a text box on the form and then use this to be passed into the procedure to Import an excel worksheet.  My attempts to change the excising code do not work, so any suggestions on how to go about this would be appreciated.  Normally I would do research, but. Y deadline is too tight.

Sandra
0
Comment
Question by:ssmith94015
5 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 39730427
This code works in access 2010 as well as earlier versions of Access:

http://access.mvps.org/access/api/api0001.htm

Place the code in a standard module, and look for the function "TestIt", which shows you how to call the file dialog function, and get the user selected filename.
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 250 total points
ID: 39732435
This code lets you select a file; after getting the file name, you can write it to a textbox, export it to Excel, or whatever:

Public Function SelectFile() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009
'Word, Access and Excel have a FileDialog property of their Application
'object; Outlook does not

On Error GoTo ErrorHandler

   Dim fd As Office.FileDialog
   Dim varSelectedItem As Variant
   Dim strFileNameAndPath As String
   
   'Create a FileDialog object as a File Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   
   With fd
      'Set AllowMultiSelect to True to allow selection of multiple files
      .AllowMultiSelect = False
      .Title = "Browse for File"
      .ButtonName = "Select"
      .Filters.Clear
      'Modify filters as needed
      .Filters.Add "Documents", "*.doc; *.txt", 1
      .InitialView = msoFileDialogViewDetails
      If .Show = -1 Then
         'Get selected item in the FileDialogSelectedItems collection
         For Each varSelectedItem In .SelectedItems
            strFileNameAndPath = CStr(varSelectedItem)
         Next varSelectedItem
      Else
         Debug.Print "User pressed Cancel"
         strFileNameAndPath = ""
      End If
   End With
   
   SelectFile = strFileNameAndPath
   
ErrorHandlerExit:
   Set fd = Nothing
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in SelectFile procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39735949
I used to use the API call provided by mbizup, but now use the Office.FileDialog method used in Helen's example;  I find it easier to use.
0
 

Author Comment

by:ssmith94015
ID: 39736775
I have not forgotten this, am trying suggestions amid system crisis eps!
0
 

Author Closing Comment

by:ssmith94015
ID: 40320957
Both were useful.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

756 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