Coding Basics

OK, so far you have learned about forms, command buttons, labels, text boxes, and finally menus. By themselves they can't do anything, that's where "code" comes in. If you add a command button to a form, and double-click on it, you will see the code window, yup you guessed it -that's where the codes go...

coding basics preview

Data Types come in 3 basic types; string (if you are dealing with text data, use strings), numeric (if you are dealing with numbers you will need one of the many numeric data that VB supports) and special (if you are using something other then those two, special data is what you will be using).

Data in detail

Boolean A data type that has only two values, either true or false
Byte Is a positive number without decimals that goes from 0-255
Currency From -$922,337,203,685,477.5808 to $922,337,203,685,477.5808
Date From Jan 1,100 to Dec 31, 9999
Double Numeric values that range from -1.7976313486232E+308 to 1.7976313486232E+308
Integer Numeric values from -32,768 to 32,768
Long They're bigger then integers, ranging from -2,147,483648 to 2,147,483648
Object Special data
Single Numeric value that goes from -3.4 to 3.4
String A data that has from 0 to 65,400 characters of numbers, letters and symbols
Variant Any type of data

Those are the data types you will be using in VB. I know it's confusing, but it gets easier along the way.

Tip: Null Strings are strings that don't have anything in it, in other words they're empty.

Variables are things that can change, (the opposite of a literal) meaning things that can change like what's in a text box. Variables need to be declared, to declare them we use the "Dim" statement. For example, in the VB's code window write Dim VarName as String. VarName is a name that you supply, an example;

We have a form, a text box, and a command button, and a label

Private Sub Command1_Click()

    REM I have clicked on the cmdbtn on the form
    REM now I'm in its code window (you can see it says command1_click)

    Dim VarName As String 'declaring variable as a string

    VarName = Text1.Text 'VarName is = to whatever textbox's text is
    Label1.Caption = VarName

    'now when the user clicks the button
    'the label's caption which is blank
    'switches to whatever VarName, or whatever the text box has in it
    'simple

End Sub

Note: You can use the statements ' or REM to write yourself notes in codes, as you can see it comes in very handy.

VB operators

+ Addition
- Subtraction
* Multiplication
/ Division
^ Raises to a power
& Joins two strings (such as Str1 & Str2)
> Greater than
< Less than
= Equal to
=> Greater than or equal to
<= Less than or equal to
<> Not equal to

There you have it, you have just learned the data types that VB supports, how to declare & use variables, and also some of VB's operators.


Menus                                   Coding examples