Opening an attachment from a different table using a button on a form (1 Viewer)

jjake

Registered User.
Local time
Today, 07:41
Joined
Oct 8, 2015
Messages
291
Hello,

I have 2 Tables.

tblOrder
tblAttachment


In tblAttachment i have 5 Fields.

ID
AttFile - Attachment field
AttFilePath - A Short text field which stores the path on my C Drive
AttType - The type of attachment .PDF .JPEG etc.
AttName - The name of the Raw file as saved on the C Drive

I have a folder on my C Drive that i store all my attachments then i manually input all the data into the table for that attachment.

I have a form based on tblOrder that has a button (cmdAtt) that i would like to open a specific attachment saved in tblAttachment when i click once on it.

How do i enable the button to run a specific path or open a specific attachment (FreightMatrix.PDF)? The tables are on the back end if that means anything.

Thanks.
 

Ranman256

Well-known member
Local time
Today, 08:41
Joined
Apr 9, 2015
Messages
4,337
Paste this code into a module, and it will open ANY file in its native application.
usage: OpenNativeApp "c:\folder\file.pdf"
will open it in acrobat
and
OpenNativeApp me.txtBox
will open the doc in Word if the item in txtBox is C:\myfile.doc

Code:
'Attribute VB_Name = "modNativeApp"
'Option Compare Database
Option Explicit

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
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
 

jjake

Registered User.
Local time
Today, 07:41
Joined
Oct 8, 2015
Messages
291
Ranman,

Thank you for your reply. How would I use this code to open a certain object. I don't see where I would put in my file path. (I'm a novice at VBA)
 

jjake

Registered User.
Local time
Today, 07:41
Joined
Oct 8, 2015
Messages
291
So after some digging this is what I ended up using to fix my problem. It wasn't what I wanted but it got the job done.

Code:
Private Sub NameOfButtonGoesHere_Click()
    Dim AttFilePath as String
    
    AttFilePath = "Your File Path Goes Here"

    Application.FollowHyperlink AttFilePath
End Sub
 

Users who are viewing this thread

Top Bottom