Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2004
    Location
    South Africa
    Posts
    178

    Question Unanswered: return link to file

    Good Morning
    I've got a button that needs to open a windows-explorer-like-window for me to look for a file and then return the path to that file in a textbox

    any suggestions
    thanks again
    actionant

  2. #2
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    Good morning

    Copy and past the next long code in an extra Module


    Option Compare Database
    Option Explicit


    Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type




    Public Const OFN_ALLOWMULTISELECT = &H200
    Public Const OFN_CREATEPROMPT = &H2000

    Public Const OFN_EXPLORER = &H80000 ' new look commdlg
    Public Const OFN_EXTENSIONDIFFERENT = &H400
    Public Const OFN_FILEMUSTEXIST = &H1000
    Public Const OFN_HIDEREADONLY = &H4
    Public Const OFN_LONGNAMES = &H200000 ' force long names for 3.x modules
    Public Const OFN_NOCHANGEDIR = &H8
    Public Const OFN_NODEREFERENCELINKS = &H100000
    Public Const OFN_NOREADONLYRETURN = &H8000
    Public Const OFN_NOTESTFILECREATE = &H10000
    Public Const OFN_NOVALIDATE = &H100
    Public Const OFN_OVERWRITEPROMPT = &H2
    Public Const OFN_PATHMUSTEXIST = &H800
    Public Const OFN_READONLY = &H1
    Public Const OFN_SHAREAWARE = &H4000
    Public Const OFN_SHOWHELP = &H10

    Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Public Declare Function GetOpenClipboardWindow Lib "user32" () As Long




    Function CommonFileOpenSave( _
    Optional ByRef Flags As Variant, _
    Optional ByVal InitialDir As Variant, _
    Optional ByVal Filter As Variant, _
    Optional ByVal FilterIndex As Variant, _
    Optional ByVal DefaultExt As Variant, _
    Optional ByVal filename As Variant, _
    Optional ByVal DialogTitle As Variant, _
    Optional ByVal hWnd As Variant, _
    Optional ByVal OpenFile As Variant) As Variant
    ' This is the entry point you'll use to call the common
    ' file open/save dialog. The parameters are listed
    ' below, and all are optional.
    '
    ' In:
    ' Flags: one or more of the OFN_* constants, OR'd together.
    ' InitialDir: the directory in which to first look
    ' Filter: a set of file filters, set up by calling
    ' AddFilterItem. See examples.
    ' FilterIndex: 1-based integer indicating which filter
    ' set to use, by default (1 if unspecified)
    ' DefaultExt: Extension to use if the user doesn't enter one.
    ' Only useful on file saves.
    ' FileName: Default value for the file name text box.
    ' DialogTitle: Title for the dialog.
    ' hWnd: parent window handle
    ' OpenFile: Boolean(True=Open File/False=Save As)
    ' Out:
    ' Return Value: Either Null or the selected filename
    Dim OFN As OPENFILENAME
    Dim strFileName As String
    Dim strFileTitle As String
    Dim fResult As Boolean
    ' Give the dialog a caption title.
    If IsMissing(InitialDir) Then InitialDir = CurDir
    If IsMissing(Filter) Then Filter = ""
    If IsMissing(FilterIndex) Then FilterIndex = 1
    If IsMissing(Flags) Then Flags = 0&
    If IsMissing(DefaultExt) Then DefaultExt = ""
    If IsMissing(filename) Then filename = ""
    If IsMissing(DialogTitle) Then DialogTitle = ""
    If IsMissing(hWnd) Then hWnd = Application.hWndAccessApp
    If IsMissing(OpenFile) Then OpenFile = True
    ' Allocate string space for the returned strings.
    strFileName = Left(filename & String(256, 0), 256)
    strFileTitle = String(256, 0)
    ' Set up the data structure before you call the function
    With OFN
    .lStructSize = Len(OFN)
    .hwndOwner = hWnd
    .strFilter = Filter
    .nFilterIndex = FilterIndex
    .strFile = strFileName
    .nMaxFile = Len(strFileName)
    .strFileTitle = strFileTitle
    .nMaxFileTitle = Len(strFileTitle)
    .strTitle = DialogTitle
    .Flags = Flags
    .strDefExt = DefaultExt
    .strInitialDir = InitialDir
    ' Didn't think most people would want to deal with
    ' these options.
    .hInstance = 0
    .strCustomFilter = ""
    .nMaxCustFilter = 0
    .lpfnHook = 0
    'New for NT 4.0
    .strCustomFilter = String(255, 0)
    .nMaxCustFilter = 255
    End With
    ' This will pass the desired data structure to the
    ' Windows API, which will in turn it uses to display
    ' the Open/Save As Dialog.
    If OpenFile Then
    fResult = GetOpenFileName(OFN)
    Else
    fResult = GetSaveFileName(OFN)
    End If

    ' The function call filled in the strFileTitle member
    ' of the structure. You'll have to write special code
    ' to retrieve that if you're interested.
    If fResult Then
    ' You might care to check the Flags member of the
    ' structure to get information about the chosen file.
    ' In this example, if you bothered to pass in a
    ' value for Flags, we'll fill it in with the outgoing
    ' Flags value.
    If Not IsMissing(Flags) Then Flags = OFN.Flags
    CommonFileOpenSave = TrimNull(OFN.strFile)
    Else
    CommonFileOpenSave = vbNullString
    End If
    End Function



    Function DBSuchen(DBName As String, Optional tabName As Variant)
    On Error Resume Next
    Dim strFilter As String
    Dim strInputFileName As String

    strFilter = AddFilterItem(strFilter, "Access (*.mdb)", "*.MDB;*.MDE;*.MDA")
    'strFilter = AddFilterItem(strFilter, "Text (*.txt)", "*.csv;*.dat")
    'strFilter = AddFilterItem(strFilter, "Access (*.xls)", "*.xls;*.xlc")
    strFilter = AddFilterItem(strFilter, "Alle Dateien (*.*)", "*.*")
    If IsMissing(tabName) Then
    strInputFileName = CommonFileOpenSave(InitialDir:=DBName, Filter:=strFilter, OpenFile:=True, DialogTitle:="Datenbank öffnen ...", _
    Flags:=OFN_HIDEREADONLY)
    Else
    strInputFileName = CommonFileOpenSave(InitialDir:=DBName, Filter:=strFilter, OpenFile:=True, filename:=tabName, DialogTitle:="Datenbank öffnen ...", _
    Flags:=OFN_HIDEREADONLY)
    End If

    DBSuchen = strInputFileName
    End Function
    'Note that in order to call the Save As dialog box, you can use the same wrapper function by just setting the OpenFile option as False. For example,


    Function AddFilterItem(strFilter As String, strDescription As String, Optional varItem As Variant) As String
    ' Tack a new chunk onto the file filter.
    ' That is, take the old value, stick onto it the description,
    ' (like "Databases"), a null character, the skeleton
    ' (like "*.mdb;*.mda") and a final null character.

    If IsMissing(varItem) Then varItem = "*.*"
    AddFilterItem = strFilter & strDescription & vbNullChar & varItem & vbNullChar
    End Function



    Private Function TrimNull(ByVal strItem As String) As String
    Dim intPos As Integer
    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
    TrimNull = Left(strItem, intPos - 1)
    Else
    TrimNull = strItem
    End If
    End Function



    Then make a button and in its
    OnClick event put the next statement
    DBSuchen(CurDir & "\*.mdb")

    That's all
    Have nice day

  3. #3
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    And in order to show the file name in a textBox then


    Me.yourTextBox=DBSuchen(CurDir & "\*.mdb")

  4. #4
    Join Date
    Jan 2004
    Location
    South Africa
    Posts
    178
    Originally posted by hammbakka
    And in order to show the file name in a textBox then


    Me.yourTextBox=DBSuchen(CurDir & "\*.mdb")
    thats excellent Danke
    I don't want to return the file name though, just the path to the directory where the file is kept.

  5. #5
    Join Date
    Jan 2004
    Location
    South Africa
    Posts
    178
    Originally posted by actionant
    thats excellent Danke
    I don't want to return the file name though, just the path to the directory where the file is kept.
    or

    How do I open the file when I click on the textbox

  6. #6
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    Originally posted by actionant
    thats excellent Danke
    I don't want to return the file name though, just the path to the directory where the file is kept.
    To return the PATH of the file name make a function that
    loops the file name searching for the "\" Backslash until there is not any "\" more then using the
    Mid function you will be able to get the path and also the file name only without path


    Good luck

  7. #7
    Join Date
    Jan 2004
    Location
    South Africa
    Posts
    178
    Originally posted by hammbakka
    To return the PATH of the file name make a function that
    loops the file name searching for the "\" Backslash until there is not any "\" more then using the
    Mid function you will be able to get the path and also the file name only without path


    Good luck
    how do I return the index of the string where it finds a "\" searching from the right

  8. #8
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    There is no functoin to search from right but
    you can make a loop searching from left till there is no "\" any more using the Instr function

  9. #9
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    Good after noon:

    Here is a better solution using APIs, again copy and past



    Option Compare Database
    Option Explicit

    Type tagOPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type

    Private Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
    End Type

    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
    "SHGetPathFromIDListA" (ByVal pidl As Long, _
    ByVal pszPath As String) As Long

    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
    "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
    As Long

    Private Const BIF_RETURNONLYFSDIRS = &H1

    Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
    Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
    Global Const ahtOFN_READONLY = &H1
    Global Const ahtOFN_OVERWRITEPROMPT = &H2
    Global Const ahtOFN_HIDEREADONLY = &H4
    Global Const ahtOFN_NOCHANGEDIR = &H8
    Global Const ahtOFN_SHOWHELP = &H10
    ' You won't use these.
    'Global Const ahtOFN_ENABLEHOOK = &H20
    'Global Const ahtOFN_ENABLETEMPLATE = &H40
    'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
    Global Const ahtOFN_NOVALIDATE = &H100
    Global Const ahtOFN_ALLOWMULTISELECT = &H200
    Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
    Global Const ahtOFN_PATHMUSTEXIST = &H800
    Global Const ahtOFN_FILEMUSTEXIST = &H1000
    Global Const ahtOFN_CREATEPROMPT = &H2000
    Global Const ahtOFN_SHAREAWARE = &H4000
    Global Const ahtOFN_NOREADONLYRETURN = &H8000
    Global Const ahtOFN_NOTESTFILECREATE = &H10000
    Global Const ahtOFN_NONETWORKBUTTON = &H20000
    Global Const ahtOFN_NOLONGNAMES = &H40000
    ' New for Windows 95
    Global Const ahtOFN_EXPLORER = &H80000
    Global Const ahtOFN_NODEREFERENCELINKS = &H100000
    Global Const ahtOFN_LONGNAMES = &H200000


    Declare Sub wlib_SplitPath Lib "msaccess.exe" Alias "#59" (ByVal lpszPath As String, ByVal lpszDrive As String, ByVal lpszDir As String, ByVal lpszFname As String, ByVal lpszExt As String)




    Public Function BrowseFolder(szDialogTitle As String) As String
    Dim X As Long, bi As BROWSEINFO, dwIList As Long
    Dim szPath As String, wPos As Integer

    With bi
    .hOwner = hWndAccessApp
    .lpszTitle = szDialogTitle
    .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)

    If X Then
    wPos = InStr(szPath, Chr(0))
    BrowseFolder = Left$(szPath, wPos - 1)
    Else
    BrowseFolder = ""
    End If
    End Function

  10. #10
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    If you have lable on your Form you can do that:

    make a button on your form and put the following as onClick event
    Me.currPath.Caption = BrowseFolder("PathFinder....")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •