Get first record from WMI ExecQuery - windows

I have a simple vbscript for retrieving the Windows version:
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colVersions = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objVer in colVersions
ver = objVer.Version
Next
Is is possible to get the first record or do I have to loop over all records in the collection. All examples I've seen are with For Each construction. I receive Expected end of statement error when I try:
ver = colVersions[0].Version
It looks like the return value of ExecQuery is not a proper collection.

For Each objVer in colVersions
ver = objVer.Version
exit for
Next

On Windows Vista and later, you can use the ItemIndex method to get a collection item by its index:
ver = colVersions.ItemIndex(0).Version
On earlier Windows versions, there's no way to do this I'm afraid.

Set objWMI = GetObject("WinMgmts:{ImpersonationLevel=Impersonate}!\\.\Root\CIMV2")
Set objOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem").ItemIndex(0)
msgBox objOS.Version
Edit for Explanation:
By adding .ItemIndex(0) to your original query, you are grabbing the first item in the collection. This will eliminate the need for a For/Each loop.

Related

How to browse a query result?

I have a query:
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'
but I didn't know to browse my result.
I try this but it doesn't work
req.Open sqlPU, oConn
If Not req.EOF Then
Set resultSet = oConn.Execute(sqlPU)
For Each result In resultSet
WScript.Echo resultSet.Fields(num_uti).Value
Next
End if
Try restructuring the code like this;
Dim oConn, req, sqlPU
'Create connection
Set oConn = CreateObject("ADODB.Connection")
Call oConn.Open("yourconnectionstring")
'Set query
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'"
'Open recordset
Set req = CreateObject("ADODB.Recordset")
Call req.Open(sqlPU, oConn)
'Loop the return data
Do While Not req.EOF
'Output field called num_uti.
WScript.Echo req.Fields("num_uti").Value
'Move to next record
Call req.MoveNext()
Loop
'Clean-up
Call req.Close()
Set req = Nothing
Call oConn.Close()
Set oConn = Nothing
The code in the question has a few issues,
The sqlPU query string is missing a string termination character (") and will cause a syntax error.
When you call .Open() on a ADODB.Recordset it is populated and can be traversed using a Do loop and .MoveNext() method, the extra .Execute() you do is not required and re-runs the same query again. The .MoveNext() method tells the ADODB.Recordset to move it's pointer 1 record, once it reaches the end the value of .EOF will equal True. Without .MoveNext() the pointer will not move past the end of the file (EOF) and the loop will run indefinitely or until the script falls over, neither of which is good.
Referencing columns using .Fields() collection requires either an index (ordinal numeric value starting from 0) or a string containing the alias of the column (in this case "num_uti"). In the original code the column alias wasn't a string which means VBScript would assume it's a variable called num_uti and because num_uti is uninitialised it would fail.

Classic ASP Iterate through and Object

I'm a PHP developer, learning ASP.
I've become very reliant on PHP's useful functions: print_r() and var_dump() to see what an array or object contains.
I don't always know what columns are in a Db Table. So, when a SELECT * From Tbl is queried, and the objRS is populated, would I be able to view what the entire object's contents are?
Is this possible in ASP?
<% `my simple Select statement
Dim strDbConnection
Dim objConn
Dim objRS
Dim strSQL
strDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb;"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strDbConnection)
strSQL = "SELECT * FROM persons"
Set objRS = objConn.Execute(strSQL)
If objRS.EOF Then
Response.Write("No items found")
Else
Do While Not objRS.EOF
' show all columns I can extract here....
objRS.MoveNext()
Loop
End If
objRS.Close()
Set objRS = Nothing
objConn.Close()
Set objConn = Nothing
%>
-- I would like to see what's coming back at me in the objRS, and then cherry-pick the columns after I know what I have access to.
Is there something similar to what I'm used to?
ie: print_r()
You can use objRS.fileds(j).name and objRS.fields(j).value to get the name of columns and values.
For example:
[...]
for j = 0 to objRS.fields.count - 1
response.write(objRS.fields(j).name & " = " & objRS.fields(j).value)
next

How to translate a part using CATscript in CATIA?

I working with CATscript in CATIA to create Macros. I am trying to create a CATscript to translate a feature in CATIA.
When I run the CATscript I Should select the feature that should be translated and and the feature will be translated.
But I am getting an runtime error Type mismatch:'part1.CreateReferenceFromObject'
I could not find the solution for this problem.
Looking forward for your help.
Thanks in Advance.
Sub CATMain()
Set partDocument1 = CATIA.ActiveDocument
Set part1 = partDocument1.Part
Set hybridShapeFactory1 = part1.HybridShapeFactory
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(1.000000, 0.000000, 0.000000)
Set hybridShapeTranslate1 = hybridShapeFactory1.AddNewEmptyTranslate()
Set UserSel = partDocument1.Selection
Dim type1(0)
type1(0) = "HybridShape"
'--------------------------------------
'Dim input As Object
input = UserSel.SelectElement2(type1, "select input.", False)
Set reference1 = part1.CreateReferenceFromObject(input)
hybridShapeTranslate1.ElemToTranslate = reference1
hybridShapeTranslate1.Direction = hybridShapeDirection1
hybridShapeTranslate1.DistanceValue = 1.000000
Set hybridBody2 = hybridBodies1.Item("Geometrical Set.3")
hybridBody2.AppendHybridShape hybridShapeTranslate1
part1.InWorkObject = hybridShapeTranslate1
part1.Update
End Sub
your problem is that you are trying to create a reference from a Selection object.
input = UserSel.SelectElement2(type1, "select input.", False)
This returns the type Selection. You can dig into the input and get the actual object that you select.
try:
Dim myReference as Reference
Dim myExpectedObject as HybridShape 'or use variant
Set mySelectedObject = input.Item2(1).Value 'this will grab the first item from the selection collection
set myReference = part1.CreateReferenceFromObject(mySelectedObject)
'continue the rest of your code
Also, you should always clear the selection before you use a user selection as a good habit.
UserSel.Clear 'call this before you call a SelectElement selection function

How can I create datareport using recordset in vb6?

Private Sub showreport_Click()
sql = "select * from student_record_database where"
sql=sql+ Grade='" & Combo1.Text & "' AND Meal='" & Combo11.Text & "'"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub
I am using this code as record set to create a data report.
My task is to choose options from various combo boxes and then display it's report so record set is needed there..
My question is that whether this code is sufficient to create data report???
I didn't set any properties of data environment or data report such as (connection - command - sql) because I am passing this record set directly to data report,then no need to fire any sql in properties of data environment.
But unfortunately it is not showing desired output
Please help me.
Try this one:
Private sub cmdprint_click()
Dim rs as new adodb.recordset
rs.open "SQL Query Statement Here",CON, adOpenDynamic, adLockOptimistic
set datareport1.datasource=rs
datareport1.show
end sub
Notes:
The data report datasouce should be cleared during design mode. (See properties on the datareport and set its datasource property to empty.) Ohhh...one more thing, please keep in mind that you should set also the datafield property for each textbox object inside the datareport corresponding to the datafield on your database during design time...
I am using this method for a long time and it works fine.
Try this.
To add a quite to a string, use a double quite.
Also you missed spelled the second Combo1 reference as Combo11
Private Sub showreport_Click()
sql = "select * from student_record_database where "
sql = sql & "Grade=""" & Combo1.Text & """ AND Meal=""" & Combo1.Text & """"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub

Can't access multiple records being returned by sql via ADODB.Recordset object

When I execut the sql direct it returns 4 records. when I execute the oraCommand.Execute the oraResults object shows only 1 record returned it should have a count of 4.
Hence I can get the value and name for the single record. Question is how do I get the other 3 records? I have tried set oraResults=oraResults.NextRecordSet() and I get error saying not supported!
Any help will appreciated.
This is the snippet of the code:
set oraCommand = CreateObject("ADODB.Command")
'set connection and sql statement
set oraCommand.ActiveConnection = oraConnection
oraCommand.CommandText =oraSQLStatement
oraCommand.CommandType = 1 ' commandText
oraCommand.Prepared = True
set oraResults = CreateObject("ADODB.Recordset")
'run the sql
set oraResults = oraCommand.Execute
oraResults.Fields(0).Name - Name from first record is returned
oraResults.Fields(0)>Value - Value from first record is returned
Try something like this: loop through the oraResults recordset and read the records:
Do until oraResults.EOF
Response.Write oraResults("firstFieldName")
Response.Write oraResults("secondFieldName")
oraResults.MoveNext
Loop
Here's a link that might help.

Resources