PLay Audio files in ACCESS (1 Viewer)

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
I have watched several videos, and done some reading, regarding being able to play audio recordings from ACCESS. If I am in the the Windows Explorer I can just dbl click a file and it plays using Groove Music. I have also found that you can select what player to use when you playback a sound file by looking at the properties of that specific file. I have a screen that shows the records in a table, and one of the fields is recording name. I would like to be able to dbl click that entry in a record, much like it works in the Explorer, and have the audio play. Is there a way to do that. I understand that the recordings are in a a folder separate from ACCESS, but I know the path to the folder. I haven't tried anything yet, just looking to get pointed inthe right direction.
 

Ranman256

Well-known member
Local time
Today, 04:02
Joined
Apr 9, 2015
Messages
4,337
Paste this code into a module, then its usage is: OpenNativeApp txtBox

if the path in the text box is a audio file, it will open in the audio player
if the item in the text box is a URL, it will open in default explorer
etc...

OpenNativeApp "c:\folder\file.doc"

'whatever path is in the textbox will open in its native application


Code:
#If Win64 Then      'Public Dclare PtrSafe Function
  Private 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
  Private 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
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
You should use code to open the file asynchronously so that you can also pause or stop it before it is finished
Have a look at the code in my example app
 

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
Paste this code into a module, then its usage is: OpenNativeApp txtBox

if the path in the text box is a audio file, it will open in the audio player
if the item in the text box is a URL, it will open in default explorer
etc...

OpenNativeApp "c:\folder\file.doc"

'whatever path is in the textbox will open in its native application


Code:
#If Win64 Then      'Public Dclare PtrSafe Function
  Private 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
  Private 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
Thanks a lot
Paste this code into a module, then its usage is: OpenNativeApp txtBox

if the path in the text box is a audio file, it will open in the audio player
if the item in the text box is a URL, it will open in default explorer
etc...

OpenNativeApp "c:\folder\file.doc"

'whatever path is in the textbox will open in its native application


Code:
#If Win64 Then      'Public Dclare PtrSafe Function
  Private 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
  Private 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
Tried the code. The path is not in the txt box, so i added this in the call to add the path to the value in the textbox:
Code:
OpenNativeApp "c:\Users\Meee\OneDrive\Documents\Sound recordings\&Me.recordingname"
Code:
OpenNativeApp "c:\Users\jplor\OneDrive\Documents\Sound recordings\" & "Forms!RoboMasterEntry.recordingname"
Also tried:
Code:
OpenNativeApp "c:\Users\jplor\OneDrive\Documents\Sound recordings\" & "Me.recordingname"

Me.recordingname is the name of the textbox.
all of which are wrong, since the recording didn't play. I probably didn't get the concatenation right.
It compiles OK, no errors, just silence when there should be noise
BTW, thought hat is was cool that you wrote it for both 32 and 64 bit machines.

Thanks
 
Last edited:

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
You should use code to open the file asynchronously so that you can also pause or stop it before it is finished
Have a look at the code in my example app
If the code opens a sound player, which is says it does, then I will be able to pause using that player.
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
The conditional compilation for 64-bit in post #2 is incorrect and won't work as written.
 

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
The conditional compilation for 64-bit in post #2 is incorrect and won't work as written.
Unfortunately, I am on a 64 bit machine with 64 bit Office. When I loaded he module I noticeg that the 32bit code was red, but there was not a compile error. WIll it run on 32 bit office on a 64 bit machine, or do you have a correction for the 64 bit code?
 

Ranman256

Well-known member
Local time
Today, 04:02
Joined
Apr 9, 2015
Messages
4,337
yes, it has PTRSAFE to run on both OS types.
The red is for you to notice.
 

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
yes, it has PTRSAFE to run on both OS types.
The red is for you to notice.
It doesn't play the file. Is this right? It's in the dblclick event of a textbox that only has the recording name in it. So I have to add the path to the OpenNativeApp call.

Code:
OpenNativeApp "c:\Users\jplor\OneDrive\Documents\Sound recordings\" & "Forms!RoboMasterEntry.recordingname" & ".m4a"

Where Me.Recording names is the name of the textbox.
Appreciate the quick responses.
Note, still learning ACCESS
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
yes, it has PTRSAFE to run on both OS types.
The red is for you to notice.

I have pointed out the errors in this code at least once before
It isn't enough to write PtrSafe.
You also have to alter any pointers/handles such as hWnd from Long to LongPtr & check the output of each API
Furthermore, it is better to use #If VBA7 than #If Win64

The red indicates code that isn't valid for the particular setup you are using

Change the APIs to:

Code:
#If VBA7 Then      'for use in A210 or later (32-bit or 64-bit)
  Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, _
        ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, _
        ByVal FsShowCmd As Long) As LongPtr
  Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
#Else 'A2007 or earlier
  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

Also amend the StartDoc function to;

Code:
#If VBA7 Then
    Private Function StartDoc(psDocName As String) As LongPtr

    Dim Scr_hDC As LongPtr
#Else
    Private Function StartDoc(psDocName As String) As Long

    Dim Scr_hDC As Long
#End If

Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function

Tested & working in both 32-bit & 64-bit

OR you can play the files within Access using code like the example I suggested in post #3
 

Attachments

  • PlaySoundTest.zip
    136.1 KB · Views: 93

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
I have pointed out the errors in this code at least once before
It isn't enough to write PtrSafe.
You also have to alter any pointers/handles such as hWnd from Long to LongPtr & check the output of each API
Furthermore, it is better to use #If VBA7 than #If Win64

The red indicates code that isn't valid for the particular setup you are using

Change the APIs to:

Code:
#If VBA7 Then      'for use in A210 or later (32-bit or 64-bit)
  Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, _
        ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, _
        ByVal FsShowCmd As Long) As LongPtr
  Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
#Else 'A2007 or earlier
  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

Also amend the StartDoc function to;

Code:
#If VBA7 Then
    Private Function StartDoc(psDocName As String) As LongPtr

    Dim Scr_hDC As LongPtr
#Else
    Private Function StartDoc(psDocName As String) As Long

    Dim Scr_hDC As Long
#End If

Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function

Tested & working in both 32-bit & 64-bit

OR you can play the files within Access using code like the example I suggested in post #3
Ranman, I imported the VB7 module that you so kindly sent. (Be advised that I am not an expert in VBA, written a lot of code, but mostly in linear languages, 360/370 assembler, Fortran etc). Here is what I did with the new mod:
Imported it into my application after deleting the old mod.
IN the textbox dbleclick event I put:
Code:
OpenNativeApp "c:\Users\me\OneDrive\Documents\Sound recordings\" & Me.recordingname
The recording name is Utility.a4m
When I dblclk the textbox and cycle through the code(F8), then when I execute the OpenNativeApp, I get a compile error "Type Mismatch" on this line"
Code:
r = StartDoc(psDocName)
And the StartDoc is highlighted.

The only thing in the sample you sent was the Mod. There was not a form w/ textbox. Was I supposed to add that? Other than experience, I must be missing something,

Thanks for your help , it greatly appreciated .
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
@jpl458
I'm not sure who your reply was addressed to. You quoted my post and then began with Ranman....
Assuming you meant me, the zip file also contained a sample .wav file.
Save the wav file in the same folder and run the test procedure in the module to play it.

Code:
Sub Test()
    StartDoc CurrentProject.Path & "\Big Ben.wav"
End Sub

Check that works for you then substitute your own full path for your own file.

Do remember this isn't my code. I've just altered the code in post #2 so it will work in 64-bit.
However, I have my own code which I use instead for this purpose
 

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
Sorry for trying to give you someone else's name. Will try the solution in a bit. Family is getting in the way of becoming a TGIT. (Technecal Giant in Training)
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
Sorry for trying to give you someone else's name. Will try the solution in a bit. Family is getting in the way of becoming a TGIT. (Technecal Giant in Training)
LOL. I'll remember that acronym!
 

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
In following code:
Code:
Sub Test()

    StartDoc CurrentProject.Path & "\Big Ben.wav"
    
End Sub
It works if I use Run, but does not if I put that code behind a button or textbox.

What escapes me is setting the path to my folder where my recordings are stored.
That path is c:\Users\me\OneDrive\Documents\Sound recordings\" & Utility.a4m

I've tried this:
Code:
StartDoc CurrentProject "c:\Users\jplor\OneDrive\Documents\Sound recordings\" & Utility.a4m
as well as several other iterations, none of which works. Utility.a4m is a recording in my folder of recordings

In my app the name of the recording is in a textbox named recordingname.
In the dblclek event of that textbox I have put:

Code:
OpenNativeApp "c:\Users\me\OneDrive\Documents\Sound recordings\" & Me.recordingname & ".a4m"
I had to add the extension of the file, because it is not stored in the textbox with the name

In your example you don't use OpenNativeApp. Seems that's what should be behind any control, is that correct.

My apologies for asking so many redementary questions, and really appreciate your help
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
The file name is a string so it needs to be in quotes:

Code:
StartDoc "c:\Users\jplor\OneDrive\Documents\Sound recordings\" & "Utility.a4m"

or just

Code:
StartDoc "c:\Users\jplor\OneDrive\Documents\Sound recordings\Utility.a4m"
 
Last edited:

jpl458

Well-known member
Local time
Today, 01:02
Joined
Mar 30, 2012
Messages
1,038
Using your example, if I copy and past either of the code samples you sent, I get a compile error on my path. For example:

1673735296351.png


1673735329096.png
 

isladogs

MVP / VIP
Local time
Today, 09:02
Joined
Jan 14, 2017
Messages
18,221
Sorry - wasn't thinking when I wrote my last reply & just copied what you had written
I've now corrected my last post

CurrentProject.Path is code to return the current folder which isn't relevant in your case

BTW are you sure that .a4m is the correct file type? I use .m4a files but have never heard of .a4m
 

Users who are viewing this thread

Top Bottom