PDA

Click to See Complete Forum and Search --> : Returning an error message in a Com Object


jscontreras
Sep 25th, 2002, 09:59 AM
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

crptcblade
Sep 25th, 2002, 10:16 PM
XMLConnection = objXMLDOMNode.childNodes.Item(0).Text & ";" & objXMLDOMNode.childNodes.Item(1).Text & ";" & objXMLDOMNode.childNodes.Item(2).Text
ConnectionXML = XMLConnection


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

:)

jscontreras
Sep 30th, 2002, 12:39 PM
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

Edneeis
Sep 30th, 2002, 12:54 PM
Which line of code is giving the error?

jscontreras
Sep 30th, 2002, 01:10 PM
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

pvb
Oct 5th, 2002, 12:43 PM
Well, one problem is, the code is amazingly confusing. Besides that you make a few assumptions:

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.

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:

Dim oChildNode As IXMLDOMNode
For Each objXMLDOMNode In objXML.documentElement.childNodes
Set oChildNode = objXMLDOMNode.childNodes.Item(0)
If Not oChildNode Is Nothing Then
XMLConnection = XMLConnection & oChildNode.Text
Else
Call Err.Raise(91, "MyClass", "Item(0) not a valid object")
End If

Set oChildNode = objXMLDOMNode.childNodes.Item(1)
If Not oChildNode Is Nothing Then
XMLConnection = XMLConnection & oChildNode.Text
Else
Call Err.Raise(91, "MyClass", "Item(1) not a valid object")
End If

Set oChildNode = objXMLDOMNode.childNodes.Item(2)
If Not oChildNode Is Nothing Then
XMLConnection = XMLConnection & oChildNode.Text
Else
Call Err.Raise(91, "MyClass", "Item(2) not a valid object")
End If

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:

If objXML.Load (XMLFileName) = True Then
'do something with objXML
Else
Call Err.Raise(666, "MyClass", "XML Failed to load...")
End If


9. USE XPATH, it's more readable and more concise than spinning through nodes:

'assuming objXML was loaded successfully
Dim oNodeDatabaseName As IXMLDOMNode
'also assume that the xml has a node named <DatabaseName/>
Set oNodeDatabaseName = objXML.documentElement.selectSingleNode("/xml/DatabaseName")
If Not oNodeDatabaseName Is Nothing Then
'do something
Else
Call Err.Raise(91, "MyClass", "DatabaseName could not be determined.")
End If

good luck,
chris