• 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

    • I'm old I guess, first thing I think of is just regular input/output.
    • Now, kids, Dan O'Dowd is what we call a professional hater.
    • Billionaire slams 'Tesla Cultists' for praising Robotaxi, says it's 5+ years behind Waymo by David Uzondu Image via Depositphotos.com The Tesla Robotaxi program has kicked off in Austin, Texas, and reactions are pouring in from all corners of the internet. A select group of investors and influencers have been invited to try the service, which operates within a limited area of South Austin for a price of $4.20. While the vehicles are operating without anyone in the driver's seat, the program has specific rules for this pilot phase, including a human "safety monitor" who rides along in the passenger seat just in case things go sideways. Of course, the launch did not go unnoticed by Tesla's most vocal and well-funded critic, Dan O'Dowd. O'Dowd is the billionaire founder of a group called The Dawn Project, which has dedicated itself to highlighting what it calls critical safety failures in Tesla's Full Self-Driving software. He refers to himself as an expert in creating "unhackable" software for military and aerospace clients, and ran for U.S. Senate back in 2022 on a single-issue platform: to "make computers safe for humanity" by banning Tesla's FSD. In 2023, He was banned from advertising on X after He made promoted posts that show Tesla FSD among other things, failing to stop at Stop signs. Last year, his group, The Dawn Project, paid for a Super Bowl ad, where a Tesla equipped with FSD did not act on a child-sized mannequin in the road. That commercial ends with a message, urging parents to "boycott Tesla to keep your kids safe." Today, O'Dowd took to X to slam the launch of the Robotaxi service, saying the "Tesla Cultists are celebrating victory" over a system he believes is years behind the competition (especially Waymo). He pointed out that with only fourteen cars operating for half the day, the system was already making significant errors, a rate he claims is consistent with community-tracked FSD data. The videos shared by the creators (Rob Maurer and Ed Niedermeyer), O'Dowd mentioned in his post, appear questionable, depending on your perspective. In Maurer's video, a trip that was otherwise smooth had a few unnerving seconds of the vehicle slightly swerving into the wrong lane, correcting itself, swerving again, correcting itself, and then finally settling. The other video from Ed Niedermeyer shows something entirely different. Niedermeyer captured a Tesla Robotaxi approaching an "extensive crime scene" with multiple police vehicles parked on the side of the road. On his personal Bluesky account (Ed stopped posting on X late last year, in protest of Musk), He claims the Tesla braked hard twice for no clear reason. In his commentary, Niedermeyer argued the car "shouldn't react to any of these police vehicles," and that it was concerning how it reacted to some but not others, before stopping in the "middle of the road instead of defaulting to a minimal risk condition."
    • Arch is now also using Wayland as the default session for Plasma 6.4, with X11 session becoming optional (so upgrading to Plasma 6.4 on X11 Arch might need manual intervention). It's been well over a decade in making, but I guess the time for Wayland to be the default is finally upon us.
  • Recent Achievements

    • Week One Done
      ravenmanNE earned a badge
      Week One Done
    • Conversation Starter
      Brett76 earned a badge
      Conversation Starter
    • One Month Later
      Miguel Batista earned a badge
      One Month Later
    • Dedicated
      moojay67 earned a badge
      Dedicated
    • One Month Later
      Jim Dugan earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      659
    2. 2
      Michael Scrip
      229
    3. 3
      ATLien_0
      220
    4. 4
      Steven P.
      150
    5. 5
      Xenon
      142
  • Tell a friend

    Love Neowin? Tell a friend!