Course visual basic : introduction / PDF

Tutorial visual basic  : introduction / PDF










Tutorial visual basic  : introduction / PDF














Introduction

The purpose of this tutorial is to provide you with the basic tools needed to write very simple programs for numerical analysis in Visual Basic (VB). They were developed for version of VB that comes with MS Office 2003. This version is largely updated to Office 2010, but there may still be some things that do not quite work correctly.

VB is extremely powerful and can create nice user interfaces and do lots of fancy formatting.

There are a number of references that can help you learn these tools. In particular, I find the book by Albright to be quite useful. Fortunately, for the work we’ll be doing in this class, the vast majority of these tools will be unnecessary and this tutorial covers everything that you need to do basic programming to solve dynamic programming problems. On the other hand, VB is quite slow – for small programs this is not an impediment. But if you’re interested in solving large problems, use the programming skills you learn here and then learn a more efficient language such as Matlab or Fortran.

As you work through this sheet, make sure you understand what you’re doing. You’ll need to follow these or similar steps many times in the future. If you understand instead of just repeating, you’ll be much happier in the long run.

There is a quiz at the end of these notes will test to see if you have learned the basics of programming in VB. All students using VB for PS#3 must turn in the quiz before you turn in problem 2. You may choose to look at the quiz first – if you can solve it, you probably have all the knowledge you need for the programming assignment.

II. Overview

VB has much in common with virtually all other programming languages. A VB program is a series of commands that creates and manipulates variables. VB programs are also called Macros. Several different programs (called Subs in VB) can be in a single file. These Subs can act separately or they can be interconnected. With few exceptions, all your commands must be contained in a Sub, i.e., after a line that opens a Sub and before the End Sub that ends the sub.

Unlike the command-prompt version of Matlab, a VB program does not run until you tell it to. Further, and very importantly, VB does not give you any output unless you explicitly tell it to put the output into an Excel spreadsheet.

III. A word of warning!!!

1. Save your work every 5-10 minutes. There is usually no autosave working in Excel and even if it is, don’t trust it. VB programs frequently crash; unless you’ve saved your file, you can easily lose hours of work.

IV. First step - your first program for writing output

2. Open Excel, then go to the Developer menu, click on Macro Security. On the Macro Settings tab, select Disable all macros except digitally signed macros. This seems to work. If your computer is not recognizing your macros, you may need to switch to the last option, Enable all macros, save and close the file, then re-open it. If the Developer tab is not visible, you will need to activate. See online help for step-by-step
instructions.

3. Start with a blank Excel worksheet and save it (e.g., something like “VBintro.xls”).

4. Load the VB editor (alt-F11).

5. From the Tools menu in VB, choose the Options and on the Editor page, select “Require Variable Declaration.” (Tools, Option 2nd box on the editor page). Click Ok. This means that every time you use a new variable, you need to explicitly introduce that variable with a Dim command, just like in Matlab, where a syms command must be used before using a variable can be used on the right-hand-side of an expression.

6. Make sure that you are Viewing the Project Explorer and make sure that your current project is highlighted in the project explorer. If the Project Explorer is not visible, press Ctrl-R to view it......









Download Tutorial visual basic  : introduction / PDF














Tutorial visual basic  : introduction / PDF

1 commentaire: