Results 1 to 5 of 5
  1. #1
    westfallbp is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2021
    Posts
    26

    hyperlink from a list box which can be from url or file location

    I have watched a few youtube videos on both hyperlink property and using short text and writing vba code.



    I have a list box and the file location is bound to column 3 and in the table the field is listed as a hyperlink.

    On the double click property of the listbox I have it following the hyperlink.

    It works for files on my local hard drive C:\\ which is an mp3 which I get the message if I want to proceed or not warning and some URL's do work but not all of them. I get the error msg. runtime error 7971

    I was thinking can I change the field I am using for both URL and File Location to a text field. Then write code on the dbl click event property of the list box to either go to the URL or my local drive on my pc.? I am not the greatest and codeing but I am thinking of an else or if statement to accomidate if its a url htp run this code and if its on my local drive C:\\ run another code to accomidate both. Any help would be greatly appreciated.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    make a new module to put this code: modNativeapp

    this will open the 'link' in it's native application:
    .docx will open in word
    hyperlinks open in default browser
    etc...

    put the link in its own field, the list box can show 2 fields
    caption, hyperlink

    when the user dbl-clicks the list or clicks a button to open , use the column 2 link ... note: in vb columns start with zero so col 2= column(1)

    usage:
    Code:
    sub lstBox_DblClick()
        OpenNativeApp lstBox.column(1)
    end sub

    modNativeApp code:

    Code:
    Option Compare Database
    Option Explicit
    
    
    #If Win64 Then
      'Declare PtrSafe Sub...
        Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
        Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
    #Else
       Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
       Private Declare Function GetDesktopWindow Lib "user32" () As Long
    #End If
    
    
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&
    
    
    Public Sub OpenNativeApp(ByVal psDocName As String)
    Dim r As Long, Msg As String
    
    
    r = StartDoc(psDocName)
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                Msg = "File not found"
            Case SE_ERR_PNF
                Msg = "Path not found"
            Case SE_ERR_ACCESSDENIED
                Msg = "Access denied"
            Case SE_ERR_OOM
                Msg = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                Msg = "DLL not found"
            Case SE_ERR_SHARE
                Msg = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                Msg = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                Msg = "DDE Time out"
            Case SE_ERR_DDEFAIL
                Msg = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                Msg = "DDE busy"
            Case SE_ERR_NOASSOC
                Msg = "No association for file extension"
            Case ERROR_BAD_FORMAT
                Msg = "Invalid EXE file or error in EXE image"
            Case Else
                Msg = "Unknown error"
        End Select
    '    MsgBox msg
    End If
    End Sub
    
    
    Private Function StartDoc(psDocName As String) As Long
    Dim Scr_hDC As Long
    
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
    End Function

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Funny, I was going to post that code this morning but couldn't remember if I stole it from Ranman or CJ and didn't want to post it without credit.
    Guess that answers my question.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    if you already change the field to Text instead of hyperlink, you can add code to the listbox DblClick event:

    Code:
    Private YourListbox_DblClick(Cancel As Integer)
    FollowHyperLink YourListBox
    End Sub

  5. #5
    westfallbp is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2021
    Posts
    26
    Thanks Raman, it Worked great! Going to use this code for any database that has hyperlink.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 02-19-2021, 09:12 AM
  2. Replies: 11
    Last Post: 02-25-2019, 02:09 PM
  3. Replies: 4
    Last Post: 07-21-2015, 11:17 AM
  4. Display URL hyperlink images in form
    By cwillwright in forum Forms
    Replies: 1
    Last Post: 10-11-2012, 08:55 PM
  5. Open up url page hyperlink not working!
    By riffraff_ashraf in forum Forms
    Replies: 1
    Last Post: 09-29-2010, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums