• 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

    • Assuming you want the better GPU for gaming put the money into the GPU, the difference between the CPUs is negligible unless you're doing video encoding or similar. A 5700x3d/5800x3d would likely be a better CPU upgrade if you do decide to go that route. Have you tried running the RAM at 3200 or 3000 or bumping the voltage up a bit? It's unlikely to be "faulty" and more likely to just be an incompatibility with your motherboard or CPU memory controller. Having recently upgraded from a very similar system, that CPU is well balanced with a 3080 GPU, you might be able to pick up a decent secondhand one in your budget.
    • NAPS2 (Not Another PDF Scanner 2) 8.2.0 by Razvan Serea NAPS2 is a document scanning application with a focus on simplicity and ease of use. Scan your documents from WIA- and TWAIN-compatible scanners, organize the pages as you like, and save them as PDF, TIFF, JPEG, PNG, and other file formats. NAPS2 creates fully text searchable PDF files that can be imported and indexed within your document management system. NAPS2 is currently available in 40 different languages. NAPS2 key features: Scan documents using WIA- and TWAIN-compatible scanners Scan as many pages as you like from glass or ADF, including duplex support Rotate, flip, remove, and rearrange scanned pages Save as PDF, TIFF, JPEG, PNG, or other file formats Directly email PDFs Search through text included in your PDFs by using optical character recognition (OCR), in any of over 100 languages. Configure brightness, contrast, resolution, and other scan options Save your configurations as profiles to be easily reused later Optional command-line interface (CLI) for automation and scripting MSI installer and application-level configuration available for group policy (GPO) deployment Portable/standalone archives available Translations: English, Català, Čeština, Dansk, Deutsch, Español, Français, Hrvatski, Italiano, Magyar, Nederlands, Polski, Portugues, Russian, Ukrainian, Hebrew NAPS2 8.2.0 changelog: NAPS2 is now available on the Microsoft Store. It costs a small fee to support the developer and provide automatic updates. NAPS2 will continue to be freely available at www.naps2.com Added "Edit with" under the "Image" menu for using an external image editor Added "Share even when NAPS2 is closed" option for Scanner Sharing This will show a system tray icon and restart on login Imported file names are now used as the default file name when saving The "Apply to all selected" checkbox now stays checked Escl: Increased maximum time searching for devices from 5s to 60s Escl: Scanner IPs are now cached for faster and more reliable scanning Windows: Added an arm64 installer Windows: Replaced the "No friendly name" device name from some drivers with "Unknown Scanner" Mac: Fixed an issue where saved files didn't always have the right extension Mac: Disabled the "Apple Mail" email provider when not the default email reader Mac: Updated icons for Split/Combine Linux: Fixed issues with the Save dialog Download: NAPS2 (64-bit) | 43.5 MB (Open Source) Download: Portable NAPS2 8.2.0 | 61.9 MB Link: NAPS2 Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I still remember a prediction that in the future, the BIOS will have all the necessary drivers... for any OS. Still not there
    • No mandatory.... Once again the one-way approach (only incentives or only detractors, or as they say 'the carrot or the stick') has proven ineffective.
  • Recent Achievements

    • Week One Done
      Al_ earned a badge
      Week One Done
    • Week One Done
      MadMung0 earned a badge
      Week One Done
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      274
    3. 3
      ATLien_0
      243
    4. 4
      snowy owl
      210
    5. 5
      Edouard
      182
  • Tell a friend

    Love Neowin? Tell a friend!