Click to See Complete Forum and Search --> : Browse files and select in Access 97
mtrack81
August 1st, 2001, 08:48 AM
In my module, rather than asking the user to type in the file
path and directory, I want the user to be able to browse their computer and be
able to select the file that they want. Similar to in most programs where when
you click on "open" one can browse through their entire computer with the
treeview system. For example, say a file is located in C:\My Documents\abc.txt,
When I click on a button, i should be able to navigate through C:\, select My
documents, which would open up that folder and display all of its contents, and
then finally select the file abc.txt which is the file that I ultimately
wanted. Can anyone help me out with this?..I hope I was clear enough
johnpc7
August 1st, 2001, 09:37 AM
You need to study/research the MS Common Dialog
component, the following shows how to open MS Access Databases in the C:\My Documents directory.
You need a form with a command button, cmdOpenDB,
and a Common Dialog Control, dlgCommon, you will have to manually load the control from the components tool bar in the VB IDE.
private Sub cmdOpenDB_Click()
Dim strCheckForDatabase as string
on error GoTo HandleErrors
dlgCommon.DialogTitle = "Pick A Database"
'Give the file selection window a title.
dlgCommon.InitDir = "C:\My Documents" 'App. Path
'The file selection window will start in the
'applications directory.
'Allow the user to view only Access files.
dlgCommon.Filter = "Access Databases (*.mdb)|*.mdb|" & _
"All Files (*.*)|*.*"
dlgCommon.ShowOpen
'Open the file selection window.
strCheckForDatabase = Right(dlgCommon.FileName, 4)
'Select the last four letters of the file selected.
Select Case strCheckForDatabase
Case vbNullString
'Do not allow empty strings.
Exit Sub
Case ".mdb"
'Assign the chosen file to the path string.
mstrDatabasePath = dlgCommon.FileName
'Do not allow the user to select another DB until
'clear is clicked.
cmdOpenDB.Enabled = false
End Select
johnpc7
August 1st, 2001, 09:37 AM
You need to study/research the MS Common Dialog
component, the following shows how to open MS Access Databases in the C:\My Documents directory.
You need a form with a command button, cmdOpenDB,
and a Common Dialog Control, dlgCommon, you will have to manually load the control from the components tool bar in the VB IDE.
private Sub cmdOpenDB_Click()
Dim strCheckForDatabase as string
on error GoTo HandleErrors
dlgCommon.DialogTitle = "Pick A Database"
'Give the file selection window a title.
dlgCommon.InitDir = "C:\My Documents" 'App. Path
'The file selection window will start in the
'applications directory.
'Allow the user to view only Access files.
dlgCommon.Filter = "Access Databases (*.mdb)|*.mdb|" & _
"All Files (*.*)|*.*"
dlgCommon.ShowOpen
'Open the file selection window.
strCheckForDatabase = Right(dlgCommon.FileName, 4)
'Select the last four letters of the file selected.
Select Case strCheckForDatabase
Case vbNullString
'Do not allow empty strings.
Exit Sub
Case ".mdb"
'Assign the chosen file to the path string.
mstrDatabasePath = dlgCommon.FileName
'Do not allow the user to select another DB until
'clear is clicked.
cmdOpenDB.Enabled = false
End Select
mtrack81
August 1st, 2001, 03:57 PM
FOR FUTURE REFERENCE:
if anyone needs code for this type of problem go to this site:
http://www.calvinsmithsoftware.com/SelectDiskFile.htm
It saved my life...well not really but it's exactly what i was looking for.
GeorgeT
August 1st, 2001, 04:04 PM
This is a nice one
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Sub GetFolderPath()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
'set the owner window
'.hWndOwner = Me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("Default folder: " & GetSetting("TAC", "User Info", "Library Location"), "")
'Return only if the user selected a directory
.ulFlags = BIF_RETURNONLYFSDIRS
End With
'Show the 'Browse for folder' dialog
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
'free the block of memory
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
MsgBox sPath
End Sub
elevit
October 3rd, 2002, 10:38 AM
In the code (please, see previuos message) I would like to specify/code some action in case user presses a "Cancel" button on the 'Browse for folder' dialog. How (where in the code) can I do that?
Thanks a lot!
Eugene.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.