+ Reply to Thread
Results 1 to 20 of 20

XML VBA Error Catching

Hybrid 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

  2. #2
    Registered User
    Join Date
    06-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Home and Business 2010
    Posts
    84

    Re: XML VBA Error Catching

    Couldn't you add the lines at the end of the macro
    On Error Resume Next
    MsgBox "You have input incorrect information"
    So it would notify the operator they input incorrect information.

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

    Re: XML VBA Error Catching

    Well I am looking to avoid having them figure out what is wrong. If they receive a message as soon as they add/edit any information they will know where the error is. If they just receive an error msg when the information gets refreshed after a week they wont remember what changes where made.

    OR

    Did you mean add those two lines at the end of those two codes I posted?

  4. #4
    Registered User
    Join Date
    06-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Home and Business 2010
    Posts
    84

    Re: XML VBA Error Catching

    If you add it before the End Sub line when the macro encounters an error it will show the message box then the macro will end.

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

    Re: XML VBA Error Catching

    There are many subs to the macro. Which subs? The ones I posted?

  6. #6
    Registered User
    Join Date
    06-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Home and Business 2010
    Posts
    84

    Re: XML VBA Error Catching

    The two that you posted.

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

    Re: XML VBA Error Catching

    I added them to the code but even if I input correct information the msgbox appears.

  8. #8
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: XML VBA Error Catching

    If you want to catch the errors as they happen, i think you will need to collect them differently. It looks like your user is filling in a form, and then all of the fields values collected are validated with a series of IF statements.



    Honestly, this would appear to be a case where you could tie those values to specific cells in a worksheet - and just use the worksheet in place of a form. The advantage there is, worksheet cells can be validated to immediately tell someone what is wrong when they enter something.

    Using worksheet data validation would eliminate the need for VBA error checking - cells can be validated to only accept numbers. Does that make sense?
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

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

    Re: XML VBA Error Catching

    I need to have a userform in place, it's kind of a requirement for this task.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: XML VBA Error Catching

    You appear to already have code to check for valid input before trying to get the data from the website, is it not working as expected?
    If posting code please use code tags, see here.

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

    Re: XML VBA Error Catching

    Well that is just for making sure that the input data is in the correct format. But if the person inputs a longitude/latitude that does not exist then you would get an error from the API.

    So I would guess that the best way for the user to be informed that there is an error is exactly when the person is trying to add/edit a site. That way they will know what is wrong and correct it. I would figure that the way to do that is that in the edit and add tabs the code can send a request to the API and if the API returns an error tell the user that whatever they are trying to add or edit is incorrect, to try again.
    Last edited by Inti; 07-10-2014 at 01:59 PM.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: XML VBA Error Catching

    What's actually returned by the site when that happens?

  13. #13
    Registered User
    Join Date
    06-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Home and Business 2010
    Posts
    84

    Re: XML VBA Error Catching

    Try adding a statement saying if the text in the userform is an error then message box
    If IsError(TextBoxAddLat) Then
    Msgbox "Error your site name is incorrect
    You would have to do this for all of your lines however.

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

    Re: XML VBA Error Catching

    I wrote gibberish on the fields to force an error. Here's the link

    http://api.yr.no/weatherapi/location...6713276231.332

    An error occurred!

    The following error messages were generated:
    [Thu Jul 10 18:00:53 2014] [error] [locationforecastlts] Invalid parameter 'lat': Erroneous value
    Specified parameters:




    version=1.2

    action=run

    product=locationforecastlts


    See also...
    •LocationforecastLTS product documentation
    •Top-level WeatherAPI documentation

    If you believe this to be a bug in the application, contact the developers. Thanks!

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: XML VBA Error Catching

    When you run the code with invalid latitude and longitude is that what's returned in the responseText of the request?

    If it is I would suggest that you check the responseText before you load it into the XLM/DOM document Resp.

    Something like this perhaps.
        For Each Site In SiteList
    
            req.Open "GET", Site.Value, False
            req.send
    
            ctrr = ctrr + 1
            siteNames = Worksheets("Site List").Cells(ctrr, 2).Value
    
            If Instr(req.ResponseText, "An error occurred!") Then
                ' code to do something if there's been a problem
            Else
                Resp.LoadXML req.responseText
                ' continue with code to get data
           End If
     
        Next Site

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

    Re: XML VBA Error Catching

    That is what appears on the website, when you access the URL through a browser.

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

    Re: XML VBA Error Catching

    Is there something I can add there to pinpoint which site created the error?

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: XML VBA Error Catching

    When you send out an XML request what's returned is what you would see if you entered the URL manually.

    So when you put in a invalid latitude/longitude the code should return the error message in the response text of the XML request.



    To pinpoint the sites just refer to the current site you are on in the loop throught the site list
    For Each Site In SiteList
      ' Site is the current site in SiteList

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: XML VBA Error Catching

    Forgot to ask, did you try the code I suggested?

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

    Re: XML VBA Error Catching

    Thank you once again Norie! Great solution. It's working perfectly.

+ Reply to Thread

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