90 likes | 217 Views
Do Loop and others. Week 4 Advanced GIS 2008 Spring. Do Loop. Do...Loop- runs forever Do...Loop While - run before checking a condition is being met or not. Do While ….Loop – run while a condition is being met. Do...Loop Until - run until a condition is met Create a new project.
E N D
Do Loop and others Week 4 Advanced GIS 2008 Spring
Do Loop • Do...Loop- runs forever • Do...Loop While - run before checking a condition is being met or not. • Do While ….Loop – run while a condition is being met. • Do...Loop Until - run until a condition is met • Create a new project
Project w/o form • Open a new project and go to Project menu and select “Remove Form1” (if you have an existing form) • Now, we need a place to write your code on. Select “Add Module” from some menu. (from project | This document) • Once code window appears, create a Main subprocedure by typing in • Sub Main() • MsgBox "Hello this is no Form", vbExclamation • End Sub • Now, replace this code with codes from the following page
Do ------ Loop While Sub DoLoop() Dim intRetries As Integer Dim strPassword As String intRetries = 3 Do strPassword = InputBox("Enter the password and click OK") intRetries = intRetries – 1 ‘or you may use intRetries -= 1 Loop While strPassword <> "letmein" And intRetries > 0 If strPassword <> "letmein" Then MsgBox "You got the password wrong - Access Denied" Else MsgBox "Welcome to the system - password accepted" End If End Sub
Do While Loop Dim intCounter as Integer Dim intAnswer as Integer intCounter = 0 intAnswer = vbYes Do While (intAnswer = vbYes) intAnswer = MsgBox (“Conitnue?”, vbYesNo) If (intAnswer = vbYes) Then intCounter = intCounter + 1 End If Loop MsgBox (“Number of Continues = “ & intCounter
Do ---Loop Until • Try to use Do ...Loop Untilto do the same thing (until the right password is entered
Exercise • Create form with three buttons, cmdDoLoopWhile, cmdDoWhileLoop and cmdDoLoopUntil • Use codes from previous slide to execute three different loops.
Open File and Do Until Loop Dim strName as String Open “c:\4850\test.txt” For Input As #1 Do Until EOF (1) Input #1, strName cboName.AddItem strName Loop Close #1 Input, Output or Append End of File Now, create a file with several State names typed and save it as test.txt to your folder. Modify the filepath to point to your own file location create a new form with cboName and cmdAddFile. In click() sub, type in the above code and see what happens.
Compile or run-time errors • Syntax errors (Compile Errors) • For …. (but no Next) • Show (but no Form and dot) • VBA highlight offending procedure in yellow and the error in blue. • Run-time error • Impossible to run • Acres = 40000/0 • Acres = “SquaresFeet” / 43560