Visual Basic is a programming language that allows you to create executable programs, in other words, the EXE files that run a program. First released in 1991, this program was designed to make it easier for those that may be unfamiliar or new to programming. With many improvements, the final version was released in 1998. A similar program VBA (Visual Basic for Applications) should not be confused with VB (Visual Basic), as VB is a standalone program, while VBA runs off of other Microsoft programs. Visual Basic can seem confusing with the capability of creating a simple application or a complex one. Many different types of applications can be created with VB such as something simple like a calculator or something more complex like a learning application or business software.
There are many possibilities in creating a program when using Visual Basic. With the many possibilities comes a large amount of different commands that can be incorporated into the application. While going over every single one of these would be possible, it would take a lot of time and it is not likely that someone would want to. The reason for this is that, each creator can customize their commands for the specifics that they want the program to perform. A walkthrough of the program can be beneficial as it can show you some of the functions that can be performed. The best way to learn is to experience what the functions will do.
Finding yourself looking for more information? Infinite Skills can provide you with a different way to use Visual Basic with Excel.
Creating a Project
This first and obvious step to creating a program, is to create a new project in the Visual Basic program. To do this, simply select File and then Create New Project. This project will have a default name given by the program and it is important that you save your new project and give it the name you have picked for your project. For this example I will simply create a new web browsing application and for this type of application you must select the Windows Form Application option. When the project is formed you will see the IDE (Integrated Development Environment) that consists of the application that will be labeled Form 1, which is a form designer; a solution explorer window that contains all the needed files; and below that a properties menu that allows you to change many features of the form. The next step is to create the user interface, which is what the people who use the application interact with.
Creating the User Interface
When creating the user interface you must utilize the toolbox that is located to the far left of the screen. As this toolbox contains all of the controls available for use, it is helpful to pin the toolbox so that it will stay open. In the toolbox there are many options to choose from, but for this example you will want to first choose the panel control and drag it to the form. This will keep the functions put into this box separate from the ones that are on the rest of the page. The next option you will choose is the text box, which will be used for the URL searches in the web browsing application. Along with the textbox, you will want to choose the button and drag that to the form also. Lastly, the rest of the form will contain the web browser control, which is what enables the user to see the web page that has been searched for. While the look and feel of this application, up to this point, may not seem usable, the next step is the last step before you get into the specific codes needed to make the application function the way you would want a web browser to perform. You can also resize and use the properties menu in this step to make the application look the way you like.
Creating an Application the Looks Good and Performs
During this step you will make the form look more usable. To make the form take the shape of a web browsing page you first want to select the Panel that you placed first in the form. Then you will look to the Properties section where you will find the Layout category where you will want to select the Dock property. This will provide you with a small window with various boxes. The panel will contain the URL search and the button that will launch the search, therefore you will select the Top box to put these at the top of the page. You may want to resize the panel afterwards to allow more space for the web browser control that shows the results of the search. It is also wise to make the Text Box longer to fit more lengthy URLs. You will also want to select the Button control and select Text in the Properties section. Delete the default name and type “Go!” to make it look more like a web page. You can tailor this page to make it look the way you see fit and then you will move on to the coding section of creating an application.
Adding Code and Direction to Your Application
When you add Visual Basic code to the form you basically command the application to perform the tasks that are needed for the application to run. This is a simple application and you want the “Go!” button to launch the search for the URL the user types in. To make this happen, select the Button and a Code Editor will open. When it opens you will notice that it already has some writing and it may look like the following:
Private Sub Button1_Click(ByVal sender As System.Object…
This is an event handler, which means that anything inside the Sub runs every time the button is clicked. Inside this Sub you will type the following:
This code requires the program to take the text typed into the Textbox (Textbox1) to navigate to the page and produce it to the webbrowser (WebBrowser1). This is all that needs to be done to this project to make this particular application run, however there are many options to creating applications in Visual Basic. Another example, while not complex, is still a little more involved.
Learning about Visual Basic for Applications can be beneficial too. Edu CBA can help with this by providing a comprehensive guide as to how it works.
Creating a Simple Stopwatch in Visual Basic
To create a simple Stopwatch, you will create a new project as previously explained. Once the project is created and you have selected a form for the form design, you will want to create three labels, which will all have default names (Label1, Label2, and Label3). You will also need to drag the btnStart and the btnExit to the form. For each of the Labels change the text to the number 0, for the btnStart you will want to changes the text to Start/Stop, and for the btnExit, change the text to Exit. Laslty, you will create a timer, which will be called tmrTime in the toolbox. In order for this timer to count at the correct intervals, you muct change the timer interval to 1000, which signifies 1 second. The placement of this element does not matter as it will not affect the Stopwatch function. After you have finished with the design of the application, you will need to put some code in to make it function.
Learn VBA through Excel with CompuWorks Trainers. They can help you to learn different techniques to enhance your experience with using Visual Basic for Application.
Coding your Stopwatch
The first code to complete will complete the Start/Stop button and provide that when this button is pushed, the timer will start or stop. To script the code to make this work you will type the following into the Code Editor:
Private Sub btnStart_Click() If tmrTime.Enabled = False Then tmrTime.Enabled = True Else tmrTime.Enabled = False End If End Sub
The next step is to code the timer itself, which will allow the timer to continue counting upward until the Start/Stop button is pushed. To do this you will click on tmrTime and enter into the code editor:
Private Sub tmrTime_timer() Label13.Caption = Val (Label13.Caption) + Val(1) If Label13.Caption = 60 Then Label12.Caption = Val (Label12.Caption) + Val(1) Label13.Caption = 0 ElseIf Label12.Caption = 60 Then Label11.Caption = Val(Label11.Caption) + Val(1) Label12.Caption = 0 End If End Sub
The next code will make it so that when the form is loaded, the timer won’t start until the Start/Stop button is clicked. The following code must be typed into the Code Editor to make this happen:
Private Sub Form_Load() tmrTime.Enabled = False End Sub
The final step in creating a simple stopwatch in Visual Basic would be to code the Exit button. This code simply allow you to exit the application when you are finished using it. The description is as follow:
Private Sub btnExit_Click() Unload Me End Sub
While many beginners may not understand a lot of these codes, the best way to get familiar is to work with them. The user could also try taking tutorial courses, to understand the process better. Once you are familiar with the basics, the opportunities for creating applications is limitless.