macro to open a specific excel worksheet corresponding to a number in a textbox (1 Viewer)

awade

Registered User.
Local time
Today, 10:21
Joined
Apr 21, 2013
Messages
101
Good Evening all,

I have a form which as a textbox named "Core Serial Number"

I have a folder that contains two excel spreadsheets, with the folder name, and the excel spreadsheet name that same as the core serial number displayed in the text box.

e.g.
Core Serial Number Displayed in textbox on form = CAE540151
Folder located on A:\ Drive = CAE540151
Excel worksheet located in folder = CAE540151.

The form is linked to an master excel spreadsheet, which is updated daily, and any data that is changed in the master excel spreadsheet is replicated on the form.

What I would like to do is create a command button on the form that will allow me to open the excel worksheet located in the folder on A:\ drive that corresponds to the core serial number displayed in the textbox.

Is this possible? I am using Access 2013
 

Ranman256

Well-known member
Local time
Yesterday, 20:21
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.xls" , will open it in excel
and
OpenNativeApp "c:\folder\file.doc" , will open the doc in Word, etc.

You probably have a text box for the serial, txtSerial
make an 'invisible' txt box that merges the folder and the file
txtBoxFile.controlsource = "a:\" & txtSerial
so you would then use: OpenNativeApp txtBoxFile

this would open the file in excel.

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
 

awade

Registered User.
Local time
Today, 10:21
Joined
Apr 21, 2013
Messages
101
@Ranman256. Thanks for the response.

Im really new to access so what you have given me has totally confused me. I have made a test form and placed the OpenNativeApp "A:\eDNA\eDNA Database\.xls" against the command button to open the folder, but I get error box "sub or function not defined" I have placed the "a:\" & txtSerial as the control source against the text box and keep getting the same error message.

Please help.
 

Users who are viewing this thread

Top Bottom