Friday, 23 January 2015

PowerPoint VBA for Quiz (Part 2)

It's great to have you back on the part two (2) of PowerPoint VBA for Quiz. In the previous part of the PowerPoint VBA for Quiz, the code so far, was to tell the users if they are right or wrong.



Today, we shall improve our quiz with some new sets of codes. It's going to include some explanations, so find your time because it will be fun! Our quiz in the previous part prompts users to input their names, which will be saved for later use; but one of the duties of a good programmer is the ability to suggest errors (this is some act of debugging of the program at hand). We asked the user to input something; the user might just enter a space or leave it empty and gain access into the quiz. We wouldn't want that to happen!
In this post, we shall restrict our users to input something before they can proceed. Got that? Awesome!
To make this post easy-to-understand, I will now explain some aspects of VBA Programming that we will use.
For more aspects of the VBA Programming, I recommend you to read VBA Programming

Introducing While-Do, If-Else loops, Booleans, Assignment Variable
If you know all the above mentioned; you can skip to the coding tutorial below
In programming – no matter the language you use for your program – there were some likeliness among programming languages. For example, the word 'loop' is common to all the programming languages including VBA. They are conditional statement; in the sense that it enables us to restrict users or our program from doing something unless an action was performed. Confused? Don't be.
To explain that in a clearer sense, think of your FACEBOOK account; if you enter you username, but fail to enter the password; you will get an error message. That's in likeliness with loops in programming. Understood? Good!

While-Do loops
This is a kind of loop that can perform an action while another action is going on. For instance; While James is typing, play (do) the music! As simple as that, don't forget that, we will need it later.
If-Else loops
In some sense like the while-do, this does an action if it is what we want it to do; else it does another action. Let me elaborate that.
Example: If my book is on the table, then take it to my room, else (if it is not there) go and sleep. Very simple!
Boolean
Boolean is a data input type (e.g. input types include numbers, texts, strings, Boolean long….). It consists of only two options; True and False. This means that Boolean type of data can only be either True or False.
Assignment Operator (:=)
Mathematically, x cannot be equal to (x + 1) i.e. x ≠ x + 1, but in programming, it can! That's where assignment operator comes in. If I wrote x := x + 1, I am telling the computer that whenever this procedure was called (i.e. whenever this code is running), it should add 1 to the value in the x.
If the procedure was called three different times, it will have added 3 to the initial value of x. This can be use to keep record of the scores of the user(s). More on that later.
What differentiate the assignment operator from the mathematics' equality sign, is the presence of the colon before it. That is :=

Comments
Comment is a great feature that helps in coding, when you comment on your codes; you are saving yourself the inconveniencies of troubleshooting or debugging; making it easy for review process; help others know how you go about a code.
Programming languages have different comment style/format. To comment in Pascal, it will be *Comments goes here *; to comment on VBA, it will be 'Comments goes here.
Example:       'This is a comment by Jacob thanks for visiting my blog. Happy Surfing!
Anything within a single inverted comma is regarded as a comment by the Visual Basic Editor


Open your Visual Basic Editor from the developer tap. Oops! Developer tab wasn't there? Check this post to find how to show the developer tab
On the VBE window, click on Insert to insert a new Module
Type in the following;
Sub Username()          'This is a comment that explain the code
Dim username as String
Dim proceed as Boolean   'proceed values set to either True/False type of data
While Not Proceed             'before proceeding
Username = inputbox (prompt:="Enter your username", title:="Username") 'we provided inputbox
If username ="" then                      'if username is empty then
Proceed is false                   'Go back to line 5 above
Else                                'if something different was entered
Proceed is true                               'Proceed to the next code
End If                                              'use to end an if function
Wend                                     'Use to end while sentence
End sub()                              'This end the whole procedure

Now, go to the Run tab on the window's menu bar and select Run; if all was well with your code, you should see your code run. The only thing left is to link your macro/code to an action button. Preferably the Proceed button on your PowerPoint Presentation

Drop your comments if you had found this post useful. More updates soon.

No comments:

Post a Comment

Please drop your comments

Back to Top

Enter your email address:

Delivered by FeedBurner

Advertisement