1 / 9

Do Loop and others

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.

diana-cobb
Download Presentation

Do Loop and others

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Do Loop and others Week 4 Advanced GIS 2008 Spring

  2. 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

  3. 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

  4. 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

  5. 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

  6. Do ---Loop Until • Try to use Do ...Loop Untilto do the same thing (until the right password is entered

  7. Exercise • Create form with three buttons, cmdDoLoopWhile, cmdDoWhileLoop and cmdDoLoopUntil • Use codes from previous slide to execute three different loops.

  8. 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.

  9. 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

More Related