Siebel VB Language Reference > VB Language Reference >
GoTo Statement
This standard VB method transfers program control to a specified label. Syntax
GoTo label
|
|
label |
A name beginning in the first column of a line of code and ending in a colon (:) |
Returns
Not applicable Usage
A label has the same format as any other Basic name. Reserved words are not valid labels. GoTo cannot be used to transfer control out of the current Function or Subprogram. Example
This example displays the date for one week from the date entered by the user. If the date is invalid, the GoTo statement sends program execution back to the beginning. Sub Button_Click Dim str1 as String Dim nextweek Dim msgtext start: str1 = "5/20/2001" answer = IsDate(str1) If answer = -1 then str1 = CVDate(str1) nextweek = DateValue(str1) + 7 msgtext = "One week from the date entered is " msgtext = msgtext & Format(nextweek,"dddddd") Else GoTo start End If End Sub
NOTE: Good programming practice is to avoid the use of GoTo statements. When possible, other constructs should be used to accomplish the same end. For example, the previous example could be reworked so that the If statement appears in a separate function called by the main program. If the test failed, the initial routine could be called again. The following example demonstrates this alternative.
(general) (declarations) Option Explicit ' Variables must be declared in this section so that they ' can be used by both procedures. Dim str1 As String, nextweek, MsgText As String Declare Function CheckResponse(Answer) As String
Function CheckResponse(Answer) As String str1 = CVDate(str1) nextweek = DateValue(str1) + 7 CheckResponse = "One week from the date entered is " & _ Format(nextweek, "dddddd") End Function
Sub Button1_Click Dim Answer as String str1 = "2/5/2001" Answer = IsDate(str1) If Answer <> -1 Then 'Invalid date or format. Try again. Button1_Click Else Answer = CheckResponse(Answer) End If End Sub
Related Topics
Do...Loop Statement If...Then...Else Statement Select Case Statement While...Wend Statement
|