472,145 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

WIA image capture in Windows 7

200 100+
Using the code below to capture images in Access. On my form is an unbound OLE image and a single button. Clicking the button when a digital camera is connected via USB and turned on causes the camera to take a pic and show it in the OLE image frame.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGrabImg_Click()
  2.     'Take a picture from a webcam and store it in a temp file.
  3.     'By Justin Johnson Nov 26, 2007
  5.     On Error GoTo Err_btnTakePicture_click
  7.     Dim tempfile As String
  8.     Dim mydevice As WIA.Device
  9.     Dim item As WIA.item
  10.     Dim imfile As WIA.imagefile
  11.     Dim Commondialog1 As WIA.CommonDialog
  12.     Dim MyFilename As String
  14.     MyFilename = "C:\Documents and Settings\" & Environ("Username") & "\Desktop\" & "WIAGrab" & ".jpg"
  16.     'put the path and name for the location of your temp file here.
  17.     tempfile = ("" & MyFilename & "")
  19.     'the next 4 lines deletes the old temp file if it exists
  20.     Set filesystemobject = CreateObject("Scripting.FileSystemObject")
  21.     If filesystemobject.FileExists(tempfile) Then
  22.         Kill (tempfile)
  23.     End If
  25.     'the next two lines set up the configuration
  26.     Set Commondialog1 = New CommonDialog
  27.     Set mydevice = Commondialog1.ShowSelectDevice
  29.     Set item = mydevice.ExecuteCommand(wiaCommandTakePicture)        'instructs the camera to take the picture
  31.     Set imfile = item.Transfer        'transfers the picture from the camera
  33.     'this line saves the picture to a specified file
  34.     imfile.SaveFile (tempfile)
  36.     'this sets the picture on the form to show the new picture
  37.     Me.OLEUnbound1.Picture = (tempfile)
  39.     'MsgBox "Picture taken"
  41. Exit_btnTakePicture_click:
  42.     Set mydevice = Nothing
  43.     Set item = Nothing
  44.     Exit Sub
  46. Err_btnTakePicture_click:
  47.     MsgBox Err.Description, vbOKOnly + vbCritical, "Error Taking Picture"
  48.     Resume Exit_btnTakePicture_click
  50. End Sub
Works great in Windows XP but fails in W7 with an error stating: "object variable or with block variable not set". The camera does open up and flash so it appears to take the picture but no result in image frame.

Any ideas what is causing this to fail?

Jan 30 '12 #1
18 23322
446 Expert 256MB
For this to work I presume you will have installed a device driver for your camera which will have included a '.dll' that installs Windows Image Acquisition services.

My presumption is that you will need a different driver for Windows 7

If you set a break point at the top of the code then step through it, you should be able to identify the line where the error ocurrs.

Running your code without any sort of WIA driver causes a 'User-defined type not defined' error at Line #7.

Jan 30 '12 #2
32,499 Expert Mod 16PB
but fails in W7 with an error stating: "object variable or with block variable not set".
I shouldn't be needing to tell you the basics like which line this occurs on Tux. You've also failed to include the procedure header line, which is often required to make sense of code - especially when the code posted is 50 lines. I won't delete the question as I'm sure you'll fill in the details, but please remember for future questions.
Jan 30 '12 #3
200 100+
Thanks Sierra - The camera when connected via usb I assume installs a universal driver, yes. It installed fine in XP and W7 with no interaction (Windows auto-installed drivers). It's a modest Nikon L22 point and click.

NeoPa - If I comment line 26 or 27, I get the error mentioned and the camera does not take the picture. Leaving 26 and 27 in, I get the error mentioned and the camera snaps a shot. Looking at the camera SD card I see the image gets stored there, so I suppose it's just not transferring. I have read more recently that WIA may have issues with W7. Can't seem to find much on this topic unfortunately.
Jan 31 '12 #4
32,499 Expert Mod 16PB
It sounds like you don't know how to determine which line the error occurred on. When the error is reported, which line is highlighted in yellow in the debugger when you select Debug?
Jan 31 '12 #5
200 100+
There is no debug option in the dialog box that is presented.
Jan 31 '12 #6
446 Expert 256MB
Hi again,
If you say the code works in XP, I can't think there is anything wrong with the code per se, which I why I suspect a driver problem.

I've Googled 'windows 7 driver for Nikon L22' and found the a number of sites purporting to supply updated drivers. Note that there are 32 and 64 bit versions of software that have minor differences (What version of Access are you using? There are some issues with Service Pack 1 in the 64 bit version of Access 2010. Not certain what because I'm not affected by using 32 bit on 64 bit Win7, but I'll check it out)

As you seem to be able to communicate to the camera but the system fails when processing the returned image to the OLE image frame, i still favour a driver update.

PS. I've checked out the issue I mentioned above. It appears to be associated with running Wizards and generally not working. Here's a link "Access 2010 - Wizards not working after installing SP1"
Jan 31 '12 #7
32,499 Expert Mod 16PB
There is no debug option in the dialog box that is presented.
It sounds like you haven't set your system up correctly for debugging then. Ooooops. Debugging in VBA - 3) General Tips can help you with that.

You may find that much of your development will become easier after this point.

PS. This doesn't argue with S7's comments and advice. They may be absolutely correct (I expect they are). However, it's still always important to include such information as the line number where the error occured as the basic details of a question if posting code that crashes :-)
Jan 31 '12 #8
200 100+
Sierra - I am running 32 bit Access 2010. Also note on the MS site they say the L22 is compatible with W7 and no further drivers are necessary. I too looked at the many sites mentioning updated driver files, but those that I found are just feeder sites to get one to download non-related software which searches for updated drivers on your PC.

NeoPa - thanks for the tip :)

My code is failing at Line 31.
Feb 1 '12 #9
32,499 Expert Mod 16PB
Good for you Tux.

I'm afraid my comments were more as a moderator than as an expert as I have no experience with this device or driver. Having your code posted with the error message and line number included are always helpful though, and if it's helpful to any of us then it benefits you too, clearly.

Good luck with your problem.
Feb 1 '12 #10
200 100+
Thanks NeoPa. I appreciate all you do. The bashing I get from you from time to time is clearly deserved. I WILL get this. I am just a bit dense at times ;)
Feb 1 '12 #11
200 100+
I've established that the procedure above will activate a camera and save the pic to the SD card of the camera in XP and W7. I need code that can find the most recent saved file in a directory of my cameras' SD card (would be the current image), and save that path to an Access table. Seems doable, but the code below fails at line #13 (explanation follows):

Expand|Select|Wrap|Line Numbers
  1. Public Function GetMostRecentFile()
  2.     Dim FileSys As FileSystemObject
  3.     Dim objFile As File
  4.     Dim myFolder
  5.     Dim strFilename As String
  6.     Dim dteFile As Date
  8.     'set path for files - change for your folder
  9.     Const myDir As String = "L22" '<<<fails here
  11.     'set up filesys objects
  12.     Set FileSys = New FileSystemObject
  13.     Set myFolder = FileSys.GetFolder(myDir)
  16.     'loop through each file and get date last modified. If largest date then store Filename
  17.     dteFile = DateSerial(1900, 1, 1)
  18.     For Each objFile In myFolder.Files
  19.         If objFile.DateLastModified > dteFile Then
  20.             dteFile = objFile.DateLastModified
  21.             strFilename = objFile.Name
  22.         End If
  23.     Next objFile
  25.     MsgBox strFilename
  27.     Set FileSys = Nothing
  28.     Set myFolder = Nothing
  30. End Function
I understand some cameras connect via USB using PTP (Picture Transfer Protocol) while others, especially the older and more high end cameras use MSC (Mass Storage Communication) or have the ability to choose the communication protocol. MSC devices are assigned a drive letter while PTP cameras are not. So that's a dead end unfortunately.

That said...

Is there a different way to transfer the most recent camera image captured? As mentioned, my code posted at #1 works fantastic in Windows XP, but not in W7.
Feb 1 '12 #12
446 Expert 256MB
OK, I seem to have been on the wrong track here! I had assumed that the extra driver required to give you WIA functilonality was installed by the Nikon software.

Silly me; you seem to have downloaded the WIA Development kit from Microsoft. There are full instructions and example code included in the kit.

The required dll is WIAaut.dll for the WIA Automation layer. When I came to copy it from the kit I found there was one already installed (I'm using Win7 Ultimate. My version was 6.1.7600 the file from the kit is version 5.1.2600). Ive kept both but set a Reference in Access to the latest version. Although it was located in the System32 folder it was 'not on the list' and had to be browsed to.

I can't test out the code because although I have acquired a Nikon S4150 it does not seem to respond.(just yet!)

Refering back to your last post (find latest file) the code does work if a valid path (eg c:\L22) is defined at Line #9

However, when searching for a dll to control my camera I found that Nikon supply a fully functional application Camera Control Pro 2
It's far more comprehensive than I would write myself. I'm not sure what they will charge when the trial period expires!
Feb 6 '12 #13
200 100+
Correct S7, that is the WIA download.
Feb 6 '12 #14
446 Expert 256MB
I'm stll looking into this for my own interest but making slow progress because of other matters.

I've abandoned Win7, for the moment because it would not recognise the camera, so nothing would work. I can confirm your findings that thing works much better in XP.

While still using XP I found that although I could see the camera in Windows Explorer I could not read from the memory card. However, there was a utility in the Nikon software which came with camera that seems to have switched it so the memory card can now be read, but it still does not respond in Access 2010.

In passing, the camera is not assigned a drive letter so cannot be addressed using the syntax you used. It seems that your "L22" may be a volume name and will have to be referred to differently using "\\.\Volumename(Unique Identifier).." type of syntax. This is new to me but I'll be more clear when I get it working.

I am able to take pictures in Access 2003 using your code (well, it did not like the OLEUnbound1 object and I had to convert it to an Image frame, and I had to add a Commondialog1 control to the form)

Interestingly, in Access2010 it is possible to add a WIA.DeviceManger control to a form but it is not possible to add a WIA.CommonDialog control from the ActiveX list on the ToolBar. It gives a message saying the dll is nor registered but when you run RegSvr32 wiaaut.dll, it responds it has registered successfully. I think this is the hub of the problem.

I'm adding some code that cycles through the properties of the camera. This allows you to see the Unique Identifiers by which the camera is known to the system, where-upon you may be able to access the memory card, maybe! This is from MSDN Shared Samples
Expand|Select|Wrap|Line Numbers
  1. Dim dev 'As Device
  2. Dim p 'As Property
  3. Dim s 'As String
  4. Dim i 'As Integer
  6. Set dev = CommonDialog1.ShowSelectDevice
  8. For Each p In dev.Properties
  9.     s = p.Name & "(" & p.PropertyID & ") = "
  10.     If p.IsVector Then
  11.         s = s & "[vector of data]"
  12.     Else
  13.         s = s & p.Value
  14.         If p.SubType <> UnspecifiedSubType Then       
  15.             If p.Value <> p.SubTypeDefault Then
  16.                 s = s & "(Default = " & p.SubTypeDefault & ")"
  17.             End If
  18.         End If
  19.     End If
  21.     If p.IsReadOnly then
  22.         s= s & " [READ ONLY]"
  23.     else
  24.         Select Case p.SubType
  25.         Case FlagSubType
  26.             s = s & " [ valid flags include:"
  27.             For i = 1 To p.SubTypeValues.Count
  28.                 s = s & p.SubTypeValues(i)
  29.                 If i <> p.SubTypeValues.Count Then
  30.                     s = s & ", "
  31.                 End If
  32.             Next
  33.             s = s & " ]"
  34.         Case ListSubType
  35.             s = s & " [ valid values include:"
  36.             For i = 1 To p.SubTypeValues.Count
  37.                 s = s & p.SubTypeValues(i)
  38.                 If i <> p.SubTypeValues.Count Then
  39.                     s = s & ", "
  40.                 End If
  41.             Next
  42.             s = s & " ]"
  43.         Case RangeSubType
  44.             s = s & " [ valid values in the range from " & _
  45.                    p.SubTypeMin & " to " & p.SubTypeMax & _
  46.                    " in increments of " & p.SubTypeStep & " ]"
  47.         Case Else 'UnspecifiedSubType
  48.         End Select
  49.     End If
  51.     MsgBox s
  52. Next
I just put the code under a command button and the information is displayed in a series of MessageBoxes.
Feb 9 '12 #15
200 100+
Very nice S7. I am looking forward to seeing what you come up with - Your knowledge and understanding about how to get this done is far better than mine.
Feb 9 '12 #16
446 Expert 256MB
This will be too long for a PS..
Obviously, you can read the mempory card if you extract it from the camera and insert it into a cardreader slot, as it's then assigned a drive letter.

I'm interested in developing this for astronomy, when it's too cold to go outside to the telescope! As the images are transferred to the pc OK I will just number them sequentially and store them locally (as well as on the memorycard), so finding any particular shot will just be a matter of browsing. There is a browser in the Development Kit which just has to be converted from VB To Access VBA.
Feb 9 '12 #17
200 100+
I may just end up buying a camera with MSC functionality. But I would still like to see if my trusty Nikon L22 can work somehow. Have you had any time S7 to move forward on this? The stars outside are waiting ;)
Feb 24 '12 #18
There is an error in the original code above on line 37. It should read:
Me.OLEUnbound1.Picture = LoadPicture(tempfile)
Jun 25 '13 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Gey-Hong Gweon | last post: by
3 posts views Thread by Catalin Lungu | last post: by
4 posts views Thread by | last post: by
2 posts views Thread by Paul Aspinall | last post: by
reply views Thread by subirose | last post: by
2 posts views Thread by ghostrider | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.