I'm weak on VB syntax - a little help would be great:
I have compiled macros named TestProg-something, TestProg-another, TestProg-xyz, etc. When each is run, I want to ensure none of the others are still active - if they are, I will simply exit from the new macro without doing anything.
The code below (from a support post) normally displays a count of how many times a specified process is running. However, instead of providing a complete process name, I want to use only the 1st 8 characters (TestProg*) - thus my attempt at Left$(Name,8). The idea is, any TestProg* count over 1 means one of my tools is already running...
Is this a syntax issue, or a limitation of the specific procedure call?
--------------------------------
Modified code from support http://www.mjtnet.com/forum/viewtopic.php?t=1652
------------------------------------------------------------------------
VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where LEFT$(Name,8)='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("TestProg"),res
MessageModal>res
---------------------------------------------
Using only Name (no Left$), it works fine. However, my Left$(Name,8) results in:
SWbemObjectSet :-2147217385
Invalid Query
Line 7, Column 1
Ok, somebody make me feel stupid already!
Thanks for any ideas... ~Marvin~
Count multiple running processes using VB - syntax question
Moderators: Dorian (MJT support), JRL
From the VBScript Language Reference (RTFM )
Left Function
Returns a specified number of characters from the left side of a string.
Left(string, length)
Arguments
string
String expression from which the leftmost characters are returned. If string contains Null, Null is returned.
length
Numeric expression indicating how many characters to return. If 0, a zero-length string("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.
Remarks
To determine the number of characters in string, use the Len function.
The following example uses the Left function to return the first three characters of MyString:
Dim MyString, LeftString
MyString = "VBSCript"
LeftString = Left(MyString, 3) ' LeftString contains "VBS".
Note The LeftB function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.
Requirements
Version 1
See Also
Len Function | Mid Function | Right Function
Left Function
Returns a specified number of characters from the left side of a string.
Left(string, length)
Arguments
string
String expression from which the leftmost characters are returned. If string contains Null, Null is returned.
length
Numeric expression indicating how many characters to return. If 0, a zero-length string("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.
Remarks
To determine the number of characters in string, use the Len function.
The following example uses the Left function to return the first three characters of MyString:
Dim MyString, LeftString
MyString = "VBSCript"
LeftString = Left(MyString, 3) ' LeftString contains "VBS".
Note The LeftB function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.
Requirements
Version 1
See Also
Len Function | Mid Function | Right Function
MJT Net Support
[email protected]
[email protected]
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Thanks for the replies, but Left(Name,8) still gave the same results. I dug a little further, and made it work by using the LIKE operative , instead of "= Left(x,y)".
The sample code is shown below. I hope this is helpful to others
SUPPORT - consider if this snippet (derived from your previous efforts) would be worthy of being placed with http://www.mjtnet.com/forum/viewtopic.php?t=1652. I'm not looking for recognition, I just wish it had been there when I started looking!
To understand the purpose of this code segment:
I have a series of compiled macros that are well coded for timeouts, error checking and error reporting (via dialog screens). Each performs a specific function, then closes - error checking and timeouts prevent a macro from getting 'hung' for more than a few seconds at any point. However, I would get a major system hang if a second macro was started before the first was finished.
This code snippet is at the beginning of each compiled macro, and the name of each executable file begins with "TestTools". When a macro is called, it first checks to see if there is more than one occurance of a TestTools process running (If>%res%>1), in which case, there is a double-beep, and the second macro exits with no action taken - initial code is not interrupted or disturbed, and system is never locked.
With the code as written below, I have fired up 5-10 instances of a compiled macro in rapid succession, yet Windows remained responsive, and everything cleared within a few seconds - NO HANGS!!!
------------Beginning of code sample-------------------
----------------start of TestTools-OneOfTen-------------
VBSTART
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name LIKE '"&ProcessName&"'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("TestTools%"),res
If>%res%>1
pla>c:\TestTools\Error.wav
pla>c:\TestTools\Error.wav
GOTO>Endit
endif
------------body of code goes here-------------
------------end of body--------------------------
Label>Endit
------------end of code sample-----------------
Thanks for a great product, and a great forum... ~Marvin~
The sample code is shown below. I hope this is helpful to others
SUPPORT - consider if this snippet (derived from your previous efforts) would be worthy of being placed with http://www.mjtnet.com/forum/viewtopic.php?t=1652. I'm not looking for recognition, I just wish it had been there when I started looking!
To understand the purpose of this code segment:
I have a series of compiled macros that are well coded for timeouts, error checking and error reporting (via dialog screens). Each performs a specific function, then closes - error checking and timeouts prevent a macro from getting 'hung' for more than a few seconds at any point. However, I would get a major system hang if a second macro was started before the first was finished.
This code snippet is at the beginning of each compiled macro, and the name of each executable file begins with "TestTools". When a macro is called, it first checks to see if there is more than one occurance of a TestTools process running (If>%res%>1), in which case, there is a double-beep, and the second macro exits with no action taken - initial code is not interrupted or disturbed, and system is never locked.
With the code as written below, I have fired up 5-10 instances of a compiled macro in rapid succession, yet Windows remained responsive, and everything cleared within a few seconds - NO HANGS!!!
------------Beginning of code sample-------------------
----------------start of TestTools-OneOfTen-------------
VBSTART
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name LIKE '"&ProcessName&"'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("TestTools%"),res
If>%res%>1
pla>c:\TestTools\Error.wav
pla>c:\TestTools\Error.wav
GOTO>Endit
endif
------------body of code goes here-------------
------------end of body--------------------------
Label>Endit
------------end of code sample-----------------
Thanks for a great product, and a great forum... ~Marvin~
Ah - you were wanting SQL syntax, not VBScript! Sorry I thought you were after VBScript syntax.
It would be difficult to provide examples showing every possible SQL scenario for all available RDBMSs. There is a point at which the developer has to read the docs of the environment they are working with.
It would be difficult to provide examples showing every possible SQL scenario for all available RDBMSs. There is a point at which the developer has to read the docs of the environment they are working with.
MJT Net Support
[email protected]
[email protected]