article

Using Select Case (How To)

Email
Submitted on: 1/8/2019 11:30:01 PM
By: A_X_O 
Level: Beginner
User Rating: Unrated
Compatibility: VbScript (browser/client side)
Views: 140
 
     Very Basic Example That Demonstrates using Select Case in a VBScript


 
				'
'------------------------------------------------------------------------------	'
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	'
'		Demonstration: Using Select Case 				'
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	'
'										'
'	Purpose		:	Demonstrates VBScript Select Case		'
'	-----------------------------------------------------------------------	'
'	Creation Date	: 	09/01/2019 [dd/mm/yyyy]				'
'	Version		:	1.0						'
'	Designer	:	Fabian						'
'										'
'##############################################################################	'
'			MODIFICATION HISTORY					'
'------------------------------------------------------------------------------	'
'	Version		:	1:0	09/01/2019	Create the Sample	'
'										'
'------------------------------------------------------------------------------	'		
'
'##	Select Case is very useful for handling multiple options in a single variable.
'##	using this method, you can work through your task quickly and efficiently.
'
'##	I have created a Message box to pose a Vote question with three possible options.
'##	The question is: Do you like chocolate ?
'	
'##	The user can press Yes, No or Cancel on the Message Box. Each scenario
'##	will return a confirmation Message box confirming the Vote on chocolate.
'
'
'--------------------------------------------------------------------------------
'
'##	Memory Dimension for the Message Box
'
Dim Question 		'As VbMsgBoxResult
'
'--------------------------------------------------------------------------------
'
'##	Message Box Question 
'##	Message Box Text: Makes editing very easy when stored in a Const
'
Public Const MSGB_L1 = "Do you like chocolate ?"
Public Const MSGB_L2 = "Press Yes if you Do. Press No if you don't."
Public Const MSGB_L3 = "Press Cancel to Exit."
Public Const MSGB_L4 = "Chocolate Vote."
'
'--------------------------------------------------------------------------------
'
'##	Message Box: Cancel or Close without pressing Yes or No will use this Text
'
Public Const MSGErr1 = "You have chosen to exit without voting."
Public Const MSGErr2 = "Good Bye"
'
'--------------------------------------------------------------------------------
'
'##	Message Box Vote Confirmation text
'
Public Const VotedYes = "You voted Yes to liking chocolate."
Public Const VotedNo = "You voted No to liking chocolate."
Public Const VoteTop = "Vote Registered"
'
'--------------------------------------------------------------------------------
'
'##	Message Box Return is stored in *Question*
'
	Question = MsgBox(MSGB_L1 & _
		VbCrLf & MSGB_L2 & VbCrLf & VbCrLf & MSGB_L3, _
		VbSystemModal + VbExclamation + VbYesNoCancel, _
		MSGB_L4)
'
'--------------------------------------------------------------------------------
'
'##	Using *Select Case* to check each Message Box Scenario which could be:
'##	VbYes | VbNo | VbCancel because of the *VbYesNoCancel* option used.
'
Select Case Question
'
'--------------------------------------------------------------------------------
'
'##	Each possible *Case* you want to handle, is written like this. Case VbYes 
'##	Or Case VbNo and so on... 
'
Case VbYes
'
'##	Handle the Message Box VbYes option. A simple notify Message to the user.
'##	But could call another Sub-Routine here or Event.
'
	Msgbox VotedYes, _
		VbSystemModal + VbOkOnly + VbInformation, _
		VoteTop
'
'--------------------------------------------------------------------------------
'
'##	Handle the Message Box VbNo option. Again, a simple notify Message to the user.
'##	But could call another Sub-Routine here or Event.
'
Case VbNo
'
	Msgbox VotedNo,	_
		VbSystemModal + VbOkOnly + VbInformation, _
		VoteTop
'
Case VbCancel
'
	Msgbox MSGErr1,	_
		VbSystemModal + VbOkOnly + VbInformation, _
		MSGErr2
'
'--------------------------------------------------------------------------------
'
'##	There are no further Cases needed to be handled, so End the Select. 
'##	You could add a Case Else statement and it's probably good practice to do
'##	that. But I haven't used that here.
'
'--------------------------------------------------------------------------------
'
'Case Else
'
'	Msgbox "Some Text", VbMsgBoxStyle, "Msgbox Title"
'
End Select


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.