listbox to open pdf file (1 Viewer)

gippsy

Registered User.
Local time
Yesterday, 23:34
Joined
Dec 15, 2012
Messages
39
:confused:Hello everyone
I have a query that opens a listbox in a form
In the listbox I want to add a doubleclick event that opens a pdf file document with the selected record in it.
The record is in column 1 of the listbox. The path of the pdf files is
I can´t use the FollowHyperlink method of the colecction application due the record in the column is a Key. I am not sure if
FollowHyperlink method can be used in a listbox

Hello everyone
I have a query that opens a listbox in a form
In the listbox I want to add a doubleclick event that opens a pdf document with the selected record in it.
The record is in column 1 of the listbox.
I can´t use the FollowHyperlink method of the colecction application due the record in the column is a Key. I am not sure if
FollowHyperlink method can be used in a listbox

I have been checking codes in the web and I found this:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (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
ShellExecute Me.hwnd, "open", "D:\Seb\Desktop\GA-BD\pdf\test.pdf, "", "", 4"
However, I don´t know how to organize the code in a logic way in Sub End Sub. Neither I don´t know if the code is for the
doubleclic event in the list box or a Module. If is for a module I don´t know how to write the code in it
Thank you very much indeed for your help
 

MikeLeBen

Still struggling
Local time
Today, 06:34
Joined
Feb 10, 2011
Messages
187
Please refer to this:
http://www.utteraccess.com/wiki/index.php/Opening_Files_From_Access#The_ShellExecute_API

I personally like the ShellExecute API very easy and well devised. With it you would go more or less (after entering the code for the function in a separate module):

Code:
Option Compare Database
Option Explicit

Private Sub Mylistbox_DblClick(Cancel As Integer)

Dim mypath As String

mypath = DLookup("Path", "tblPahts", "[ID] = " & Me.Mylistbox)

Call fHandleFile(mypath, WIN_NORMAL)

End Sub
 

gippsy

Registered User.
Local time
Yesterday, 23:34
Joined
Dec 15, 2012
Messages
39
Mike. Many thanks
AS you said I wrote your code in the doubleclik listbox event.
I have been studying in the web some papers about the "ShellExecute API" function. However, due to my little knowlege of vba I don´t understand how to write the function in tjhe module. How the function works with the doubleclick event in the list box.
Please could you help me.
 

MikeLeBen

Still struggling
Local time
Today, 06:34
Joined
Feb 10, 2011
Messages
187
You use the VBA Editor menu to Insert -> Module.

In the newly inserted module you paste the code:
Code:
'************ Code Start **********
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" _
    (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&

'***************Usage Examples***********************
'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
'Handle Unknown extensions (call Open With Dialog):
'                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
'Start Access instance:
'                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
'****************************************************

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("rundll32.exe shell32.dll,OpenAs_RunDLL " _
                        & stFile, WIN_NORMAL)
                lRet = (varTaskID <> 0)
            Case ERROR_OUT_OF_MEM:
                stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
            Case ERROR_FILE_NOT_FOUND:
                stRet = "Error: File not found.  Couldn't Execute!"
            Case ERROR_PATH_NOT_FOUND:
                stRet = "Error: Path not found. Couldn't Execute!"
            Case ERROR_BAD_FORMAT:
                stRet = "Error:  Bad File Format. Couldn't Execute!"
            Case Else:
        End Select
    End If
    fHandleFile = lRet & _
                IIf(stRet = "", vbNullString, ", " & stRet)
End Function
'************ Code End **********

Then you use the Call fHandleFile from your double click event routine as previously indicated.
 

MikeLeBen

Still struggling
Local time
Today, 06:34
Joined
Feb 10, 2011
Messages
187
You use the VBA Editor menu to Insert -> Module.

In the newly inserted module you paste the code:
Code:
'************ Code Start **********
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" _
    (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&

'***************Usage Examples***********************
'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
'Handle Unknown extensions (call Open With Dialog):
'                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
'Start Access instance:
'                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
'****************************************************

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("rundll32.exe shell32.dll,OpenAs_RunDLL " _
                        & stFile, WIN_NORMAL)
                lRet = (varTaskID <> 0)
            Case ERROR_OUT_OF_MEM:
                stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
            Case ERROR_FILE_NOT_FOUND:
                stRet = "Error: File not found.  Couldn't Execute!"
            Case ERROR_PATH_NOT_FOUND:
                stRet = "Error: Path not found. Couldn't Execute!"
            Case ERROR_BAD_FORMAT:
                stRet = "Error:  Bad File Format. Couldn't Execute!"
            Case Else:
        End Select
    End If
    fHandleFile = lRet & _
                IIf(stRet = "", vbNullString, ", " & stRet)
End Function
'************ Code End **********

Then you use the Call fHandleFile from your double click event routine as previously indicated.
 

gippsy

Registered User.
Local time
Yesterday, 23:34
Joined
Dec 15, 2012
Messages
39
Hello
As you say I already copy the function in the module. Then I copy and paste the below code.
Option Compare Database
Option Explicit

Private Sub Mylistbox_DblClick(Cancel As Integer)
Dim mypath As String
mypath = DLookup("D:\Seb\Desktop\GAMMA-BD\pdf", "tblPaths", "[ID] = " & Me.Listbox94)
Call fHandleFile(mypath, WIN_NORMAL)
End Sub

However, When I doubleclick a line in the lisbox this message is displayed:
Error 3075 Syntax error (missing operator) in query expression D: \Seb\Desktop\GAMMA-BD\pdf
Next in "tblPaths" I wrote tblRef which is the table where data is storaged. In "[ID] I wrote RefID which is the Key field of the table
I Checked several times the path and is correct.
The same error4 message is displayed
I would really aprreciate if you tell me what I am doing wrong-
Many thanks
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 05:34
Joined
Sep 12, 2006
Messages
15,656
why can you not use a listbox?

eg this sort of thing

application.followhyperlink mylistbox.column(1)
 

gippsy

Registered User.
Local time
Yesterday, 23:34
Joined
Dec 15, 2012
Messages
39
why can you not use a listbox?

eg this sort of thing

application.followhyperlink mylistbox.column(1)

Finally the code in the listbox doubleclic event opens the pdf files.
I also included the Error Handing to my Code.
To Mike Leben and Dave Thank you very much for your help
I am happy to share the code
Private Sub Listbox1_DblClick(Cancel As Integer)
Dim db As DAO.Database 'These two lines are just an example
Dim rs As DAO.Recordset 'your code may not include them
On Error GoTo ErrorHandler
Application.FollowHyperlink "D:\Mypath\" & Listbox1
ExitHandler: 'clean up as necessary and exit
Set rs = Nothing 'These two lines are just an example
Set db = Nothing 'your code may not include them
Exit Sub
ErrorHandler:
Select Case Err 'specific Case statements for errors we can anticipate, the "Else" catches any others
Case 2501 'Action OpenReport was cancelled.
MsgBox "No data to display"
DoCmd.Hourglass False
Resume ExitHandler
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
 

Users who are viewing this thread

Top Bottom