Results 1 to 20 of 20

XML VBA Error Catching

Threaded View

  1. #1
    Registered User
    Join Date
    06-18-2014
    Location
    Houston, TX
    MS-Off Ver
    2010
    Posts
    63

    XML VBA Error Catching

    This code gets the data from the website

    http://api.yr.no/weatherapi/location...5;lon=8.555967

    The information obtained from this website is then organized in a spreadsheet. I am trying to avoid crashing the program. I've noticed that if an incorrect latitude/longitude is input then the website returns an error. What would need to be added in these codes to have the code check if an error is produced and then ask the user to check their input BEFORE adding the input into the "Site List" worksheet.

    Private Sub CommandButtonEditUpdate_Click()
    
        Dim Msg As String
    
        If ComboBoxEditSiteName.ListIndex < 0 Then
            MsgBox "You must select a site to edit"
    
        Else
    
            If TextBoxEditSiteName = "" Then
                Msg = Msg & "You must include a site name" & vbCrLf
            End If
    
            If TextBoxEditLat = "" Then
                Msg = Msg & "You must include a latitude" & vbCrLf
            ElseIf Not IsNumeric(TextBoxEditLat) Then
                Msg = Msg & "Latitude must be numeric" & vbCrLf
            End If
    
            If TextBoxEditLong = "" Then
                Msg = Msg & "You must include a longitude" & vbCrLf
            ElseIf Not IsNumeric(TextBoxEditLong) Then
                Msg = Msg & "Longitude must be numeric" & vbCrLf
            End If
    
            If Msg <> "" Then
                MsgBox Msg
    
            Else
                ' locate site record
                ' Write site name to cell
                ' Build URL from lat and long and write to cell
    
                ' clear form and leave form showing
    
    
                Dim selRow As Long
                Dim lat1 As String
                Dim lon1 As String
                Dim sitelink1 As String
                Dim sitename1 As String
    
                'Assign variables to construct URL
                selRow = ComboBoxEditSiteName.ListIndex + 1
                lat1 = Format(cpUserForm.TextBoxEditLat.Value, "0.0000000000000000")
                lon1 = Format(cpUserForm.TextBoxEditLong.Value, "0.0000000000000000")
    
                sitelink1 = "http://api.yr.no/weatherapi/locationforecastlts/1.2/?lat=" & lat1 & ";lon=" & lon1
                sitename1 = cpUserForm.TextBoxEditSiteName.Value
    
                'Write values to spreadsheet cells.
                Worksheets("Site List").Cells(selRow, 1) = sitelink1
                Worksheets("Site List").Cells(selRow, 2) = sitename1
                Worksheets("Site List").Cells(selRow, 3) = lat1
                Worksheets("Site List").Cells(selRow, 4) = lon1
    
    
                AddClear
                LoadComboBoxEditSiteName
                LoadListBoxDeletePickSiteList
                EditClear
            End If
        End If
    End Sub
    Private Sub CommandButtonAddUpdate_Click()
    
    
        Dim Msg As String
    
        If TextBoxAddSiteName = "" Then
            Msg = Msg & "You must include a site name" & vbCrLf
        End If
    
        If TextBoxAddLat = "" Then
            Msg = Msg & "You must include a latitude" & vbCrLf
        ElseIf Not IsNumeric(TextBoxAddLat) Then
            Msg = Msg & "Latitude must be numeric" & vbCrLf
        End If
    
        If TextBoxAddLong = "" Then
            Msg = Msg & "You must include a longitude" & vbCrLf
        ElseIf Not IsNumeric(TextBoxAddLong) Then
            Msg = Msg & "Longitude must be numeric" & vbCrLf
        End If
    
        If Msg <> "" Then
            MsgBox Msg
        Else
            ' Create a new site record
            ' Write site name to cell
            ' Build URL from lat and long and write to cell
    
            ' clear form and leave form showing
    
            Dim emptyRow As Long
            Dim lat As String
            Dim lon As String
            Dim siteLink As String
            Dim siteName As String
    
            'Assign variables to construct URL
            emptyRow = Sheets("Site List").Cells(Rows.Count, 2).End(xlUp).Offset(1).Row
            lat = Format(cpUserForm.TextBoxAddLat.Value, "0.0000000000000000")
            lon = Format(cpUserForm.TextBoxAddLong.Value, "0.0000000000000000")
            siteLink = "http://api.yr.no/weatherapi/locationforecastlts/1.2/?lat=" & lat & ";lon=" & lon
            siteName = cpUserForm.TextBoxAddSiteName.Value
    
            'Add values to site list worksheet
            Worksheets("Site List").Cells(emptyRow, 1) = siteLink
            Worksheets("Site List").Cells(emptyRow, 2) = siteName
            Worksheets("Site List").Cells(emptyRow, 3) = lat
            Worksheets("Site List").Cells(emptyRow, 4) = lon
    
            'Refresh lists
            AddClear
            LoadComboBoxEditSiteName
            LoadListBoxDeletePickSiteList
    
        End If
    
    End Sub
    Attached Files Attached Files
    Last edited by Inti; 07-10-2014 at 12:32 PM. Reason: Bad post formating

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Error catching and repeat macro
    By DaveMurray23 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-16-2014, 09:35 AM
  2. Catching error on Application.GetOpenFilename embed in Open For Input
    By Clevis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-27-2013, 02:04 PM
  3. Proper error catching runtime error 1004
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2010, 01:07 AM
  4. Catching an error
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-04-2006, 05:15 AM
  5. [SOLVED] globally catching any error event?
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1