Results 1 to 6 of 6

Thread: Returning an error message in a Com Object

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Boston
    Posts
    26

    Angry Returning an error message in a Com Object

    Hi All,

    I created a DLL that has one method connectionxml that parses a XML document and returns a connection string. Using this in multiple applications therefore I decided to create an object. Here's the code:

    These are global

    Dim XMLFileName As String
    Dim objXML As New DOMDocument
    Dim objXMLDOMNode As IXMLDOMNode

    Function ConnectionXML()
    Dim XMLFileName As String, XMLConnection As String

    If Right$(App.Path, 1) = "\" Then
    XMLFileName = App.Path & DBConnectionName
    Else
    XMLFileName = App.Path & "\" & DBConnectionName
    End If

    Set objXML = New DOMDocument
    objXML.async = False
    objXML.Load XMLFileName

    For Each objXMLDOMNode In objXML.documentElement.childNodes
    XMLConnection = objXMLDOMNode.childNodes.Item(0).Text & ";" & objXMLDOMNode.childNodes.Item(1).Text & ";" & objXMLDOMNode.childNodes.Item(2).Text
    ConnectionXML = XMLConnection
    Next
    End Function

    This is how I'm calling the method.

    I compile and reference the DLL.

    Dim objTestFunction As clsRoutines

    Set objTestFunction = New clsRoutines

    objTestFunction.ConnectionXML (XMLFileName)

    I get the following error message
    object variable or with block variable not set.

    Any suggestions

    Thanks

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. XMLConnection = objXMLDOMNode.childNodes.Item(0).Text & ";" & objXMLDOMNode.childNodes.Item(1).Text & ";" & objXMLDOMNode.childNodes.Item(2).Text
    2. ConnectionXML = XMLConnection

    I think these 2 lines need a "Set" in the front.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Boston
    Posts
    26

    Angry Still getting Error 91

    Hi Again,

    Thanks for the posting. However I'm still receiving the error message 91. Even worse when I use the Set ... I'm getting a compile error.

    Where I'm getting confused is that when I use the function as part of a project it works like a charm. However when I created it as part of a COM object that's where it's returning an error message.

    The other method that I for this COM object works fine. When I reference my .DLL however the method that requires the use of the DOM object does not work. At this time I have no clue.

    Thanks

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Which line of code is giving the error?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Boston
    Posts
    26
    After I create the object and reference the object. This is how I'm calling the object

    Dim objConnection As New clsRoutines,strConnection AS String

    strConnection = objConnection.ConnectionXML(DBConnectionName) it errors out on this line.

    Like I said before, this works using my other method. Does not work when I'm trying to do it this way for this method. I get intellisense for this

    I also tried this way as well

    Dim objConnection As object ,strConnection AS String

    Set objConnection = CreateObject("DBFunctions.clsRoutines")
    strConnection = objConnection.ConnectionXML(FileName)

    Get the same error message

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well, one problem is, the code is amazingly confusing. Besides that you make a few assumptions:
    VB Code:
    1. XMLConnection = objXMLDOMNode.childNodes.Item(0).Text
    This code assumes there actually is an Item(0) and that you can call a Text property off of it. If there is no Item(0) and you call the Text property you will receive an Error 91 indicating the object is invalid.
    strConnection = objConnection.ConnectionXML(DBConnectionName) it errors out on this line.
    It doesn't really error out on this line, because you have zero error handling in the ConnectionXML function, the only line that catches the error is the guy calling the function.
    VB Code:
    1. ConnectionXML = XMLConnection
    This line is in your For..Next loop. Not knowing how your XML files are set up if you have more than one node in your documentElement, then for each node you are essentially resetting the value of ConnectionXML. So not too sure what the intention was for that line.
    Suggestions

    1. Add error handling to your ConnectionXML function.

    2. Tools > Options > General Tab -> in the error trapping box, make sure Break on All Errors is checked.

    3. When iterating through nodes, check to make sure your node is valid before using it:
    VB Code:
    1. Dim oChildNode As IXMLDOMNode
    2. For Each objXMLDOMNode In objXML.documentElement.childNodes
    3.   Set oChildNode = objXMLDOMNode.childNodes.Item(0)
    4.   If Not oChildNode Is Nothing Then
    5.     XMLConnection = XMLConnection & oChildNode.Text
    6.   Else
    7.     Call Err.Raise(91, "MyClass", "Item(0) not a valid object")
    8.   End If
    9.  
    10.   Set oChildNode = objXMLDOMNode.childNodes.Item(1)
    11.   If Not oChildNode Is Nothing Then
    12.     XMLConnection = XMLConnection & oChildNode.Text
    13.   Else
    14.     Call Err.Raise(91, "MyClass", "Item(1) not a valid object")
    15.   End If
    16.  
    17.   Set oChildNode = objXMLDOMNode.childNodes.Item(2)
    18.   If Not oChildNode Is Nothing Then
    19.     XMLConnection = XMLConnection & oChildNode.Text
    20.   Else
    21.     Call Err.Raise(91, "MyClass", "Item(2) not a valid object")
    22.   End If
    23.  
    24. Next
    And this code could probably be refactored to use a function to test the value of the node instead of repeating blocks of the same code.

    4. Avoid the use of member/module level variables when they're really only needed locally. You have a module level declaration of XMLFileName and a local declaration of the same name. That's confusing and can lead to some hard to find bugs.

    5. Every example you use of calling your function it appears that you're sending in an argument but your function definition is not defined as having any parameters???

    6. It also appears that the function is basically going to return a string so add the return type As String.

    7. If you step through your code i bet you'd find your problem child right away.

    8. Always check to make sure that your xml document even loaded. The Load method returns true or false, check for that value:
    VB Code:
    1. If objXML.Load (XMLFileName) = True Then
    2.   'do something with objXML
    3. Else
    4.   Call Err.Raise(666, "MyClass", "XML Failed to load...")
    5. End If

    9. USE XPATH, it's more readable and more concise than spinning through nodes:
    VB Code:
    1. 'assuming objXML was loaded successfully
    2. Dim oNodeDatabaseName As IXMLDOMNode
    3. 'also assume that the xml has a node named <DatabaseName/>
    4. Set oNodeDatabaseName = objXML.documentElement.selectSingleNode("/xml/DatabaseName")
    5. If Not oNodeDatabaseName Is Nothing Then
    6.   'do something
    7. Else
    8.   Call Err.Raise(91, "MyClass", "DatabaseName could not be determined.")
    9. End If
    good luck,
    chris
    Last edited by pvb; Oct 5th, 2002 at 06:33 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width