Go To Statement

The Go To statement is a control structure that directs flow to a label. It does not return a value. The value of the label argument uses the same format as any other Visual Basic name. A reserved word is not a valid label. You cannot use a Go To statement to transfer control out of the current function or subroutine. It is recommended that you avoid using a Go To statement. For more information, see the following.

Format

GoTo label

The following table describes the arguments that you can use with this method.

Argument Description

label

A name that begins in the first column of a line of code and ends in a colon (:).

Example 1

The following example displays the date for one week from the date that the user enters. If the date is not valid, then the Go To statement directs flow to the beginning of the start statement:

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

It is recommended that you avoid using a Go To statement. When possible, you must use another technique. For example, Go To Statement could use an If statement that occurs in a separate function that the main code calls. If the test fails, then Siebel VB can call the initial code again. For example:

(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