• 0

Exitting a vbscript based on msgbox result


Question

folks;

I'm really new at vbscripting for windows but have managed to work out some of my issues.

I'm stuck on closing the script early if a user clicks the cancel button in a msgbox.

I can get the result code but even using wscript.quit the script continues to run when I click cancel in the msgbox box.

Here's a snippet of the script I am working on

On error resume next

MsgBox "This utility will close ALL instances of Microsoft Access.", 1+32

If result = 2 then wscript.quit

For each Process in GetObject("winmgmts:").InstancesOf ("win32_process")

If ucase(Process.name) = ucase("msaccess.exe") Then

Process.Terminate

End IF

Next

Msgbox "Time to continue with other steps"

The idea is to close the script if the Cancel button is pressed or continue if OK is pressed.

Appologies for the rookie question but I can't find any guidance on this .

Thanks

5 answers to this question

Recommended Posts

  • 0

like this...

result = MsgBox("This utility will close ALL instances of Microsoft Access.", vbOkCancel) 
if(result=vbCancel)Then ' or result=2
		  wscript.echo "you chose 'cancel'.  exiting script."
		  wscript.quit
else
		  WScript.Echo "you chose 'ok'."
end If

you either add your constant values together for the display icon or you use the intrinsic name (vbOkCancel, etc.)

for instance, your "1+32" displays the ok\cancel options with the question mark image in the dialog box. not sure if that's what you were aiming for...

here's a list of the contants...

msgbox constants

  • 0

Thanks pacifica. That helps a ton.

More assistance if possible.

Once I have determined if the user wants to continue, there are a few additional steps that will follow. Each step has the potential to error, present an error message, user clicks OK and the script quits.

What's the best way to structure this.

Here is some more of the script.

'Check to see if USERFOLDER exists

'if not then create the folder

'yes then delete all files in the folder

If NOT objFSO.FolderExists(strUSERFOLDER) then objFSO.CreateFolder(strUSERFOLDER) else objFSO.DeleteFile(strUSERFOLDER & strALLFILES)

numerr = Err.number

abouterr = Err.description

If numerr <> 0 Then MsgBox "Please contact HELPDESK and provide the following information" &vbCRLF&vbCRLF& "Error number: " &numerr&vbCRLF& "Description: " &abouterr&vbCRLF& "data: " &strUSERFOLDER, 0+16, "ERROR with User folder"

Err.Clear

'COPY CHIP.MDE to local system

objFSO.CopyFile strSRCFOLDER&strCHIPMDE, strUSERFOLDER

numerr = Err.number

abouterr = Err.description

If numerr <> 0 Then MsgBox "Please contact HELPDESK and provide the following information" &vbCRLF&vbCRLF& "Error number: " &numerr&vbCRLF& "Description: " &abouterr&vbCRLF& "Data: " &vbCRLF& " " &strSRCFOLDER&strCHIPMDE &vbCRLF& " " &strUSERFOLDER&strCHIPMDE, 0+16, "ERROR copying CHIP.MDE"

Err.clear

If the first section fails, I want the script to exit when the user presses OK, ditto with the second section. there are 3 more sections after this.

thanks again

  • 0

you could do this instead for your if statements (sorta the samething pacifica posted)

If numerr &lt;&gt; 0 then
	MsgBox "Please contact HELPDESK and provide the following information" &amp;vbCRLF&amp;vbCRLF&amp; "Error number: " &amp;numerr&amp;vbCRLF&amp; "Description: " &amp;abouterr&amp;vbCRLF&amp; "data: " &amp;strUSERFOLDER, 0+16, "ERROR with User folder"
	wscript.quit
End if

Edited by primortal
  • 0

Thanks.

Everything works the way i had originally envisioned it.

Any recommendations for learning vbScripting for Windows...good books or programs...editors (I'm using Context - it is pretty basic)

One other question, Is it possible to compile .vbs files into exe's or is it worth it?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yeah it is like Aero Glass reborn! (which I loved) but I do not have any Apple devices.
    • Amazon to invest $20 billion in Pennsylvania for AI infrastructure, creating 1,250 jobs by Paul Hill The governor of Pennsylvania, Josh Shapiro, has announced that Amazon has agreed to invest at least $20 billion in AI infrastructure in the state. Shapiro noted that this is the largest-ever private sector investment in the state’s history and is set to create at least 1,250 high-paying, high-tech jobs, plus thousands of construction jobs to build the AWS data centers. The initial locations for the first two data centers are Salem Township in Luzerne County and Falls Township in Bucks County. The governor said that more sites were also under consideration, so this $20 billion figure could grow further. Aside from the obvious benefits for the locals, it also helps to keep America in a strong position in its AI race with China. Why Pennsylvania? The Shapiro administration's role “Pennsylvania is competing again – and I’m proud to announce that with Amazon’s commitment of at least $20 billion to build new state-of-the-art data center campuses across our Commonwealth, we have secured the largest private sector investment in the history of Pennsylvania,” said Governor Josh Shapiro. “This initial investment from Amazon will create thousands of good-paying, stable jobs as Pennsylvania workers build, maintain, and operate the first two data center campuses in Luzerne County and Bucks County. Our team worked closely with local leaders and Amazon to land this deal, and we continue to be actively engaged on securing additional sites in Pennsylvania – helping them secure local support, developing the infrastructure needed to support more data centers, and ensuring our permitting processes move quickly and efficiently. With this historic announcement, we’re creating opportunity for our workers, generating new revenue for our local communities, and ensuring the future of AI runs right through Pennsylvania.” Based on what Shapiro said, the state has tried quite hard to get Amazon to build there instead of somewhere else. States really have to put in effort to convince big tech, as other areas are also clamoring for businesses to set up there too. Back in 2021, we reported that Samsung had agreed to build its Taylor, Texas factory after the state made a tax break offer with the Korean company. The impact on jobs, communities, and the AI landscape Initially, Amazon will need thousands of construction workers to build the data centers and when this is done, it will be hiring 1,250 operators, technicians, and engineers to help manage the facilities - these jobs promise to be high-paying jobs. These jobs are also expected to drive up tax revenues, which will be positive for the state, counties, and municipalities. David Zapolsky, Amazon’s chief global affairs and legal officer, also said that the investments would create workforce development programs and community initiatives for the benefit of residents. It’s unclear from the announcement what form these will take.
    • Feels like Apple is kind of bringing Aero to their apple stuff, honestly. I get that same vibe.
    • Yeah a lot of people do that, but there are also terrible people on secondhand market places that will claim things like the CPU isn't working, or there's a problem with the motherboard etc It's harder to confirm to a buyer that the item is fine when it is all sold separately. I was thinking of taking out the 4070 and riser kit and lowering to 1400. I think people who would buy this sort of system would pair a better GPU with it anyway (I had a 4080 SUPER in it).
  • Recent Achievements

    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
    • Reacting Well
      eric79XXL earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      281
    3. 3
      ATLien_0
      253
    4. 4
      Edouard
      202
    5. 5
      snowy owl
      201
  • Tell a friend

    Love Neowin? Tell a friend!