• 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

    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
    • I think you've hit the nail on the head! I logged out after disabling Cookie AutoDelete, relaunched Firefox, and returned to the site, and no pop-ups. Before claiming victory, I'm going to leave this extension deactivated, and I'll let you know the results in a few days' time. If it's this extension that's the problem, I'll uninstall it, as well as report the problem I've encountered. Thank You!  
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      330
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!