Objekt variable oder With-Block variable nicht festgelegt - Access 2010-VBA

Grüße an das gut des Wissens...

Ich habe beim Lesen der zahlreichen Beiträge auf diesem bestimmten Fehler und haben nichts gefunden, löst mein besonderes Problem ist.

Ich habe einige VBA-code in eine Access-2010-front-end. Manchmal, aber nicht immer, bekomme ich ein "Objekt-variable oder With-block variable nicht festgelegt." - Fehler. Mein code ist wie folgt:

Public Sub ValidateAddress(PassedAddress As Object, PassedCity As Object, PassedState As Object, _
    PassedZIP As Object, PassedCongressionalDistrict As Object, PassedValidated As Object, HomeForm As Form)

On Error GoTo ShowMeError
    Dim strUrl As String    ' Our URL which will include the authentication info
    Dim strReq As String    ' The body of the POST request
    Dim xmlHttp As New MSXML2.XMLHTTP60
    Dim xmlDoc As MSXML2.DOMDocument60
    Dim dbs As Database
    Dim candidates As MSXML2.IXMLDOMNode, candidate As MSXML2.IXMLDOMNode
    Dim components As MSXML2.IXMLDOMNode, metadata As MSXML2.IXMLDOMNode, analysis As MSXML2.IXMLDOMNode
    Dim AddressToCheck As Variant, CityToCheck As Variant, StateToCheck As Variant, ZIPToCheck As Variant
    Dim Validated As Boolean, District As Variant, MatchCode As Variant, Footnotes As Variant
    Dim candidate_count As Long, SQLCommand As String, Start, Finish

    ' This URL will execute the search request and return the resulting matches to the search in XML.
    strUrl = "https://api.smartystreets.com/street-address/?auth-id=<my_auth_id>" & _
    "&auth-token=<my_auth_token>"

    AddressToCheck = PassedAddress.Value
    CityToCheck = PassedCity.Value
    StateToCheck = PassedState.Value
    If Len(PassedZIP) = 6 Then ZIPToCheck = Left(PassedZIP.Value, 5) Else ZIPToCheck = PassedZIP.Value

    ' Body of the POST request
    strReq = "<?xml version=""1.0"" encoding=""utf-8""?>" & "<request>" & "<address>" & _
                "   <street>" & AddressToCheck & "</street>" & "   <city>" & CityToCheck & "</city>" & _
                "   <state>" & StateToCheck & "</state>" & "   <zipcode>" & ZIPToCheck & "</zipcode>" & _
                "   <candidates>5</candidates>" & "</address>" & "</request>"
    With xmlHttp
        .Open "POST", strUrl, False                     ' Prepare POST request
        .setRequestHeader "Content-Type", "text/xml"    ' Sending XML ...
        .setRequestHeader "Accept", "text/xml"          ' ... expect XML in return.
        .send strReq                                    ' Send request body
    End With

    ' The request has been saved into xmlHttp.responseText and is
    ' now ready to be parsed. Remember that fields in our XML response may
    ' change or be added to later, so make sure your method of parsing accepts that.
    ' Google and Stack Overflow are replete with helpful examples.

    Set xmlDoc = New MSXML2.DOMDocument60
    If Not xmlDoc.loadXML(xmlHttp.ResponseText) Then
        Err.Raise xmlDoc.parseError.errorCode, , xmlDoc.parseError.reason
        Exit Sub
    End If

    ' According to the schema (http://smartystreets.com/kb/liveaddress-api/parsing-the-response#xml),
    ' <candidates> is a top-level node with each <candidate> below it. Let's obtain each one.
    Set candidates = xmlDoc.documentElement

    ' First, get a count of all the search results.
    candidate_count = 0
    For Each candidate In candidates.childNodes
        candidate_count = candidate_count + 1
    Next

    Set candidates = xmlDoc.documentElement
    Select Case candidate_count
        Case 0 ' Bad address cannot be corrected.  Try again.
            Form_frmPeople.SetFocus
            MsgBox "The address supplied does not match a valid address in the USPS database.  Please correct this.", _
                vbOKOnly, "Warning"
            PassedAddress.BackColor = RGB(255, 0, 0)
            PassedCity.BackColor = RGB(255, 0, 0)
            PassedState.BackColor = RGB(255, 0, 0)
            PassedZIP.BackColor = RGB(255, 0, 0)
            Exit Sub
        Case 1 ' Only one candidate address...use it and return.
            For Each candidate In candidates.childNodes
                Set analysis = candidate.selectSingleNode("analysis")
                PassedAddress.Value = candidate.selectSingleNode("delivery_line_1").nodeTypedValue
                Set components = candidate.selectSingleNode("components")
                PassedCity.Value = components.selectSingleNode("city_name").nodeTypedValue
                PassedState.Value = components.selectSingleNode("state_abbreviation").nodeTypedValue
                PassedZIP.Value = components.selectSingleNode("zipcode").nodeTypedValue & "-" & _
                    components.selectSingleNode("plus4_code").nodeTypedValue
                Set metadata = candidate.selectSingleNode("metadata")
                PassedCongressionalDistrict.Value = CInt(metadata.selectSingleNode("congressional_district").nodeTypedValue)
                PassedValidated.Value = True
            Next
            Exit Sub
        Case Else ' Multiple candidate addresses...post them and allow the user to select.
            DoCmd.SetWarnings False
            Set dbs = CurrentDb
            If IsTableQuery("temptbl") Then dbs.Execute "DROP TABLE temptbl"

            dbs.Execute "CREATE TABLE temptbl (Selected BIT, CandidateAddress CHAR(50), CandidateCity CHAR(25), _
        CandidateState CHAR(2), CandidateZIP CHAR(10), CandidateCongressionalDistrict INTEGER, _
        MatchCode CHAR(1), Footnotes CHAR(30));"
            DoCmd.SetWarnings True

            Start = Timer
            Do While Timer < Start + 1
                DoEvents
            Loop

            For Each candidate In candidates.childNodes
                Set components = candidate.selectSingleNode("components")
                AddressToCheck = candidate.selectSingleNode("delivery_line_1").nodeTypedValue
                CityToCheck = components.selectSingleNode("city_name").nodeTypedValue
                StateToCheck = components.selectSingleNode("state_abbreviation").nodeTypedValue
                ZIPToCheck = components.selectSingleNode("zipcode").nodeTypedValue & "-" & _
                    components.selectSingleNode("plus4_code").nodeTypedValue
                Set metadata = candidate.selectSingleNode("metadata")
                District = metadata.selectSingleNode("congressional_district").nodeTypedValue
                Set analysis = candidate.selectSingleNode("analysis")
                MatchCode = analysis.selectSingleNode("dpv_match_code").nodeTypedValue
                Footnotes = analysis.selectSingleNode("dpv_footnotes").nodeTypedValue
                DoCmd.SetWarnings False
                dbs.Execute "INSERT INTO temptbl ( CandidateAddress, CandidateCity, CandidateState, CandidateZIP, _
                CandidateCongressionalDistrict, MatchCode, Footnotes ) " & vbCrLf & "SELECT """ & AddressToCheck & _
                    """ AS Expr1, """ & CityToCheck & """ AS Expr2, """ & StateToCheck & """ AS Expr3, """ & _
                    ZIPToCheck & """ AS Expr4, " & District & " AS Expr5, """ & MatchCode & """ AS Expr6, """ & _
                    Footnotes & """ AS Expr7;"
                DoCmd.SetWarnings True
            Next

            DoCmd.OpenForm "frmPeopleAddressMaintenance"

            Do Until CurrentProject.AllForms("frmPeopleAddressMaintenance").IsLoaded = False
                DoEvents
            Loop

            HomeForm.SetFocus
            If IsTableQuery("temptbl") Then dbs.Execute "DROP TABLE temptbl"
    End Select
    dbs.Close
    Exit Sub

ShowMeError:
    MsgBox Err.Description, vbOKOnly, "ERROR!"
End Sub

Der Fehler tritt in zwei bestimmten Orten:

Unter "Fall 1": Der Fehler tritt sofort nach...

PassedCongressionalDistrict.Value = CInt(metadata.selectSingleNode("congressional_district").nodeTypedValue)

...ausgeführt wird. Ich habe gedebuggt dies und bestätigt, dass die Anweisung korrekt ausgeführt und der Wert von "PassedCongressionalDistrict" Objekt die richtige ist.

Dann unter "Case Else": Die For-Schleife verarbeitet das erste Element der Liste richtig, aber nicht mit den identifizierten Fehler, wenn zu Beginn der Verarbeitung der zweiten Position, obwohl es gute und legitime Daten in die zweite Position.

Ich hoffe ich habe gut genug erklärt. Ich kann einfach nicht scheinen, um herauszufinden, (1) wie, um mehr vollständig Debuggen und (2) warum tritt der Fehler auf, als es scheint, dass ich alle meine Objekt-Variablen richtig definiert.

Grüße,
Ken

  • In der Vergangenheit habe ich Projekte Fehler auf mich bei der Migration von 2007 auf 2010. 2010-Motor ist irgendwie "schneller" und so gibt es Situationen, in denen mein code scheint zu versuchen, eine Referenz zu einem Objekt, bevor es zugewiesen wurde oder instanziiert, und der Fehler zeigt nur manchmal, und wenn ich Debuggen Schritt für Schritt immer funktioniert, und es funktioniert nicht-Fehler in 2007. Also meine Unordnung fix für diese Situationen ist das hinzufügen einer timer-Verzögerung für eine zweite ( oder so lange wie erforderlich), bevor Sie die problematische Zeile. keine elegante Lösung, aber es hat für mich gearbeitet auf mehreren Gelegenheiten.
  • Habe ich auch gedacht. Ich bin eigentlich Los, um zu versuchen, dass jetzt, Sie haben es erwähnt.
  • Vielleicht den Zugang lieber aus der Politik.
  • Ich habe gerade die Freiheit, Sie verstellen den Blick auf die auth-id und auth-token in den Beispiel-code (aber der edit muss sein peer-genehmigt werden, bevor Sie wirksam werden). Diese Werte sind privat und sollten nie veröffentlicht! Ich empfehle dringend, das löschen, die Taste pair auf Ihr Konto und erstellen Sie eine neue. (smartystreets.com/account/keys)
  • HUCH! Ich habe regeneriert einen neuen auth-id und auth-token. Danke für den catch.
InformationsquelleAutor KACJR | 2013-08-30
Schreibe einen Kommentar