I am in the middle of a project where I use four different folders containing all .wav files. Depending on the results of a sign on (ID) I want to use one of the four files. I am using the results of that sign in a "if..then" comparison to determine the folder I want to use. I then put that foldername in a varible and place the varible in the file PATH. However; the .wav does not play. Is there a way to use a varible in a file path?

The wave is not playing because the file is not open I suspect. Try the following which is very similar to a post here today as well -

Option Explicit
'Description: Calls the "Open File Dialog" without need for an OCX

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

'Place the following code in under a command button or in a menu, etc...
'This will open a file...
    
Private Sub Command1_Click()

Dim ofn As OPENFILENAME
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    ofn.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "Rich Text Files (*.rtf)" + Chr$(0) + "*.rtf" + Chr$(0)
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = CurDir
        ofn.lpstrTitle = "Our File Open Title"
        ofn.flags = 0
        Dim a
        a = GetOpenFileName(ofn)

        If (a) Then
                MsgBox "File to Open: " + Trim$(ofn.lpstrFile)
        Else
                MsgBox "Cancel was pressed"
        End If
End Sub

Private Sub Command2_Click()

'This will save a file

    Dim ofn As OPENFILENAME
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    ofn.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "Rich Text Files (*.rtf)" + Chr$(0) + "*.rtf" + Chr$(0)
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = CurDir
        ofn.lpstrTitle = "Our File Save Title"
        ofn.flags = 0
        Dim a
        a = GetOpenFileName(ofn)

        If (a) Then
                MsgBox "File to Save: " + Trim$(ofn.lpstrFile)
        Else
                MsgBox "Cancel was pressed"
        End If
End Sub

Private Sub Command3_Click()

'Description: Compares the content of two files
'If their sizes differ, save the newest using the above....

Open "file1" For Binary As #1
Open "file2" For Binary As #2
issame% = True
If LOF(1) <> LOF(2) Then
    issame% = False
Else
    whole& = LOF(1) \ 10000         'number of whole 10,000 byte chunks
        part& = LOF(1) Mod 10000        'remaining bytes at end of file
        buffer1$ = String$(10000, 0)
        buffer2$ = String$(10000, 0)
        start& = 1
        For x& = 1 To whole&            'this for-next loop will get 10,000
        Get #1, start&, buffer1$      'byte chunks at a time.
        Get #2, start&, buffer2$
        If buffer1$ <> buffer2$ Then
            issame% = False
                Exit For
        End If
        start& = start& + 10000
        Next
        buffer1$ = String$(part&, 0)
        buffer2$ = String$(part&, 0)
        Get #1, start&, buffer1$        'get the remaining bytes at the end
        Get #2, start&, buffer2$        'get the remaining bytes at the end
        If buffer1$ <> buffer2$ Then issame% = False
        End If
        Close
        If issame% Then
            MsgBox "Files are identical", 64, "Info"
        Else
                 MsgBox "Files are NOT identical", 16, "Info"
        End If
End Sub

Thank you for the reply. However; I don't think you fully understand my problem. I am able to access the .wav file using the PATH with the folder name. Where I have the problem is when I try to access the file using a variable in the PATH in place of the folder. My question is. Is it possible to access a .wav file using a variable in the PATH in place of a folder. In other words I would like to put the folder name in a variable and place the varible in the PATH. Thanks again for your help.

check out this link from MS. http://support.microsoft.com/kb/86281
placing the path on a variable should be okay.
don't have time to test but maybe you can get an idea for this:

Dim strpath, strfolder, fullpath as string

strpath = "c:\"
strfolder = "FolderNametest"
fullpath = strpath & strfolder

msgbox fullpath '(just to check whether you'll get the desired path correctly)
                'fullpath display should be: c:\FolderNametest

Goodluck!

cguan gave you the option on declaring the variable into a pathname. Below is the code to play the wave sound. Your code will look something like this -

'To play .wav
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Public Const SND_ASYNC = &H1
Public Const SND_FILENAME = &H20000

Public Sub PlayWavFile(ByVal pstrFilename As String)
    If Not (Len(Dir(pstrFilename)) = 0) Then
        PlaySound pstrFilename, ByVal 0&, SND_FILENAME Or SND_ASYNC
    End If
End Sub

'under your call event like command button -
'cguan's code first -
Dim strpath, strfolder, fullpath as string
 
strpath = "c:\"
strfolder = "FolderNametest\sound.wav"
fullpath = strpath & strfolder

PlayWavFile fullpath

Was this what you were looking for?

Thank you all so much for your help. Your code seems to work just fine. Now I just have to find a way to place your code in my program. I don't think that will be very hard. Thanks again I really appreciate your help.

It was only a pleasure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.