?
Solved

Error while executing WMI query to check whther CD inserted or not

Posted on 2011-05-11
5
Medium Priority
?
762 Views
Last Modified: 2012-05-11
Hi I have written code to check whether Cd inserted or not in drive using WMI wuery and VB6.

But while executing the code it throwing an error in while entering into 'For each' loop i.e Runtime error  '-21456793':Automation error.

Can any one please help me to find out the solution for this.

Private Sub Command1_Click()
  Dim strComputer
  Dim objWMIService
  Dim colItems
  Dim objItem
 
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select * from __InstanceModificationEvent Within 1 Where TargetInstance ISA 'Win32_CDROMDrive' and TargetInstance.MediaLoaded = True and PreviousInstance.MediaLoaded = False ")
  For Each objItem In colItems
    MsgBox "Device ID: " & objItem.DeviceID
    MsgBox "Media Loaded: " & objItem.MediaLoaded
  Next
End Sub

 

Thanks in advance
0
Comment
Question by:sandeepsangshetty
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
Psy053 earned 1500 total points
ID: 35744013
I may be missing something, but if you're only trying to determine whether it is loaded, couldn't you simplify the query?

For example:
Private Sub Command1_Click()
  Dim strComputer
  Dim objWMIService
  Dim colItems
  Dim objItem
 
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select * Win32_CDROMDrive")

  For Each objItem In colItems
    MsgBox "Device ID: " & objItem.DeviceID
    MsgBox "Media Loaded: " & objItem.MediaLoaded
  Next
End Sub

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35744226
Also, as for the cause of your error, when you query an InstanceModificationEvent you can't use the For Each loop the way you have.  You need to use the iteration structure as shown here:
http://blogs.technet.com/b/heyscriptingguy/archive/2007/04/20/how-can-i-write-a-script-that-increments-a-counter-each-time-a-log-file-is-updated.aspx

Regards,

Rob.
0
 

Author Comment

by:sandeepsangshetty
ID: 35744284
Hi  RobSampson,
After going through the link I modified my code as below but that doesn't worked fine.
Its throwing an error saying that " Runtime Error :438: Object doesn't support this property or method"

What changes need to make to run this.

Private Sub Command1_Click()
  Dim strComputer
  Dim objWMIService
  Dim colItems
  Dim objItem
 
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select * from __InstanceModificationEvent Within 1 Where TargetInstance ISA 'Win32_CDROMDrive' and TargetInstance.MediaLoaded = True and PreviousInstance.MediaLoaded = False ")
 
 
  Do
    Set objItem = colItems.NextEvent
    If colItems <> 0 Then
        MsgBox "Device ID: " & objItem.DeviceID
        MsgBox "Media Loaded: " & objItem.MediaLoaded
    End If
 Loop

End Sub

Regards,
Sandeep
0
 

Author Closing Comment

by:sandeepsangshetty
ID: 35744352
Thanks for ur reply

I tried to Execute ur code but same automation error getting .
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35745421
Hi, the latest code you have is fine, except you need to change this bit:
objWMIService.ExecQuery
to this
objWMIService.ExecNotificationQuery

Regards,

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question