引用Msxml2产生 实时错误 '91' 对象变量或WITH变量未设置。

xiangyyy 2007-04-12 10:33:04
错误地方如下,请问如何解决.谢谢!

emp.appendChild tempNode------------实时错误‘91’:对象变量或WITH变量未设置。


Public tempDoc As MSXML2.DOMDocument 'xml文件
Public tempNode As MSXML2.IXMLDOMNode
Public Root As MSXML2.IXMLDOMElement
Public tempelement As MSXML2.IXMLDOMElement
Public tempattribute As MSXML2.IXMLDOMElement
'Public emp As MSXML2.IXMLDOMElement



Private Sub Form_Load()

Dim OraDB, Sqlstr


Set OraDB = New ADODB.Connection

OraDB.CursorLocation = adUseServer

OraDB.open "linkstr"


Set OraRe1 = New ADODB.Recordset

Sqlstr1 = "select order_number,item_code,order_quantity,item_volume,order_price from CUX.CUX_SPD_ORDERLIST_INTERFACE "

OraRe1.open Sqlstr1, OraDB, 3

MsgBox OraRe1.RecordCount




Set tempDoc = New MSXML2.DOMDocument

'生成根节点并把它设置为文件的根
Set Root = tempDoc.createElement("orders")
Set tempDoc.documentElement = Root


Do While Not OraRe1.EOF And Not OraRe1.BOF

Dim emp As MSXML2.IXMLDOMElement
'生成孩子节点添加到根节点上去,并且为这个节点设置一个属性
Set tempNode1 = tempDoc.createNode(MSXML2.NODE_ELEMENT, "order_number", "")
tempNode1.Text = OraRe1("order_number")
emp.appendChild tempNode1

Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "item_code", "")
tempNode.Text = OraRe1("item_code")
emp.appendChild tempNode

Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "order_quantity", "")
tempNode.Text = OraRe1("order_quantity")











emp.appendChild tempNode------------实时错误‘91’:对象变量或WITH变量未设置。





Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "item_volume", "")
tempNode.Text = OraRe1("item_volume")
emp.appendChild tempNode

Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "order_price", "")
tempNode.Text = OraRe1("order_price")
emp.appendChild tempNode


OraRe1.MoveNext
Loop

Dim pi As IXMLDOMProcessingInstruction
Set pi = tempDoc.createProcessingInstruction("xml", "version='1.0' encoding='gb2312'")

Call tempDoc.insertBefore(pi, tempDoc.childNodes(0))
'直接保存成文件即可
tempDoc.save "c:\CUX.xml"

Unload Me

End Sub
...全文
529 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
daisy8675 2007-04-23
  • 打赏
  • 举报
回复
楼主的代码写得太乱,各种命名几乎可以用乱七八糟来形容,看得头大。

实在没办法去看清楚,只能提醒一点太明显的

OraDB.CursorLocation = adUseServer 最好用adUseClient端。由于你的代码实在太糟糕。建议你,第一,重新整理变量名。第二。重新检查引用。

给你段代码自己参考好了。

Option Explicit

Public Rs As New ADODB.Recordset
Public Conn As New ADODB.Connection

Public tempDoc As MSXML2.DOMDocument 'xml文件
Public tempNode As MSXML2.IXMLDOMNode
Public Root As MSXML2.IXMLDOMElement
Public tempelement As MSXML2.IXMLDOMElement
Public tempattribute As MSXML2.IXMLDOMElement
Public emp As MSXML2.IXMLDOMElement

Private Sub Command1_Click()

'生成一个XML DOMDocument对象
Set tempDoc = New MSXML2.DOMDocument

'生成根节点并把它设置为文件的根
Set Root = tempDoc.createElement("employees")
Set tempDoc.documentElement = Root
'在节点上添加多个属性
Call Root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
Call Root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
Call Root.setAttribute("xmlns", "http://www.kingdee.com/ReK3Inventory")

Do While Not Rs.EOF
Set emp = tempDoc.createNode(MSXML2.NODE_ELEMENT, "employee", "")
Root.appendChild emp

'生成孩子节点添加到根节点上去,并且为这个节点设置一个属性
Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "Employeeid", "")
tempNode.Text = Rs(0)
emp.appendChild tempNode

Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "Firstname", "")
tempNode.Text = Rs(1)
emp.appendChild tempNode

Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "Title", "")
tempNode.Text = Rs(2)
emp.appendChild tempNode
Rs.MoveNext
Loop

Dim pi As IXMLDOMProcessingInstruction
Set pi = tempDoc.createProcessingInstruction("xml", "version='1.0' encoding='gb2312'")

Call tempDoc.insertBefore(pi, tempDoc.childNodes(0))
'直接保存成文件即可
tempDoc.Save "c:\myTest.xml"

Unload Me

End Sub

Private Sub Form_Load()
Dim strConn As String
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=LocalHost"
Conn.CursorLocation = adUseClient
Conn.Open strConn

If Rs.State <> adStateClosed Then Rs.Close
Rs.Open "Select employeeid,Firstname,Title from employees ", Conn, adOpenStatic, adLockOptimistic

End Sub
xiangyyy 2007-04-18
  • 打赏
  • 举报
回复
不行啊,谁能解答?

guyehanxinlei 2007-04-18
  • 打赏
  • 举报
回复
多数是对象引用方面的问题
CathySun118 2007-04-12
  • 打赏
  • 举报
回复
在下面代码之前增加Set tempNode=nothing 试试
Set tempNode = tempDoc.createNode(MSXML2.NODE_ELEMENT, "order_quantity", "")
tempNode.Text = OraRe1("order_quantity")











emp.appendChild tempNode------------实时错误‘91’:对象变量或WITH变量未设置。


xiangyyy 2007-04-12
  • 打赏
  • 举报
回复
不行呢.
fj182 2007-04-12
  • 打赏
  • 举报
回复
emp对象未实例化,按照你代码的意思这个emp节点应该是对应到每一条记录,加入下列代码试试。

Dim emp As MSXML2.IXMLDOMElement
'生成孩子节点添加到根节点上去,并且为这个节点设置一个属性
Set emp = Root.appendChild (tempDoc.createElement("item")) ' *****
Set tempNode1 = tempDoc.createNode(MSXML2.NODE_ELEMENT, "order_number", "")
tempNode1.Text = OraRe1("order_number")
emp.appendChild tempNode1

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧