VB6.0 实时错误1004 应用程序定义或对象定义错误 急~~

VB6.0 实时错误1004 应用程序定义或对象定义错误 急~~
我说做一个导出,就是把vb datagrid中的记录导出到excel中,其他记录都ok,但运行到一条记录时就出现了实时错误1004 应用程序定义或对象定义错误,调试跳到的错误语句是:.Cells(lngID + 1, intField) = Adodc1.Recordset(intField - 1).Value
具体代码如下
Private Sub Command2_Click()

Dim priXLS As Excel.Application
Dim priWorkbook As Excel.Workbook
Dim priSheet As Excel.Worksheet
Dim lngRow, lngRows, intField, intFields, lngID As Long
Set priXLS = New Excel.Application
Set priWorkbook = priXLS.Workbooks.Add
Set priSheet = priXLS.Sheets(1)
With priSheet
intFields = Adodc1.Recordset.Fields.Count
For intField = 1 To intFields
.Cells(1, intField) = Adodc1.Recordset(intField - 1).Name
Next
Adodc1.Recordset.MoveLast
Adodc1.Recordset.MoveFirst
For lngID = 1 To Adodc1.Recordset.RecordCount
For intField = 1 To intFields
.Cells(lngID + 1, intField) = Adodc1.Recordset(intField - 1).Value
Next
Adodc1.Recordset.MoveNext
Next
End With
priXLS.Visible = True

End Sub
出现实时错误1004 应用程序祥凳睁定义或对象定义错误的原因是:
在 Adodc中,该字段的数据谨岁类型是图片或多媒体等,而Excel不能识别,故出现上粗码述错误。
修改如下:
intField) = Adodc1.Recordset(intField - 1).Value
具体代码如下
Private Sub Command2_Click()

Dim priXLS As Excel.Application
Dim priWorkbook As Excel.Workbook
Dim priSheet As Excel.Worksheet
Dim lngRow, lngRows, intField, intFields, lngID As Long
Set priXLS = New Excel.Application
Set priWorkbook = priXLS.Workbooks.Add
Set priSheet = priXLS.Sheets(1)
With priSheet
intFields = Adodc1.Recordset.Fields.Count
For intField = 1 To intFields
.Cells(1, intField) = Adodc1.Recordset(intField - 1).Name
Next
Adodc1.Recordset.MoveLast
Adodc1.Recordset.MoveFirst
On Error Resume Next '防止字段类型Excel不识而出错!
For lngID = 1 To Adodc1.Recordset.RecordCount
For intField = 1 To intFields
.Cells(lngID + 1, intField) = Adodc1.Recordset(intField - 1).Value
Next
Adodc1.Recordset.MoveNext
Next
End With
priXLS.Visible = True

End Sub
实际代李腔码有没有,贴出来看看,光看问题不知道哪凳衫根结在哪里一般来讲就是代码没有写对,注意你的控件name属性在代码中调用的时候是否写对了。例如一个按钮控件,name属性是btnok,而你在代码里面写成这样粗穗的:text1.text=command1.caption这样写就会出现424的错误。先把代码贴出来看看。
← 上一篇: 下一篇: →