Can I call a sub routine and not continue onto the sub routine completes?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Is it possible to call a sub routine and not continue onto the next line of code until that called sub routine completes?

Please show me some coding that will do that.

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I also thought that was the case, but at least on my Android device it seems the order of flows is a bit different. Please look at the code that is included here. Maybe you can show me what I did wrong.

The Toast message indicating data is saved to the database is shown before the Toast message for the Geocoding. The Geocoding code is placed before the code that saves the data to the database.

B4X:
Sub ButtonSaveEventHandler_Click

   Dim dataSavedOk As Boolean

   TabHostPeople.CurrentTab = 0 ' Go to tab page 1.
      
   ' Perform validation first.
   '--------------------------
   If EditTextFirstName.Text = "" Then
      
      ToastMessageShow("Please enter the first name.", True)
      
      EditTextFirstName.RequestFocus
   Else
      ' Set up variable used prior to a Geocoding attempt.
      '---------------------------------------------------
      blnAddressGeocodedOk = False
      blnParserCalledForThisStreet = False
      strStreetNameToGeocode = EditTextAddressStreetName.Text
      strStreetNameFromTheInternet = ""
   
      ' Try to Geocode the address.
      '----------------------------
      ReverseGeocodeAddress (EditTextAddressStreetName.Text, _
                        EditTextCity.Text, _
                        EditTextState.Text, _
                        EditTextZip.Text)
                        
      ' If the Geocoding is successful, use the street name found from Geocoding.
      '--------------------------------------------------------------------------
      If strStreetNameFromTheInternet <> "" Then
         EditTextAddressStreetName.Text = strStreetNameFromTheInternet

         ToastMessageShow(strStreetNameFromTheInternet,True)
      End If
      
      ' Insert or Edit the data into the database.
      '-------------------------------------------
      If tableMode = "New" Then

         ' It's in New mode so insert the data.
         '-------------------------------------

      
         ' Put in a default date if the user missed it.
         '---------------------------------------------
         If EditTextDateOfLastVisit.Text = "" Then
            EditTextDateOfLastVisit.Text = DateTime.Date(DateTime.Now)
         End If
         
         SQL.ExecNonQuery2("INSERT INTO PeopleToVisit " & _ 
                       "(Id, FirstName, LastName, AddressHouseNumber, AddressStreetName, City, State, Zip, DateOfLastVisit, " & _
                       "PrimaryPhone, SecondaryPhone, Email, LastVisitNote) " & _
                       "VALUES " & _
                       "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", _
                      Array As Object(Null, _
                                   EditTextFirstName.Text, _
                                  EditTextLastName.Text, _
                                      EditTextAddressHouseNumber.Text, _
                                     EditTextAddressStreetName.Text, _
                                     EditTextCity.Text, _
                                     EditTextState.Text, _
                                     EditTextZip.Text, _
                                     EditTextDateOfLastVisit.Text, _
                                     EditTextPrimaryPhone.Text, _
                                     EditTextSecondaryPhone.Text, _
                                     EditTextEmail.Text, _
                                  EditTextLastVisitNote.Text _
                                  ))

         ToastMessageShow(EditTextFirstName.Text & " has been ADDED to the database.", True)

         ClearDetails                
      Else 
      
         ' It's in Edit mode so just update the data.
         '-------------------------------------------
         SQL.ExecNonQuery2("UPDATE PeopleToVisit " & _ 
                          "SET FirstName = ?, " & _
                              "LastName = ?, " & _
                              "AddressHouseNumber = ?, " & _
                              "AddressStreetName = ?, " & _
                              "City = ?, " & _
                              "State = ?, " & _
                              "Zip = ?, " & _
                              "DateOfLastVisit = ?, " & _
                             "PrimaryPhone = ?, " & _
                             "SecondaryPhone = ?, " & _
                             "Email = ?, " & _
                             "LastVisitNote = ? " & _
                        "WHERE Id = ?", Array As Object(EditTextFirstName.Text, _
                                                   EditTextLastName.Text, _
                                                   EditTextAddressHouseNumber.Text, _
                                                   EditTextAddressStreetName.Text, _
                                                   EditTextCity.Text, _
                                                   EditTextState.Text, _
                                                   EditTextZip.Text, _
                                                   EditTextDateOfLastVisit.Text, _
                                                    EditTextPrimaryPhone.Text, _
                                                    EditTextSecondaryPhone.Text, _
                                                    EditTextEmail.Text, _
                                                EditTextLastVisitNote.Text, _
                                                   intCurrentId))
                        
         ToastMessageShow(EditTextFirstName.Text & " has been MODIFIED in the database.", True)
         
         Activity.Title = "Maintenance - People To Visit *** EDIT *** " & _
            EditTextLastName.Text.Trim & ", " & EditTextFirstName.Text 
      End If
   End If
   
   PopulateTheListView
End Sub

Sub ReverseGeocodeAddress (AddressStreetName As String, _
                     City As String, _
                     State As String, _
                     Zip As String)

    strUrlToCall = "http://where.yahooapis.com/geocode?q=" & _
      AddressStreetName & " " & _
      City & " " & _
      State & " " & _
      Zip & " "

   strUrlToCall = strUrlToCall.Replace(" ", "%20")

   HttpUtils.CallbackActivity = "People" 'Current activity name.
   HttpUtils.CallbackJobDoneSub = "JobDone" 
    HttpUtils.Download("ReverseGeocode", strUrlToCall)
End Sub

Sub JobDone (Job As String)
    
   Dim strStringFromWebSite As String
     Dim in As InputStream

   ' Proceed when the internet site with the XML was found.
   '-------------------------------------------------------
    If HttpUtils.IsSuccess(strUrlToCall) Then
     
      ' Parse the input stream from the web site.
      '------------------------------------------
      in = HttpUtils.GetInputStream(strUrlToCall)
      XmlParser.Parse(in, "XmlParser")
      in.Close
    Else
        ToastMessageShow("PLEASE MAKE SURE YOU HAVE AN INTERNET CONNECTION.", True)
    End If
End Sub

Sub XmlParser_EndElement (Uri As String, Name As String, Text As StringBuilder)

   ' Grab the latitude and longitude from the XML.
   '----------------------------------------------
   If Name = "latitude" Then
   
      fltLatitude = Text.ToString
   
   Else If Name = "longitude" Then
      
      fltLongitude = Text.ToString
   
   Else If Name = "street" Then
      
      ' Only allow 1 street per address to be parsed.
      '----------------------------------------------
      If blnParserCalledForThisStreet = False Then
         blnParserCalledForThisStreet = True
      
         If Text.ToString <> "" Then
      
            fltGeocode = fltLatitude + fltLongitude
            strStreetNameFromTheInternet = Text.ToString

            ToastMessageShow("This address has been GEOCODED as " & fltGeocode, True)
         
            blnAddressGeocodedOk = True ' Confirms address was found. 
         Else
            Msgbox("Sorry, I can't geocode this address. " & CRLF & CRLF & _
                "Please correct the address.", _
               "GEOCODING ERROR")

            blnAddressGeocodedOk = False ' Exact match not found. 
         End If
      End If

   Else If Name = "Error" Then
      
      If Text.ToString <> "0" Then
         ToastMessageShow("CAN'T GEOCODE. PLEASE CORRECT ADDRESS.", True)
      End If
   End If
End Sub

Thanks.
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You have to do this to exit that subroutine, otherwise the execution continues (see line in BOLD):

B4X:
   ' Perform validation first.
   '--------------------------
   If EditTextFirstName.Text = "" Then
      
      ToastMessageShow("Please enter the first name.", True)
      
      EditTextFirstName.RequestFocus

                [b]Return True[/b] '<-- This will exit this routine
   Else
      ' Set up variable used prior to a Geocoding attempt.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi NJDude,

The validation coding performs ok. My problem is after the Else statement which processes the Geocoding and then the saves of the data to the database.

I was hoping the Geocoding would execute first and only when it completes then the coding to save the data to the database would execute.

Can you show me where I went wrong in that part of the coding?

Thanks.
 
Last edited:
Upvote 0
Top