急救呀,为什么老提示“实时错误94,无效使用NULL“?
急救呀,本人有个程序实现从ACCESS把数据导出到EXCEL里的代码,但是调试时老是出现一个问题,就是每次执行到"d9 = rs.Fields("备注")"这一句时,就老提示“实时错误94,无效使用NULL“? 首先声明本人的”备注“栏里并不是每要栏都有数据,在大多栏是没有内容的。我想问题就是出现在这,有什么办法可以把不管有没有备注内容都可以实现导出到EXCEL里呢? 高手指教!谢谢Private Sub Command1_Click()
Dim conn As Connection
Dim rs As Recordset
Dim strcon As String
Dim sql As String
Dim xlapp As Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlbllk As Excel.Workbook
Dim d1 As String
Dim d2 As String
Dim d3 As String
Dim d4 As String
Dim d5 As String
Dim d6 As String
Dim d7 As String
Dim d8 As String
Dim d9 As String
Set conn = New ADODB.Connection
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fullpath("ziliao.lbl")
conn.Open strcon
Set rs = New ADODB.Recordset
sql = "select 编号,文件名称,数量,类型,入库时间,编制单位,编制时间,存放位置,备注 from wzzl_v"
rs.Open sql, conn, adOpenDynamic, adLockOptimistic
Set xlapp = New Excel.Application
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
Set xlbllk = xlapp.Workbooks.Add
Set xlsheet = xlbllk.Worksheets(1)
If IsNull(rs!编号) = False Then
xlsheet.Cells(1, 1) = "编号"
End If
If IsNull(rs!文件名称) = False Then
xlsheet.Cells(1, 2) = "文件名称"
End If
If IsNull(rs!数量) = False Then
xlsheet.Cells(1, 3) = "数量"
End If
If IsNull(rs!类型) = False Then
xlsheet.Cells(1, 4) = "类型"
End If
If IsNull(rs!入库时间) = False Then
xlsheet.Cells(1, 5) = "入库时间"
End If
If IsNull(rs!编制单位) = False Then
xlsheet.Cells(1, 6) = "编制单位"
End If
If IsNull(rs!编制时间) = False Then
xlsheet.Cells(1, 7) = "编制时间"
End If
If IsNull(rs!存放位置) = False Then
xlsheet.Cells(1, 8) = "存放位置"
End If
If IsNull(rs!备注) = False Then
xlsheet.Cells(1, 9) = "备注"
End If
i = 2
Do While Not rs.EOF
d1 = rs.Fields("编号")
d2 = rs.Fields("文件名称")
d3 = rs.Fields("数量")
d4 = rs.Fields("类型")
d5 = rs.Fields("入库时间")
d6 = rs.Fields("编制单位")
d7 = rs.Fields("编制时间")
d8 = rs.Fields("存放位置")
d9 = rs.Fields("备注")
xlsheet.Cells(i, 1) = d1
xlsheet.Cells(i, 2) = d2
xlsheet.Cells(i, 3) = d3
xlsheet.Cells(i, 4) = d4
xlsheet.Cells(i, 5) = d5
xlsheet.Cells(i, 6) = d6
xlsheet.Cells(i, 7) = d7
xlsheet.Cells(i, 8) = d8
xlsheet.Cells(i, 9) = d9
i = i + 1
rs.MoveNext
Loop
End Sub
--------------------编程问答-------------------- d1 = rs.Fields("编号") & "" --------------------编程问答-------------------- d9 = rs.Fields("备注") & "" --------------------编程问答-------------------- 可以优化一下:
--------------------编程问答--------------------
for ii%=0 to rs.Fields.Count - 1
xlsheet.Cells(1, ii%+1) = rs.Fields(ii%).Name
next ii%
rs.MoveFirst
i = 2
Do While Not rs.EOF
for ii%=0 to rs.Fields.Count - 1
xlsheet.Cells(i, ii%+1)="'" & rs.Fields(ii%)
next ii%
i = i + 1
rs.MoveNext
Loop
大哥,这个好像还是不对哦,加了这个后调试是没有报错了,但是”备注“栏里本来有备注信息的导出到EXCEL时却全部变成空白了,这种”与“法不对吧? --------------------编程问答--------------------
-----------------------------
呵呵,问题解决,大哥说的是正确的,是我调试时没把程序写好,谢谢,
三楼的写了代码优化非常短,太强了,只不过我看不懂,所以还没试过对不对,呵呵,有心的朋友可以测下,学习下。 --------------------编程问答-------------------- 这就是4条裤衩和1条裤衩的区别!嘎嘎! --------------------编程问答-------------------- d9 = iif(isnull(rs.Fields("备注"))=true, "", rs.Fields("备注")
--------------------编程问答-------------------- d9 = iif(isnull(rs.Fields("备注"))=true, "", rs.Fields("备注"))
忘了,刚才少一个括号
补充:VB , 数据库(包含打印,安装,报表)