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.
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
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
This countdown timer is similar to those found on smartphones. Use it to display remaining time (min/sec) during timed events. A choice of alarms are available to indicate when countdown has completed
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
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
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.
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
This countdown timer is similar to those found on smartphones. Use it to display remaining time (min/sec) during timed events. A choice of alarms are available to indicate when countdown has completed
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?
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.
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
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:
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,
@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
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)
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)
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