none
Using MS Access with a webcam, saving images, it freezes RRS feed

  • Question

  • I am taking over a VBA app using Access 2000 and 2002, XP SP2 and a Microsoft LifeCam. An Access form is used to take pictures of deliveries to record who received it, what time, etc. The Access form "sees" the video from the webcam in an image control; a button when clicked "takes" a picture and saves it as a jpg file. I don't know where the former developer got the code. I am not able to go to the site and step through the code.

    The problem is that after the second or third image is captured, the app freezes. I get a message window headed "Microsoft Office Access" with the message "Failed to create video window."  After the user reboots the computer, he can take another couple of pics before it freezes again. I searched for the error message in Microsoft - got "not found". 

    In the application references have been set for CommonDialog and WIA; no variables other than the string variables have been dimensioned or set outside this sub.

    It acts like it is sucking up memory (since rebooting solves the problem). I suspect that something is wrong with using the CommonDialog--that the definition of the device is not specific enough. The app uses 2 classes: WIA.VideoPreview.1 Class and WIA.CommonDialog.1 Class. I have been unable to find a source that explains their properties and methods.
     
    Can anyone tell me what is wrong or point me to possible solutions? 

    Here is the code:

    Private Sub cmdTakePicture_Click()

    On Error GoTo Err_cmdTakePicture_Click

        'the webcam (connected through the commondialog control)

        'is told to take a pic in ITM, which is then transferred to IMG.

        'IMG saves the file as a jpg

        'then displays the jpg in the image control

           

        Dim dev As Device

        Dim itm As Item

        Dim img As ImageFile

       

        DoCmd.Hourglass True

        Me!vpVideoPreview.Enabled = False

        Set dev = CommonDialog1.ShowSelectDevice

        Set itm = dev.ExecuteCommand(wiaCommandTakePicture) '*probable line for failure

        Set img = itm.Transfer

        'in case a photo had been saved and this is a re-take---

        If Len(Dir(strLabelPhotoFile)) > 0 Then

            Kill strLabelPhotoFile

        End If

       

        '*********picture is saved as a file here***************

        img.SaveFile strLabelPhotoFile

        RepaintPicture strLabelPhotoFile 'put the new photo into the form's image

        Me!imgMemberImage.Visible = True

        Me!vpVideoPreview.Visible = False

        Me!cmdRetakePicture.Visible = True

        Me!cmdSave.Visible = True

        Me!cmdSave.SetFocus

        Me!cmdTakePicture.Visible = False

        DoCmd.Hourglass False

     

    Exit_cmdTakePicture_Click:

        Set img = Nothing

        Set itm = Nothing

        Set dev = Nothing

    Exit Sub



    Sunday, May 4, 2008 2:14 AM

Answers

  • It's really time consuming to disable the preview window.  Because you have recreate the object just to take a picture.  Instead use the connection to the device already established by the preview window

    Change this line
    Set itm = dev.ExecuteCommand(wiaCommandTakePicture) '*probable line for failure

    to
    Set itm =
    Me!vpVideoPreview.Device.ExecuteCommand(wiaCommandTakePicture)

    and you can delete
    Set dev = CommonDialog1.ShowSelectDevice

    This will be really fast.  At least it worked on my computer.  I hope this helps

    Thank you,

    -Steve Drollinger
    Intern


    Saturday, May 17, 2008 9:30 PM

All replies

  • Hi Mere,
    I stumbled across your message purely by chance after scouring Google for the past 2 weeks trying to find anything relating to previewing a WIA webcam within MSAccess. I've been able to get the webcam to take the picture using similar code to yours, but I have no idea what I'm taking until it's saved!
    I was beginning to think that it could not be done, but your post gives me hope! It sounds like you have a method somewhere in there that does exactly what I'm trying to do. Any chance that you could send me the WIA.VideoPreview.1 Class?

    Anyway, back to your problem. I'm far from an expert, but the first thing that I can think of when looking at the code you've posted is that it only clears out the variables upon exit, therefore, if you took seveal photos after each other, it's never properly being reset between photos and so is just building up? 
    This could be something to do with the memory leak? Have you tried doing the following lines after each time the picture is taken/saved/decided upon? Rather than upon pressing the exit button? 

        Set img = Nothing

        Set itm = Nothing

    Feel free to completely shoot down what I've just said, but it's just my initial thoughts. Like I said, I'm far from good at this!
    Cheers,
    Matt
    Wednesday, May 7, 2008 2:10 PM
  •  MereWM wrote:
    The app uses 2 classes: WIA.VideoPreview.1 Class and WIA.CommonDialog.1 Class. I have been unable to find a source that explains their properties and methods.

     

    Try these links:
     
    I don't know if it's relevant to the problem, but the code appears to end with an Exit Sub instead of an End Sub. If that's the case I'm surprised you aren't getting compile errors. Exit Sub is used to escape a procedure partway through, but the procedure should end with an End Sub.
    Thursday, May 8, 2008 5:00 AM
  • Thank you very much and  I apologize--I omitted the last lines of the code which were the go-to Err and End Sub.

    This is a single paragraph executed when the Take Picture button is clicked so the objects are always set to nothing after they are used.  The app was passed to me with the libraries so I don't know whence they came--I only cleaned up the code a bit. However, I see in the references for my app the following libraries--maybe you have the right one already. If not, perhaps the link provided by Cringing Dragon will help:

    Microsoft Windows Acquisition Library v2.0------------C:\Windows\System32\wiaaut.dll
    WIAVideo 1.0 Type Library----------C:\Windows\System32\wiavideo.dll
    wiaview 1.0 Type Library-----------C:\Windows\System32\camocx.dll

    BTW, my form is called from an earlier form and it opens with a direct link to the camera in the video preview control as if you were looking through the camera lens; when the item to be photographed is properly positioned, the Take Picture button is clicked and this code runs, saves the photo and shows the photo in an image control (NOT the "jmg" of the code above):

    Private Sub RepaintPicture(strLabelPhotoFile)
        Me!imgMemberImage.Picture = strLabelPhotoFile
    End Sub
     
    Saturday, May 10, 2008 1:20 AM
  • Thank you very much and  I apologize--I omitted the last lines of the code which were the go-to Err and End Sub.

    I will follow up on the links - BTW, some people in an Access SIG I attend tell me that there are multiple versions of Common Dialog and they have a variety of idiosyncrasies. I am still flummoxed by the fact that the code works perfectly for the first few pics, then freezes. 
    Saturday, May 10, 2008 1:27 AM
  • You may need to set the objects to nothing in your Err_cmdTakePicture_Click section as well. If an error is encountered after the picture is taken but before it gets to Exit_cmdTakePicture_Click: then the variables are not cleared but you may not be aware that there was a problem.

     

    I don't have a webcam so can't even attempt to reproduce you situation, and I only know of one other detailed thread here on the subject. They ended up solving it themselves - I tried to help but I don't know if I was much help or not.

     

    You code is quite similar to that one. One difference I notice is that they specified WIA. before the object types at variable declaration and assignments. You said you've heard that there are variations of the commondialog, so perhaps specifying WIA.CommonDialog would help. Specifying the WIA shouldn't do any harm and might help for all the objects that are WIA objects (it avoids any possible conflict between similarly named objects from different libraries).

     

    For troubleshooting, try temporarily commenting out the On Error line. Then take pictures until it fails. What line does it fail on and exactly what error do you get?

     

    I should also add a note about the links I provided earlier. They point to the online reference for WIA 2, but most of the examples in that reference are for VB6. VBA is based on VB6, so large portions of those examples can be used in VBA - but watch out for the VB6 Forms (like custom dialog boxes). Where the examples use a VB6 Form to assign variables, you'll have to do that differently in your code.

    Saturday, May 10, 2008 2:32 AM
  • It's really time consuming to disable the preview window.  Because you have recreate the object just to take a picture.  Instead use the connection to the device already established by the preview window

    Change this line
    Set itm = dev.ExecuteCommand(wiaCommandTakePicture) '*probable line for failure

    to
    Set itm =
    Me!vpVideoPreview.Device.ExecuteCommand(wiaCommandTakePicture)

    and you can delete
    Set dev = CommonDialog1.ShowSelectDevice

    This will be really fast.  At least it worked on my computer.  I hope this helps

    Thank you,

    -Steve Drollinger
    Intern


    Saturday, May 17, 2008 9:30 PM
  • Thanks - that sounds like a real possibility. I will be trying it as soon as I can get back to the problem computers. The client gave up on this. I feel there has to be a solution but I have to wait for an opportunity to test this new idea. In the meantime I will close the thread, reactivate when I get results. 

    If anyone has had this problem I would appreciate hearing about it -- with or without a solution.

    thanks to everyone.

    MereWM
    Sunday, May 18, 2008 4:46 PM
  • MereWM,

     

    I am looking to make a similar Access-based webcam app.  Specifically, I am going to try to build a home security DB that takes pictures every few minutes, records the time and date, stores the photos (both on a local HD and to an FTP server for offsite storage), and keeps a table with links to the files by time and date.  There have been a lot of break-ins in my area lately and this seems like a good tool to have. 

     

    I was wondering if you would be so kind as to let me use your Access Database as a starting point for my app?  I'm pretty good at VBA but am not too knowledgeable when it comes to integrating hardware and such.  I assure you that I will not resell the app or anything like that.  If you would be willing to share your MDB, please let me know and I will send you my email address so we can communicate off the board.

     

    Thanks!!!  :-)

    Tuesday, August 19, 2008 2:00 PM
  • I was working on this as a consultant so I do not have the database any more (and it isn't mine to give anyway), but you are welcome to use the code from my first post - it is the key part anyway (but I forgot to put "End Sub" as the last line - don't forget to add it) - and my freezing problem was never solved because I had no way to do a valid test after that--the cause seemed to be somewhere in the user's hardware and the versions of the OCX's installed.

    What we finally did (and what might be a solution for you) was to make the camera action separate from the db. The user took a picture which was saved to a specific directory; in the Access db, the user copied the picture to its own image file, deleted the original from the camera's image file and and renamed it. In code, the db knows what to name the next image based on the name of the last image saved and it knows what the directory paths are. 

    Our camera was manually clicked only as needed. If you automate your camera to take pics and store them (each image to overlay the last one so there is always only one), you could use a timer function in Access to capture, rename and store the images whereever you wanted.

    I don't know how much this helps, but you are welcome to send your email address.




    Tuesday, August 19, 2008 2:59 PM
  • Hi - enjoyed reading this thread - we are developing an application to control a webcam from Access 2003 database, take still pictures and save the image  to a pre-defined folder.  The problem I am having is which webcam to use - Logitech ones no longer seem to support wia and I couldn't get the 'take picture' command to work with the LiveCaM3000.  What did you use?

    Many thanks for any help!
    Tuesday, March 17, 2009 7:43 PM
  • Okay I see these are really old posts but I have the same question as you.  Which web cam to use that is wia compatible.  Anyone?
    Thursday, February 17, 2011 5:00 PM
  • We have just incorporated webcams functinality in our AccessImagine product.

    I must say, this is very tricky - especially because of different API bugs under different Windows versions. For example, bullet-proof code in Windows XP may fail in Windows 7.

    I recommend to give AccessImagine control a  try, download it here - http://access.bukrek.net

    By the way, there is one not-so-funny thing about instant webcam picture capture without preview window. The problem is that first few seconds after initialization webcam translates black frames without picture, and this delay varies of system state and perfomance. In general, the input should be analyzed to detect picture appearing before taking shot. We failed to implement it in this release, but it will be done, if there is customer demand.

    Feel free to ask questions, I can even share some pieces of code - its not a big secret.


    Thursday, July 5, 2012 1:22 PM