Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a Browse for file window 1

Status
Not open for further replies.

MarkRobinson

Programmer
Feb 18, 2000
112
US
I've found several program listing on this site, other sites, and one on the microsoft site.
None of them seem to work, though.
I've got access 2000. When I try to load in the ActiveX control it talks about licensing the ODBC.
Several of the others that I've tried generate errors, possibly because I'm not pasting them in the right place. Even the one on the microsoft site is riddled with typos.

Can anyone help me find an easy, well documented browse for a file?
 
Hi MarkRobinson...

I've got a database where I have to OPEN a file...I'm not totally clear what you need to do exactly. Anyway, here is my code for opening a standard "Open Dialog" window that stores the file name to a variable...
****************************************

Dim FileName As String
Dim strFilter As String
Dim strInputFileName As String


strFilter = ahtAddFilterItem(strFilter, "LST Files (*.lst)", "*.lst")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.txt")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strInputFileName = ahtCommonFileOpenSave(Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Open Payroll File...", _
Flags:=ahtOFN_HIDEREADONLY)


FileName = strInputFileName
If FileName = "" Then
MsgBox ("You Must Specify a Location for the File!")
End If

**********************************

This code first declares the types of files you will be able to open, then opens the dialog box, then stores the file name to a variable. I've had problems if no value gets stored to FileName (for whatever reason), so make sure you work something out to stop the process if no filename has been selected.

This code can be put anywhere in the process of the OnClick command or whatever you need it for...the declarations should of course go in the beginning of the block of code.

Hope this helps!!

Labby
 
"Sub of Function Note Defined"
points to: ahtAddFilterItem

Doesn't this requite the ahtAddFilterItem to be a function.

What I'm trying to accomplish is to let the user click, browse through folders, double-click a file and have that filename entered into the text box (that file then gets read by a VB program)
 
I had the same problem and have since sworn off dialogue controls. Use the Windows API instead. Drop api code into a module as is and then you just call it thus:

strFileName = apiOpenFile

API Code:

[tt]


Private Declare Function apiGetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long


'END API DECLARATIONS ----------------------------------------------
'TYPE DECLARATIONS -----------------------------------------------------
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

' END TYPE DECLARATIONS --------------------------------------------------

'Constants for OpenFileDialog
Private Const cdlOFNAllowMultiselect = &H200
Private Const cdlOFNCreatePrompt = &H2000
Private Const cdlOFNExplorer = &H80000
Private Const cdlOFNExtensionDifferent = &H400
Private Const cdlOFNFileMustExist = &H1000
Private Const cdlOFNHelpButton = &H10
Private Const cdlOFNHideReadOnly = &H4
Private Const cdlOFNLongNames = &H200000
Private Const cdlOFNNoChangeDir = &H8
Private Const CdlOFNNoDereferenceLinks = &H100000
Private Const cdlOFNNoLongNames = &H40000
Private Const CdlOFNNoReadOnlyReturn = &H8000
Private Const cdlOFNNoValidate = &H100
Private Const cdlOFNOverwritePrompt = &H2
Private Const cdlOFNPathMustExist = &H800
Private Const cdlOFNReadOnly = &H1
Private Const CdlOFNShareAware = &H4000



Public Function apiOpenFile(strFilterType As String, _
Optional strDialogTitle As String = "Name of File to Open") As String

On Error GoTo Error_apiOpenFile

Dim lngReturn As Long
Dim strFilter As String
Dim intLocNull As Integer
Dim strTemp As String
Dim ofnFileInfo As OPENFILENAME
Dim strInitialDir As String

Select Case strFilterType

Case "Word"
strFilter = "Word Docs (*.doc)" & Chr(0) & "*.doc" & Chr(0)
Case "Excel"
strFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls" & Chr(0)
Case "Access"
strFilter = "Access Files (*.mdb)" & Chr(0) & "*.mdb" & Chr(0)
Case Else
strFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
End Select

strInitialDir = CurrentProject.Path
'strFileName = String(256, 0)

With ofnFileInfo
.lStructSize = Len(ofnFileInfo)
.lpstrFile = String(256, 0)
.lpstrFileTitle = String(256, 0)
.lpstrInitialDir = strInitialDir
.hwndOwner = Application.hWndAccessApp
.lpstrFilter = strFilter
.nFilterIndex = 1
.nMaxFile = 256 'Len(strFileName)
.nMaxFileTitle = ofnFileInfo.nMaxFile
.lpstrTitle = strDialogTitle
.flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or _
cdlOFNNoChangeDir
.hInstance = 0
.lpstrCustomFilter = String(255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With

lngReturn = apiGetOpenFileName(ofnFileInfo)

If lngReturn = 0 Then
strTemp = ""
Else
'-- Trim off any null terminator
strTemp = Trim(ofnFileInfo.lpstrFile)
intLocNull = InStr(strTemp, Chr(0))
If intLocNull Then
strTemp = Left(strTemp, intLocNull - 1)
End If
End If

apiOpenFile = strTemp

Error_apiOpenFile:
Exit Function

End Function



[/tt]


Turn your headache into my project!
Jeffrey R. Roberts
Insight Data Consulting
Access and SQL Server Development
 
Sorry Mark! I didn't even realize that there is a whole module that is necessary for my version of the dialog box to work...It works off of API code. If you'd like I can paste the code here, but it's pretty long, and Quehay's method appears a bit shorter. I guess I would try his first.

Let me know.
Labby
 
IT WORKS!!
Great!

I had to call it with:

strFileName = apiOpenFile("*.*")

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top