Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OpenFileDialog in Access 2.0

Posted on 1998-08-07
1
Medium Priority
?
439 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 400 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

885 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