How to exit all the VB Scripts which was started by me - scripting

I have two VB Scripts, say First.vbs and Second.vbs.
First.vbs calls Second.vbs each time some action/event happens.
I want to write a new VB Script, say Third.vbs, which terminates all the instances of First.vbs and Second.vbs including itself, Third.vbs.
Can anyone give some suggestion/reference?

Try this:
Set wmi = GetObject("winmgmts://./root/cimv2")
Sub TerminateScript(name)
qry = "SELECT * FROM Win32_Process WHERE CommandLine LIKE '%" & name & "%'"
For Each p In wmi.ExecQuery(qry)
p.Terminate
Next
End Sub
TerminateScript "Second.vbs"
TerminateScript "First.vbs"
WScript.Quit(0) 'terminate self

You can also use the following query:
"SELECT * FROM Win32_Process WHERE CommandLine LIKE '%wscript.exe%'"
Please note this may stop all the processes running in a machine, with the name wscript.exe.

Related

Pass Through Query Won't Run From Command Line

I created a pass through query in Access.
All it says is .
sp_Submit ''
If I click on it directly it runs the SSMS stored procedure which just changes some test tables.
However if I run it in VBA it does not work. It doesn't error out or anything, it just does not work.
I have tried
sSQL = "sp_Submit '" & Me.cboNumber & "'"
and
sSQL = "sp_Submit '"
Please not the stored procedure isn't doing anything much at this point. I have it testing some stuff. It just deletes everything in one table and inserts it in another.
What am I doing wrong? I've used this in the past and it has worked so I'm not sure why it doesn't work this time. The stored procedure itself is set up to accept one variable but it doesn't actually do anything with it (yet.).
Thank you.
Given what you have to far, then you should be able to edit the saved pass though query with
Sp_Submit 'test'
Assuming the 1 paramter is text. If the parameter is to be a number, then
Sp_Submit 123
At this point, you have to save that query. Now click on it, does it run correctly?
And in place of clicking on the query, you can certainly do
CurrentDB.execute "name of saved query goes here"
However, keep in mind that a PT query is raw T-SQL. What executes runs 100% on the server. This means that such query(s) cannot contain references to any form, or anything else. So you have to “pre-process” or create the value you wish to pass and modify the PT query. So your code of creating the SQL you have looks correct, but you then have to take that sql string and "set" the PT query.
The most easy way to do this is with code like this:
With CurrentDb.QueryDefs("MyPass")
.SQL = "sp_Submit '" & Me.cboNumber & "'"
.Execute
End With
Of course if the PT query returns records, then you would go:
Dim rstRecords As DAO.Recordset
With CurrentDb.QueryDefs("MyPass")
.SQL = "sp_Submit '" & Me.cboNumber & "'"
.ReturnsRecords = True
Set rstRecords = .OpenRecordset
End With
And to be fair, you likely should set “returns” records = false in the first example. You can do this in the property sheet of the save query, or in code like this:
With CurrentDb.QueryDefs("MyPass")
.SQL = "sp_Submit '" & Me.cboNumber & "'"
.ReturnsRecords = False
.Execute
End With
Note that you can use the one PT query over and over in your code. So once you ceated that one PT query, then you can use it to pass any new sql you wish, such as:
Dim rstRecords As DAO.Recordset
With CurrentDb.QueryDefs("MyPass")
.SQL = "select * from tblHotels"
.ReturnsRecords = True
Set rstRecords = .OpenRecordset
End With
So you can "stuff" in any sql you want for that one PT query - and use it throughout your application.

Use WMI query in VBA to get list of open files on local computer

As per title. Using this code example (3rd example down) as my starting point.
Here's my effort at VBA code, but it stalls at the first hurdle ("Run time error 438: object doesn't support this property or method")
Sub Test()
Set objConnection = GetObject("WinNT://HM10")
Set colResources = objConnection.Resources
For Each objResource In colResources
Debug.Print objResource.Path
Next
End Sub
HM10 is my computer name. Eventually it will need to be an environmental variable (if that's the right term) for whatever machine it happens to be on.
EDIT: Ok, more searching has led me to this:
Sub test()
Filename = "."
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Process WHERE CommandLine LIKE '%" & Filename & "%'"
For Each p In wmi.ExecQuery(qry)
Debug.Print p.commandline
Next
End Sub
which is closer, but only shows local files, not network files. I only need network files, how do I get them?

Automating access 2003 application

I have an access application (VBA) ( access 2003 ) , which generates 4 different text file based on its database at specific path on pressing of 4 respective different buttons.
But this is something manual , which i do everyday for the file generation.
Im in need of its automation.For example my file should get automatically generate at any specific time.
One of the button's event procedure is menitoned below :
I tried doing with help of VB script , but this is giving error .
"Provider cannot be found.May not be properly installed "
Set objAccess = CreateObject("Access.Application")
Set conn = CreateObject("ADODB.Connection")
strConnect = "Provider=Microsoft.JETs.OLEDB.12.0;
Data Source=E:\Project\test.mdb"
conn.Open strConnect
test()
function test()
objAccess.DoCmd.Hourglass True
objAccess.DoCmd.SetWarnings False
objAccess.DoCmd.RunSQL ("INSERT INTO Table1 ( name, FileName, [DateTime] ) SELECT Environ(""UserName"") AS name, ""test.mdb Generate ABCD File"" AS FileName, Format(Now(),""yyyyMMddhhmmss"") AS [DateTime];")
objAccess.DoCmd.OpenQuery ("qry_ABCD")
objAccess.DoCmd.TransferText acExportDelim, "qry_ABCD_Formatted Export Specification", "qry_ABCD_Formatted", "E:\Ouputs\" & Format(Now(), "yyyymmdd") & ".txt", False
objAccess.DoCmd.SetWarnings True
objAccess.DoCmd.Hourglass False
End function
I dnt know how to resolve this issue. Or is there any other better way to resolve this.
There is no need to setup or create some connection string in your script. Once you opened Access, then you have use of all tables in that application. And in fact in your example you create a conneciton object, but it not really required.
So, lets assume your VBA code to run is this:
Sub MyExport()
Dim strSQL As String
strSQL = "INSERT INTO Table1 ( name, FileName, [DateTime] ) " & _
"SELECT Environ(""UserName"") AS name, ""test.mdb Generate ABCD File"" AS FileName, " & _
"Format(Now(),""yyyyMMddhhmmss"") AS [DateTime];"
CurrentDb.Execute strSQL
DoCmd.TransferText acExportDelim, "qry_ABCD_Formatted Export Specification", _
"qry_ABCD_Formatted", _
"E:\Ouputs\" & Format(Now(), "yyyymmdd") & ".txt", _
False
End Sub
The above code is assumed to be saved as a sub (not a function) in a STANDARD MS Access VBA code module.
Now, your VBS scrip can call the above code like this:
dim accessApp
set accessApp = createObject("Access.Application")
accessApp.OpenCurrentDataBase("C:\some path name\someMdb.accDB")
accessApp.Run "MyExport"
accessApp.Quit
So it now a simple matter to run the above VBS script. And such a script should also just run fine via the windows task scheduler. As above shows there is LITTLE need to put the SQL and code in the VBS script. You BEST get the sub working in VBA and MAKE SURE it works. Test it many times, and THEN and ONLY then do you create the VBS cript to call that sub. So FIRST get the routine working, and get it working WITHOTU any prompts etc. You can call and test the VBA sub by placing your cursor in the VBA editor in that routine, and then hit F5 to run the sub. Once you get it working the way you want, then you use the above second VBS (not VBA script to call + run that working sub that you are NOW 100% SURE it works and runs correctly)

WMI query: select all processes under specific username?

I have this code in VBA:
strTerminateThis = "notepad.exe"
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis & "'")
I want to narrow this query down by adding another where to select a specific username that process is running under.
How can this be done? While name is the name of the process, I wasn't able to find something simple like pid_owner.
My goal would be to count how many Notepad processes are open under a specific user.
The Win32_Process doesn't expose any property related to the owner of the process, so you only option is list all the procesess and then filter manually calling the method GetOwner
.

vbscript-terminating process instance

I'm writing a script that runs an external program, and after a while terminates it, the relevant code section looks like this:
Set objshell=createObject("Wscript.Shell")
objShell.run ""app.exe""
objShell.run ""app.exe""
WScript.Sleep(5000)
strWmiq = "select * from Win32_Process where name='app.exe'"
Set objQResult = objWmi.Execquery(strWmiq)
For Each objProcess In objQResult
intRet = objProcess.Terminate(1)
Next
What I want to do is to close each of the instances of app.exe after different sleep times, any ideas how to do that?
Use WScript.Shell's .Exec method to get a WshScriptExec objects for each session/run/instance of app.exe and .Terminate them at your leisure (and risk). See Terminate Method (WshScriptExec)