Results 1 to 9 of 9

Thread: Need help to ...audio files.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Need help to ...audio files.

    Dear Friends,

    I got a sample code to store pictures in MS Access Database. I found it in Si_the_geek's signature (http://www.vbforums.com/showthread.php?t=376767) In that sample, there is an imagebox to show the pictures. Now, I have created a database to store audio files similar to this project. But don't know how to open an audio file (here I don't know which control to put on the form instead of imagebox)?

    The following is the code where I need help (the line is in BOLD)

    VB Code:
    1. Public Sub FillFields()
    2.     If Not (rs.BOF And rs.EOF) Then
    3.         txtDescription.Text = rs.Fields("Description")
    4.         [B]Set Image1.DataSource = rs [/B] 'setting image1’s datasource
    5.         Image1.DataField = "Picture" 'set its datafield.
    6.     End If
    7. End Sub

    Set Image1.DataSource = rs which control to be used in place of Image1.DataSource

    Need help, please.

    Please also check the following code:

    VB Code:
    1. Private Sub cmdAdd_Click()
    2.     Dim bytData() As Byte
    3.     Dim strDescription As String
    4.    
    5.     On Error GoTo err
    6.    
    7.     With dlgAdd
    8.         .Filter = "Audio Files (*.dat, *.wma)|*.dat;*.wma"
    9.         .DialogTitle = "Select Audio File"
    10.         .ShowOpen
    11.            
    12.         Open .FileName For Binary As #1
    13.         ReDim bytData(FileLen(.FileName))
    14.         End With
    15.      
    16.     Get #1, , bytData
    17.     Close #1
    18.    
    19.     strDescription = InputBox("Enter description.", "New Audio File")
    20.    
    21.     With rs
    22.         .AddNew
    23.         .Fields("Description") = strDescription  'adding record to db
    24.        [B] .Fields("Audio").AppendChunk bytData  [/B] 'adding the audio to the db
    25.         .Update
    26.     End With
    27.    
    28.     FillFields
    29.     Exit Sub
    30. err:
    31.     If err.Number = 32755 Then
    32.     Else
    33.         MsgBox err.Description
    34.         err.Clear
    35.     End If
    36. End Sub
    Margaret
    Last edited by Margaret; Dec 3rd, 2006 at 02:59 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to ...audio files.

    I hadn't noticed before (how could I have missed it? ), but the code from the tutorial does not actually extract the file from the database - instead it binds the picture to an Image control.

    This wont work for other file types (and is a 'bad' method anyway), so you need some extra code. What you need to do is save the data to a file, and then use the file in whatever way is appropriate (possibly followed by deleting the file you saved).


    Here is a sub I have written which saves the data to a file:
    VB Code:
    1. Sub SaveFileFromDB(strFileName As String, fldPictureField As ADODB.Field)
    2. 'Save data from a OLE/BLOB field to a file
    3.  
    4. Dim intFileNum As Integer
    5. Dim bytChunk() As Byte
    6.  
    7.           'open file for writing
    8.   intFileNum = FreeFile
    9.   Open strFileName For Binary As #intFileNum
    10.           'get data from database
    11.   bytChunk() = fldPictureField.GetChunk(fldPictureField.ActualSize)
    12.           'write data to file
    13.   Put #intFileNum, , bytChunk()
    14.           'close the file
    15.   Close #intFileNum
    16.  
    17. End Sub
    ..and to use it, put the following code (or similar) in FillFields, replacing both lines that use "Image1":
    VB Code:
    1. Call SaveFileFromDB(App.Path & "\temp.tmp", rs.Fields("Picture"))

    One thing to note is that in this example I have used a file name that may not work for you (temp.tmp).. for example, the way you play the audio files may need the file to have the correct extension (eg: .wav , or .mp3).

    What I would recommend is adding an extra field to the database table, called FileName (with a data type of: Text). When you save data in cmdAdd_Click, set the value of this field to the name of the file (with the extension, but not the path), using code like this:
    VB Code:
    1. .Fields("FileName") = MyFileName   'where MyFileName is a variable containing the file name
    ..you can then re-save the file using the original name, eg:
    VB Code:
    1. Call SaveFileFromDB(App.Path & "\" & rs.Fields("FileName").Value, rs.Fields("Picture"))

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to ...audio files.

    Did this work for you?

    (I'd like to update the FAQ as appropriate, but need to be sure the code is valid first)

  4. #4
    Banned
    Join Date
    Oct 2006
    Posts
    98

    Re: Need help to ...audio files.

    dont work for me. Too many bugs.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to ...audio files.

    Can you show us how you are using it?
    (it would be best to upload the code files and database).

  6. #6
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Need help to ...audio files.

    Hi si_the_geek

    I have ammended the code as above shows and it seems to be saving the word document files, but...
    how do I then show then when I open them?

    I changed temp.tmp to temp.doc.

    I have attached the project, but with the mdb the file is too big, so I have shown an image.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by aikidokid; Apr 25th, 2007 at 10:59 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to ...audio files.

    Oh my, I've had a bigger delay in replying than I thought! (and possibly too late for you too)

    The best method for most file types (unless you have specific code in your app to deal with the file type) is to use ShellExecute as in your thread about this.

    Assuming that you used the example from the end of post 2, the code could be like this:
    Code:
            ShellExecute Me.hwnd, "open", App.Path & "\" & rs.Fields("FileName").Value, vbNullString, "", SW_SHOWNORMAL

  8. #8
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Need help to ...audio files.

    Quote Originally Posted by si_the_geek
    Oh my, I've had a bigger delay in replying than I thought! (and possibly too late for you too)
    No problem si, I know you would get back when you could

    I have replied to this in that other thread.

    You said you wanted feedback so you might use this as a FAQ.
    Well, it all works for me. I tried it with JPEG, GIF and word documents (.doc).

    Thanks
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to ...audio files.

    Excellent, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width