SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - Programming Fundamentals Using VBA
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Programming Fundamentals Using VBA
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 19
    2005-07-28

    Table of Contents:
  • Programming Fundamentals Using VBA
  • Creating Procedures
  • Declaring Variables
  • Constants
  • Control Structures
  • The ElseIf Structure
  • Do Loop
  • Arrays
  • Dynamic Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Programming Fundamentals Using VBA - Constants


    (Page 4 of 9 )

    Many times you may want to declare a value that will not change. This type is called a Constant and is declared using the keyword Const in place of the normal declaration using Dim.

    As an example:

    Const conNumber1 As Integer
    Const conDate As Date = #03/02/04#

    Notice that you preface the variable’s name with con instead of the normal type. Also, when declaring a constant, you must assign it an initial value, or you will get a syntax error message when you leave the line.

    Input and Output

    You now know how to save data with the use of variables. But how do you get the information into a variable? Or read the information that is stored in them? In many ways, that will be what we spend the rest of this book examining. However, let’s begin by looking at a couple of simple techniques for testing our code.

    One of the simplest techniques for getting information from the user into a variable is to use a built-in function called InputBox. This will give you a simple dialog box with a prompt.

    To code this in our small example, you would enter the shaded lines shown here:

    Sub addNumbers()
       'Declare the variables
      Dim intNumber1 As Integer
      Dim intNumber2 As Integer
      Dim intSum As Integer
      'Create InputBoxes to enter numbers
      intNumber1 = InputBox("Enter the first number")
      intNumber2 = InputBox("Enter the second number")

    End Sub

    Notice that for clarity’s sake, I also added a comment to indicate what I am doing.

    The entry intNumber1 = InputBox is called an assignment statement, and it assigns the value the user enters to the variable. In this case two assignments have been created: one for intNumber1 and the other for intNumber2.

    You can execute the code by selecting the Run button:

    That brings up a dialog box like the one shown here for the first entry (a second one will appear for the second entry).

     

    If you followed the preceding steps, the code will run and the variables will be set. However, there is no means of outputting the results. There are two ways to create an output for now. The first way is to add the code, shown with shading, here:

    Sub addNumbers()
       'Declare the variables
       Dim intNumber1 As Integer
       Dim intNumber2 As Integer
       Dim intSum As Integer
      
    'Create InputBoxes to enter numbers
       intNumber1 = InputBox("Enter the first number") 
       intNumber2 = InputBox("Enter the second number")
       'Add numbers
       intSum=intNumber1 + intNumber 2
    ' Create an output
       Debug.Print "The numbers entered were " & intNumber1 & " and " & _
    intNumber2
    End Sub

    If you run the code now, you will once again get the two dialog boxes. Notice that the output will appear in the Immediate window, located at the bottom of the editor environment:

     

    This window is used as a temporary testing place for our code and will come in handy initially.

    There are several issues worth discussing here. First of all, the line:

    Debug.Print "The numbers entered were " & intNumber1 & " and " & _
    intNumber2

    This is called a concatenation because it brings various components together. The literal text is enclosed in quotation marks, and the variables are not. The two types are separated by the ampersand.

    Also, notice that the line is broken with a space and then an underscore. When you break up a statement onto multiple lines, VBA requires this. Throughout this book, we may need to do that for typographical reasons. However, you can keep the code on one line.

    Another way of formatting an output is to use a technique you first saw in Chapter 4. You could use a message box by replacing the output line with the following line of code:

    MsgBox "The numbers entered were " & intNumber1 & " and " & intNumber2

    This will produce output similar to the following:

     

    You now have your first VBA program running.

    More Visual Basic.NET Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from chapter six of the book Access VBA Programming, written by Charles E. Brown and Ron Petusha (McGraw-Hill/Osborne, 2004; ISBN: 0072231971). Check it out at your favorite bookstore today. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway