There is a lot of differnet data types that can be used in VB. Lets start with Intergers, they can hold whole numbers. Longs which also hold whole numbers but they can have very large numbers. Singles can have decimals, and doubles can hold very large numbers with decimals. String variables can hold text. The last type is Variant, it can hold anything. You may be asking yourself why do i use Integers when I can just use Variants to hold my data? Memory size, thats why. Integers use 2 bits while variants use up to 16! When you write a large program this more meaningful then in the little projects we will make.
Lets put some of our data types to use. Start a new project in your compiler. Add two text boxs named "txtInput" and "txtOutput". Now add a command button named "cmdButton" with a caption of "&Change". Notice the & sign in the caption. This is an access key. If a user hits Alt + C the Buttons event will be processed. keep in mind no access keys can be the same letter on one form (will cover this more in Tutorial 3.) Back to declaring variables, open the command buttons code (Doubld click the "&Change" button you just made). Lets make some variables under "Private Sub cmdButton_Click()", type Dim strInput as String and press return. remember how text boxs get a txt prefix, variables are the same way. Strings get str, Integers get int, Longs get lng, and so on. Lets make another one, type Dim strOutput as string. What we just did was made two "Local" variables. They are local because once the Private Sub cmdButton_Click() event is over they are deleted. This is the Scope of the variable.
Local variables are in a sub event, and are delete after the event is done. So what if you what the data in a variable saved after the event was done? We can make it "Form Level", what you have to do is at the top of your code in the "General Declarations" type your Dim statment. This will save the data in that variable until you close that form. What if we had two forms and need to pass some user input to the other form? We can add a code "Module" and write a statment like the dim, but we'll say: "Public Our_Variable as Type" Any information put in to a Public variable can be used in any form and stays there until the program ends. (Don't worry if you don't total understand this part, we'll explain it more in an example.)
Now that you know what variables are and the scope of them, lets put it to use. We have already Dimed two string variables in our project, but what are going to do with them? We can use them to change what is in our text boxs. Lets move the data in txtinput to txtoutput. Under your Dim statments type strinput = txtinput.text and press return. We have just put the data in txtinput into the variable strinput. Now lets that data into txtoutput. Type txtoutput.text = strinput Notice how the information on the left of the = sign is where we want the data on the right to goto. Run the program and enter something in to txtinput then click your command button. It copied it to txtoutput! Now lets make the program switch both boxs at once. Make your code look like:
Private Sub cmdButton_Click()
Now run the program and enter some text in both text boxs and click your command button. They changed places, or swpped. Swapping is used a lot in code. WHen we get in to If statments you'll see what i mean.
Now lets make another form in our project to show the results of the swap. To add a form right click in the project explorer and goto add and choose form then when the dialog box opens pick form. Now you have a second form with no controls on it. Add two text boxs name them "txtInput" and "txtOutput" just like you did on the first form. Since we are going to move data from form to form we will need a code module. Just like how you added the form add a module. Right click in the project explorer and click add and then pick module. Cut your two dim statments from your form and paste them in the module. And change the dims to public. Now we need to write the code to show the second form and to show the data in our labels. The first thing we can do is name our second form lets call it "frm2". Now lets say when you click the button frm2 will pop-up type frm2.show in the Private Sub cmdButton_Click() on form 1 (After all your other code). Now form 2 will pop-up but the data still isn't there! Cut and paste the txtInput.Text = strOutput and txtOutput.Text = strInput to the second form in the Form Load event. The way to get to this is just simply doulbe click the second form. Now run the program! Wow, isn't that cool?
Understanding how variables are used is very important. We will be using variables all the time from here on out. We will not be showing you how to dim them anymore we will just say "create a Variable_type named Variable_name" and you will have to type Dim Variable_name as Variable_type. If you don't understand what I'm talking about please read this tutorial again or E-Mail Me. Other than that I think you are ready to move on to Tutorial 3: Appearance