190 likes | 282 Vues
Visual Basic for Applications - The Environment. What is an event-driven program? A user interface? What are Access/Accelerator Keys? NOTE: Important material on VBA is in the Course Guide at the start of this lecture slide section, so read those pages soon!.
E N D
Visual Basic for Applications - The Environment • What is an event-driven program? • A user interface? • What are Access/Accelerator Keys? • NOTE: Important material on VBA is in the Course Guide at the start of this lecture slide section, so read those pages soon!
What we learn in the next part of CS105 How can we program computers? Making decisions How to create functions and procedures CS 105 Fall 2005
Why Learn Visual Basic for Applications? • Business College wants you to learn flow charts and the kind of thinking that programmers use • Extremely powerful -- lets non-professionals to do some heavy-duty programming CS 105 Fall 2005
Why Learn Visual Basic for Applications? • Widely used to create applications for the PC and to jazz up Windows, Browsers, etc. • Excel and Access can be modified by Visual Basic for Applications (VBA is built in) • FUN to do--you can see your results quickly CS 105 Fall 2005
What is a software program? "As a rule, software systems do not work well until they have been used, and have failed repeatedly, in real applications." (Dave Parnas) CS 105 Fall 2005
Visual Basic for Applications: BASICstands for Beginner’s All-purpose Symbolic Instruction Code. • Developed in the 1960s. Microsoft developedVisualBasicin 1991. • Lets you make stand-alone programs Visual Basicfor Applications • Allows you to program in Microsoft applications, to create or improve macros, to create user interfaces, and even make your own programs. CS 105 Fall 2005
Programming Languages: Visual Basic for Applications • A program’s ability to respond to events forms the basis of event-driven programming • Responds to user-initiated events such as keystroke or click, or even opening up a workbook examples: Visual Basic for Applications, Java • Uses Objects such as command buttons, cells, pictures, charts, spreadsheets. CS 105 Fall 2005
What is an Event? • An event is any action to an object that is recognized by an application such as Excel. • Opening or closing an Excel workbook • Clicking on a command button • Changing the data in a cell • An event procedure is code that runs in response to the event. CS 105 Fall 2005
What does a procedure look like? Private Sub cmdFormat_Click() Range("F8").Value = Range(“C8").Value Range(“C8").ClearContents End Sub CS 105 Fall 2005
Calling Event Procedures You can run one procedure from another procedure. You "call" it, even if it is stored in another module/worksheet. You call a subprocedure by writing its name: Private Sub cmdRun_Click() FormatSheet End Sub CS 105 Fall 2005
Graphical User Interface • You create the GUI (graphical user interface)/ CHI (computer-human interface) You create prompts, questions to the user Key entry, Mouse Click, Menu Selection, Text or data entry You decide the program responses to user actions Including Computations, Change of Interface, etc. CS 105 Fall 2005
What happens when you use VBA • You create Input Boxes or use controls to gain input from the user. • You insert code behind spreadsheet, in the VISUAL BASIC EDITOR, to process the information. • The basic building block of a VBA program is the procedure (subroutine = sub procedure = sub) CS 105 Fall 2005
Assignment Statement • [Let] Object.property = a color or quantity, etc. • Either of these works: • Let Range("B2").Value = 33 • Range("B2").Value = 33 • The = sign means "take the value on the right side of the equal sign and assign it to a place in the computer’s memory named on the left side of the = sign." CS 105 Fall 2005
A FLAWED assignment statement 33 = Range(“C3”).Value CS 105 Fall 2005
How the computer reads a line of code • In C language, the ; character tells the computer to stop reading each separate statement • C Language -- Range(“C2”).Value = 4 + 5; • In VBA, the end of the line is considered to be the end of the statement (like a period in English) Range(“C2”).Value= 5 + 5 • In order to force the computer to consider the next line part of the first, we use (space)(underline) as seen on the next slide. CS 105 Fall 2005
Errors in continuing lines of code We use an underscore in VBA: __ BAD: Range(“C2”).Value = "Me Tarzan, you _" & Range(“A1”).Value Range(“C2”).Value = "Me Tarzan, you"_ & Range(“A1”).Value Don’t put inside the quote Leave a space! CS 105 Fall 2005
Java code—what does it look like? // create board s = new Space[grid.width][grid.height]; // download images this.showStatus("Downloading images..."); tracker = new MediaTracker(this); mine = this.getImage(this.getDocumentBase(),"images/mine.gif"); tracker.addImage(mine, 0); Note: comments start with//, lines end with; CS 105 Fall 2005
To summarize: What is • an event-driven program? • a user interface? • an assignment statement? • Your moment of Zen http://youtube.com/watch?v=nyeR7bmvjco CS 105 Fall 2005