Solved

OpenFileDialog in Access 2.0

Posted on 1998-08-07
1
374 Views
Last Modified: 2008-02-01
I am implementing a macro which will perform an import of a text file, but I want the file to be selectable by a dialog box.  How can I go about selecting a file using Access 2.0, assigning the filename to a variable and then using the variable in the TransferText macro action.
0
Comment
Question by:cwirrgan
1 Comment
 
LVL 8

Accepted Solution

by:
Helicopter earned 100 total points
ID: 1958539
This is from MSDN. Try it out, it worked for me. It opens the common dialog box which later became integral to access and VB

Put this in general declarations of a module

 Option Compare Database
 Option Explicit

   Type tagOPENFILENAME
        lStructSize As Long
           hwndOwner As Integer
        hInstance As Integer
        lpstrFilter As Long
        lpstrCustomFilter As Long
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As Long
        nMaxFile As Long
        lpstrFileTitle As Long
        nMaxFileTitle As Long
        lpstrInitialDir As Long
        lpstrTitle As Long
        Flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As Long
        lCustData As Long
           lpfnHook As Long
        lpTemplateName As Long
   End Type

   Declare Function GetOpenFileName% Lib "COMMDLG.DLL" ( OPENFILENAME As tagOPENFILENAME)
   Declare Function GetSaveFileName% Lib "COMMDLG.DLL" (OPENFILENAME As tagOPENFILENAME)
   Declare Function lstrcpy& Lib "Kernel" (ByVal lpDestString As Any, ByVal lpSourceString As Any)

Dim OPENFILENAME As tagOPENFILENAME

Global Const OFN_READONLY = &H1
Global Const OFN_OVERWRITEPROMPT = &H2
Global Const OFN_HIDEREADONLY = &H4
Global Const OFN_NOCHANGEDIR = &H8
Global Const OFN_SHOWHELP = &H10
Global Const OFN_ENABLEHOOK = &H20
Global Const OFN_ENABLETEMPLATE = &H40
Global Const OFN_ENABLETEMPLATEHANDLE = &H80
Global Const OFN_NOVALIDATE = &H100
Global Const OFN_ALLOWMULTISELECT = &H200
Global Const OFN_EXTENSIONDIFFERENT = &H400
Global Const OFN_PATHMUSTEXIST = &H800
Global Const OFN_FILEMUSTEXIST = &H1000
Global Const OFN_CREATEPROMPT = &H2000
Global Const OFN_SHAREAWARE = &H4000
Global Const OFN_NOREADONLYRETURN = &H8000
Global Const OFN_NOTESTFILECREATE = &H10000

Global Const OFN_SHAREFALLTHROUGH = 2
Global Const OFN_SHARENOWARN = 1
Global Const OFN_SHAREWARN = 0

   '-------------------------------------------------------
   ' Open Common Dialog Function


   '-------------------------------------------------------
Then create a function:

Function OpenCommDlg ()
Dim Message$, Filter$, FileName$, FileTitle$, DefExt$
Dim Title$, szCurDir$, APIResults%

       '*Define the filter string and allocate space in the "c" string
filter$ = "Access(*.mdb)" & Chr$(0) & "*.MDB;*.MDA" & Chr$(0)
Filter$ = Filter$ & "Text(*.txt)" & Chr$(0) & "*.TXT" & Chr$(0)
Filter$ = Filter$ & "Batch(*.bat)" & Chr$(0) & "*.BAT" & Chr$(0)
filter$ = Filter$ & Chr$(0)

       '* Allocate string space for the returned strings.
FileName$ = Chr$(0) & Space$(255) & Chr$(0)
FileTitle$ = Space$(255) & Chr$(0)

       '* Give the dialog a caption title.
Title$ = "My File Open Dialog" & Chr$(0)

       '* If the user does not specify an extension, append TXT.
DefExt$ = "TXT" & Chr$(0)

       '* Set up the default directory
szCurDir$ = CurDir$ & Chr$(0)

       '* Set up the data structure before you call 'theGetOpenFileName

OPENFILENAME.lStructSize = Len(OPENFILENAME)

       'If the OpenFile Dialog box is linked to a form use this line.
          'It will pass the forms window handle.

OPENFILENAME.hwndOwner = Screen.ActiveForm.hWnd

       'If the OpenFile Dialog box is not linked to any form use this line.
       'It will pass a null pointer.

OPENFILENAME.hwndOwner = 0&

OPENFILENAME.lpstrFilter = lstrcpy(Filter$, Filter$)
OPENFILENAME.nFilterIndex = 1
OPENFILENAME.lpstrFile = lstrcpy(FileName$, FileName$)
OPENFILENAME.nMaxFile = Len(FileName$)
OPENFILENAME.lpstrFileTitle = lstrcpy(FileTitle$, FileTitle$)
OPENFILENAME.nMaxFileTitle = Len(FileTitle$)
OPENFILENAME.lpstrTitle = lstrcpy(Title$, Title$)
OPENFILENAME.Flags = OFN_FILEMUSTEXIST Or OFN_READONLY
OPENFILENAME.lpstrDefExt = lstrcpy(DefExt$, DefExt$)
OPENFILENAME.hInstance = 0
OPENFILENAME.lpstrCustomFilter = 0
OPENFILENAME.nMaxCustFilter = 0
OPENFILENAME.lpstrInitialDir = lstrcpy(szCurDir$, szCurDir$)
OPENFILENAME.nFileOffset = 0
OPENFILENAME.nFileExtension = 0
OPENFILENAME.lCustData = 0
OPENFILENAME.lpfnHook = 0
OPENFILENAME.lpTemplateName = 0
      '* This will pass the desired data structure to the Windows API,
       '* which in turn uses it to display the Open Dialog form.

APIResults% = GetOpenFileName(OPENFILENAME)

If APIResults% <> 0 Then

           '* Note that FileName$ will have an embedded Chr$(0) at the
           '* end. You may wish to strip this character from the string.

FileName$ = Left$(FileName$, InStr(FileName$, Chr$(0)) -1)

Message$ = "The file you chose was " + FileName$
'this is the name you use in your transfer text macro

       Else
Message$ = "No file was selected"
       End If

MsgBox Message$

End Function




0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now