Solved

OpenFileDialog in Access 2.0

Posted on 1998-08-07
1
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

628 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