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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display file path in a textbox.....almost there...!! 1

Status
Not open for further replies.

chribi

Programmer
Joined
May 8, 2003
Messages
6
Location
CH
Hi all!
On a form i have a button that opens the "windows open file dialog box" and after selecting a file path I want to store that filepath in a textbox on that form.
Opening the "windows open file dialog box" with below code works (i found it here in one of the forums).
But getting the filepath to display in a textbox and storing it is what I'm stuck with.
I've used an expression in a command button to run the function ap_fileopen.

Can anyone help please?
THanks much
Chris

**************CODE begin for openfile dialog box*********
Option Compare Database
Option Explicit

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

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

Public Function ap_FileOpen(Optional strTitle As String = "Open File", _
Optional strFileName As String = "", _
Optional strFilter As String = "") As String

Dim OpenFile As OPENFILENAME
Dim lngReturn As Long

If Len(strFileName) = 0 Then
strFileName = String(255, 0)
End If

If Len(strFilter) = 0 Then
strFilter = "Image Files (*.*)" & Chr(0) & _
"*.*" & Chr(0)
End If

OpenFile.lStructSize = Len(OpenFile)

OpenFile.lpstrFilter = strFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = strFileName + _
Space(255 - Len(strFileName))
OpenFile.nMaxFile = 255
OpenFile.lpstrFileTitle = strFileName
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = CurrentProject.Path
OpenFile.lpstrTitle = strTitle
OpenFile.flags = 0

ap_GetOpenFileName OpenFile

ap_FileOpen = Left(OpenFile.lpstrFile, _
InStr(OpenFile.lpstrFile, Chr$(0)) - 1)

End Function
***************Code end***********************************
 
try setting lpstrInitialDir before you invoke the dialog

You can also change the dialog title by setting lpstrTitle.

It probably wouldn't be a bad idea to search the web for more information on windows API functions, common file dialog.

There's also a FAQ in here:
 
Please disregard my response, I totally misread your question.
 
Daniel, thx much for reply. Actually i found a code which works better. Now u might laugh at me but i cant get the filename displayed. Here is what i have:

Have a command button (cmdOpFileDbox) with click event:

Private Sub cmdOpFileDbox_Click()
Dim RetVal As Long
Dim FileName As String
Dim Path As String
RetVal = FileDialog(hwnd, FileName, "All Files(*.*)" + String(1, 0) + "*.*" + String(1, 0), "", "*")
If RetVal > 0 Then txtCtrl = Left(FileName, InStr(FileName, Chr(0)) - 1)
End Sub

This calls the function FileDialog and the Box pops up as it should be. Now i tried every way to capture the selected file path in a textbox. No luck. Can u be a bit more detailed in how i should proceed since i'm quite new to VB.

Thanks in advance
Chris
 
LOL
It works better but you can't display what you need...seems to me like it's not working at all.

OK, let's launch the code dissector...

Dim RetVal As Long declares a Long Integer
Dim FileName As String declares a string variable, but this variable takes no value in your code-it is just ""
Dim Path As String same comment
RetVal = FileDialog(hwnd, FileName, "All Files(*.*)" + String(1, 0) + "*.*" + String(1, 0), "", "*") tells me that your FileDialog function returns a Long Integer (don't ask me why)

And finally:
txtCtrl = Left(FileName, InStr(FileName, Chr(0)) - 1)
Remember? FileName is "". What did you expect to see in the box???

For the Open/Save dialog, I always use the code from:

Follow the instructions on the site, then change your code to:

Private Sub cmdOpFileDbox_Click()
Dim FileName As String
FileName = GetOpenFile(CurDir, "Select File to open")
If Nz(FileName,&quot;&quot;)<>&quot;&quot; Then txtCtrl = FileName
End Sub

Good luck


[pipe]
Daniel Vlas
Systems Consultant

 
Hey Daniel

got it working, great. here is ur star for the web page and helping me with this. the web page u gave me really has got some cool stuff. even managed to open the file, which is displayed in the text box, with the shellexecute code from that page.

here's the code:
********************
Private Declare Function apiShellExecute Lib &quot;shell32.dll&quot; _
Alias &quot;ShellExecuteA&quot; _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long

'***App Window Constants***
Public Const WIN_NORMAL = 1 'Open Normal
Public Const WIN_MAX = 3 'Open Maximized
Public Const WIN_MIN = 2 'Open Minimized

'***Error Codes***
Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&


Function fHandleFile(stFile As String, lShowHow As Long)
Dim lRet As Long, varTaskID As Variant
Dim stRet As String
'First try ShellExecute
lRet = apiShellExecute(hWndAccessApp, vbNullString, _
stFile, vbNullString, vbNullString, lShowHow)

If lRet > ERROR_SUCCESS Then
stRet = vbNullString
lRet = -1
Else
Select Case lRet
Case ERROR_NO_ASSOC:
'Try the OpenWith dialog
varTaskID = Shell(&quot;rundll32.exe shell32.dll,OpenAs_RunDLL &quot; _
& stFile, WIN_NORMAL)
lRet = (varTaskID <> 0)
Case ERROR_OUT_OF_MEM:
stRet = &quot;Error: Out of Memory/Resources. Couldn't Execute!&quot;
Case ERROR_FILE_NOT_FOUND:
stRet = &quot;Error: File not found. Couldn't Execute!&quot;
Case ERROR_PATH_NOT_FOUND:
stRet = &quot;Error: Path not found. Couldn't Execute!&quot;
Case ERROR_BAD_FORMAT:
stRet = &quot;Error: Bad File Format. Couldn't Execute!&quot;
Case Else:
End Select
End If
fHandleFile = lRet & _
IIf(stRet = &quot;&quot;, vbNullString, &quot;, &quot; & stRet)
End Function
************************

In the DoubleClick of a button i put:
Print fHandleFile(Me.txtCtrl, 1) where Me.txtCtrl is the textbox with path as its value.
The app with the file is opening but i get a runtime error '438': object not supporting property or method.

Any thoughts on this one ?

Thanks again
Chris
 
Hey Rick, thx but no luck.
 
On second thought, it your code was not behind the form, the function would not recognize &quot;Me!&quot;.......you might have to make an explicit ref to the form
 
Hey Rick, tried that one too, no luck. Thx again.
 
I think the problem is with the &quot;Print&quot; bit!
In Access there is no Print Method, you need to assign it to the debug object so you get:

Debug.Print fHandleFile(Me.txtCtrl, 1)


which will run the file specified in me.txtCtrl and then print to the debug screen a code that says whether or not it was completed successfully.

hth

Ben

----------------------------------------------
Ben O'Hara

&quot;Where are all the stupid people from...
...And how'd they get so dumb?&quot;
NoFX-The Decline
----------------------------------------------
 
Chris, first of all I think it's kinda rude posting the entire code without even mentioning who wrote it. Especially when:

'You are free to use it in any application,
' provided the copyright notice is left unchanged.


For those who don't know it yet, the code was written by Dev Ashish and the link is:


I'm a little touchy about this subject...

I have used code from this site in many applications, but I never ever dared to remove the copyright notice. I just quoted what my changes were...

Now, about your problem:

You don't need that code to launch a registered file:
FollowHyperlink Me.txtCtrl
will do...

What is Print in your code supposed to do???

Sorry for the lecture



[pipe]
Daniel Vlas
Systems Consultant

 
Hey Daniel
Sorry about being rude, was certainly not my intension. Think was so happy having found this site that I didnt really think about copyright issues when posting it here. Will definately be considerate of this when posting/using code in fut.
Thanks anyway for ur help.
CHris
 
I used the code on for some project and it worked fine indeed.

I used it with the following code:

Code:
Private Sub btnOpenFile_Click()

Dim Current_Value

Current_Value = Me.Derived_Modulename.Value

Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, &quot;PDF Files (*.PDF)&quot;, &quot;*.PDF&quot;)
strInputFileName = ahtCommonFileOpenSave(InitialDir:=&quot;M:\modules\actual\PDF&quot;, _
                Filter:=strFilter, OpenFile:=True, _
                DialogTitle:=&quot;Please select a Module...&quot;, _
                Flags:=ahtOFN_HIDEREADONLY)
                
If strInputFileName = &quot;&quot; Then
    Me.Derived_Modulename.Value = Current_Value
Else
  Me.Derived_Modulename.Value = strInputFileName
End If

Me.Refresh

End Sub

the only thing is..i would like to split the strInputFileName in its components - Directory and Filename.

Anyone got a suggestion?



---
It's never too late to do the Right thing
 
Paste the below function in a module
Code:
'==============================
Function fPathOrFile(FullPath As String, Optional JustDir = True) As String
Dim i As Integer

For i = Len(FullPath) To 1 Step -1
If Mid(FullPath, i, 1) = &quot;\&quot; Then
  If JustDir Then
  fPathOrFile = Left(FullPath, i)
  Else
  fPathOrFile = Right(FullPath, Len(FullPath) - i)
  End If
Exit For
End If
Next

End Function
'==================================

Then use the function to split the string in components:

strInputDir = fPathOrFile(strInputFileName)
-this will output also the trailing backslash

strInputFileName=fPathOrFile(strInputFileName, False)

Good luck


[pipe]
Daniel Vlas
Systems Consultant

 
Thank you Daniel for your quick reply. I did some searching myself and i found the following code:


I modified the previous code to

Code:
Me.Derived_Modulename.Value = ParseFileNameFromPath(strInputFileName)

I needed the full path to the file to be able to open it with the following code:

Code:
Private Sub btnOpenPDF_Click()
On Error GoTo Err_btnOpenPDF_Click

Me.btnOpenPDF.Hyperlink.Address = Me.File.Value
Me.btnOpenPDF.Hyperlink.Follow
    

Exit_btnOpenPDF_Click:
    Exit Sub

Err_btnOpenPDF_Click:
    Resume Exit_btnOpenPDF_Click
End Sub

but i wanted the user to see only the filename.

User is happy now, thanks for your help.

ParseFileNameFromPath function follows below:

Code:
Function ParseFileNameFromPath(strPathAndFileName As String) As String

' ---------------------------------------------------------------------------------
' Author: Calvin Smith - CodeDiskHelp@Yahoo.com
' Environment(s): MS Access (32-bit) / Visual Basic (32-bit)
' ---------------------------------------------------------------------------------
'
'   *****************************************
'   * Courtesy code from my CodeDisk© product
'   *****************************************
   '
   ' ----------------------------------------------------------------------------
   ' Purpose: Example of how to parse a filename from a full path
   '
   ' Accepts: strPathAndFileName as the fully qualified path and filename
   '
   ' Returns: The parsed filename
   '
   ' Example: strRetVal = ParseFileNameFromPath(&quot;C:\CodeDisk\CodeDisk.exe&quot;)
   ' ----------------------------------------------------------------------------
   '
On Error GoTo ErrorHandling_Err

          Dim iFoundWhere As Integer

          Dim iReStartHere As Integer

          ParseFileNameFromPath$ = strPathAndFileName$

          iFoundWhere% = InStr(strPathAndFileName$, &quot;\&quot;)

          Do While iFoundWhere%

                    iReStartHere% = iFoundWhere%
                    iFoundWhere% = InStr(iReStartHere% + 1, strPathAndFileName$, &quot;\&quot;)

          Loop

          If iReStartHere% > 0 Then ParseFileNameFromPath$ = _
                    Mid$(strPathAndFileName$, iReStartHere% + 1)

ErrorHandling_Exit:
   Exit Function

ErrorHandling_Err:

   If Err Then
       MsgBox Err.Description
       Resume ErrorHandling_Exit
   End If

End Function
[code]


---
It's never too late to do the Right thing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top