我做的一个通过串口来接收单片机数据的程序,然后把数据写进数据库,然后能够动态的画出曲线来,可是在我调试的时候总是出现这一行的错误,实时错误'94' 无效使用null , Picture1.Circle ((64 - kccount) * 140, (100 - rst.Fields("Voltage").Value) * 25), 30
我把完整的代码贴出来,请高手赐教!
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '...打开串口
End IfEnd SubPrivate Sub Command2_Click()
MSComm1.PortOpen = False
End SubPrivate Sub Command3_Click()MSComm1.CommPort = Combo2.Text '...使用选择的Com口MSComm1.PortOpen = True '...打开串口If MSComm1.PortOpen = True ThenMsgBox "串口已经打开", , "提示"End If
End Sub
Private Sub Form_Load()Combo1.Text = Combo1.List(0)
Combo2.Text = Combo2.List(0)
Combo3.Text = Combo3.List(0)MSComm1.InputMode = 0 '以文本的形式读入,如果为1,就是二进制读取MSComm1.InputLen = 0MSComm1.InBufferCount = 0MSComm1.RThreshold = 1MSComm1.Settings = "115200,n,8,1" '...设置通讯参数
End SubPrivate Sub MSComm1_OnComm()'...通讯事件发生Select Case MSComm1.CommEventCase comEvReceive '...有接受事件发生 '...接受显示数据
 
Text1.Text = Text1.Text + MSComm1.Input & Chr(10) & Chr(13)  '换行显示,紧接着下一行显示MSComm1.InBufferCount = 0 '...清空输入寄存器'....从接收的文本中提取数据
'用vbscript正则表达式提取字符串中的数字,工程-引用-Microsoft VBScript RegularExpressions 5.5打钩-确定
Dim str As String, num() As Single                          'str用来存储待处理的字符串,num()用来存储提取的数字
str = Text1.Text                                            '为str赋值
Set re = New RegExp                                         '初始化re为RegExp正则匹配模式
re.Global = True                                            '设置匹配时搜索str的整个字符串,若为false,只搜索str里符合条件的第一项
re.Pattern = "[0-9\-]+(?:\.[0-9]+)?"                        '定义正则表达式,这里为数字
Set mc = re.Execute(str)                                    '返回符合正则表达式匹配对象,并存储在mc中,mc为MatchCollection对象,其count属性返回匹配对象的总数
ReDim num(1 To mc.Count)                                    '重新定义num数组,下限为1,上限为由str提取到的数字个数即mc.count
i = 1                                                       '初始化数组sum脚标
For Each ma In mc                                           '循环读取mc中的每一匹配对象,并存储在ma中,ma为Match对象,其value属性返回匹配对象所匹配的值,即str里的数字。
num(i) = ma.Value                                           '将数字存储在数组sum中
i = i + 1
Next
For i = 1 To UBound(num)                                    '打印数组
'Print num(i)
Next'把数据写进数据库
sql = "select * from table1"
 Set rst = run_SQL(sql)
    rst.AddNew
    rst.Fields("channel").Value = num(1)                '这个数字是通道号
    rst.Fields("voltage").Value = num(2) * 0.001        '这个数据为电压值,电压单位为伏
    If num(2) = 0 Then                                  
    rst.Fields("resistance").Value = 0
    Else:                                                 '此公式为计算电阻值的公式
    rst.Fields("resistance").Value = (2.5 - num(2) * 0.001) * Val(Combo3.Text) / (num(2) * 0.001) '这是电阻的计算公式         
 End If
    rst.Fields("datetime").Value = Now()
    rst.Update
    Set rst = Nothing'从数据库中把数据读出来并且画出来List1.Clear
Picture1.ClsIf Combo1.Text = "第一通道" Then ch = 1
If Combo1.Text = "第二通道" Then ch = 2
If Combo1.Text = "第三通道" Then ch = 3
If Combo1.Text = "第四通道" Then ch = 4sql = "select * from table1 where channel=" & ch & " order by datetime desc"
    Set rst = run_SQL(sql)
    kccount = 0
    List1.AddItem "   "
    If Not rst Is Nothing Then
    Do While Not rst.EOF And kccount < 65
      List1.AddItem "通道号:" & rst.Fields("channel").Value & "   电阻:" & Left(rst.Fields("resistance").Value & "欧姆         ", 8) & "   采集时间:" & rst.Fields("datetime").Value
    
    Picture1.Circle ((64 - kccount) * 140, (100 - rst.Fields("Voltage").Value) * 25), 30
 
    If kccount > 0 Then
      Picture1.Line ((64 - (kccount - 1)) * 140, (100 - dianya) * 25)-((64 - kccount) * 140, (100 - rst.Fields("Voltage").Value) * 25), RGB(255, 0, 0)
    End If
     dianya = rst.Fields("Voltage").Value
      kccount = kccount + 1
      rst.MoveNext
    Loop
    Set rst = Nothing
    End If
End Sub 
问题补充:前面写进数据库的这一句很奇怪,rst.Fields("voltage").Value = num(2) * 0.001        '电压单位为伏
,这一句我的num(2)不是零的时候,我调试也会看到等式左边提示我为null,当右边num(2)为零的时候,左边也会提示我是null,我之前没有出现这个问题,请高手赐教!

解决方案 »

  1.   

    读出来的时候 rst.Fields("Voltage").Value & vbnullstring
      

  2.   

    rst.Fields("Voltage").Value & ""
      

  3.   

    参考此帖:
    http://topic.csdn.net/u/20100227/21/bd3a0139-8570-45f1-bc8c-09bbd08c6981.html
    送分100分:text1.text = rst1.fields("姓名") 时出现无效使用NULL的提示
      

  4.   


    Picture1.Circle ((64 - kccount) * 140, (100 - IIf(IsNull(rst.Fields("Voltage").Value), 0, rst.Fields("Voltage").Value)) * 25), 30
      

  5.   

    不行啊,还是会出错,这次提示,实时错误'13',类型不匹配。我觉得是在写入时就有问题,就是我说的那句rst.Fields("voltage").Value = num(2) * 0.001 '电压单位为伏,这一句在调试的时候,右边不为零,可是左边值为null,我觉得是不是这里的问题呢?
      

  6.   

    if not isnull(rst.Fields("Voltage").Value) then
        ....
    end if
      

  7.   

    还是会报同样的错误实时错误"94",无效使用null,没有变化啊。
      

  8.   

    'Picture1.Circle ((64 - kccount) * 140, (100 - rst.Fields("Voltage").Value) * 25), 30Picture1.Circle ((64 - kccount) * 140, (100 - val(rst.Fields("Voltage").Value & vbnullstring)) * 25), 30
      

  9.   

    如果rst.Fields("Voltage").Value 为 null 这个表达式就会出错...
    解决方法就是上面大家的方法,把rst.Fields("Voltage").Value转换为字符串:
    rst.Fields("Voltage").Value & vbnullstring
    在Picture1.Circle 方法中,都是数值运算,转换为字符串的结果必须的还原为数字,否则当然是类型不匹配,所以还要用一下val:
    val(rst.Fields("Voltage").Value & vbnullstring)
    这样null就转换为0...
      

  10.   

    7楼的方法是如果为null就不执行Picture1.Circle...
    你要根据程序需求决定怎么做.... 
      

  11.   

    你的方法不错,如果为null的话,那这个地方就变为0来进行计算,你能帮我解释一下,rst.Fields("Voltage").Value 为0与为null的区别吗?
      

  12.   

    可是按照你的方法,系统还是提示那个错误,因为val(rst.Fields("Voltage").Value & vbnullstring)这一行里面不允许rst.Fields("Voltage").Value为Null
      

  13.   

    0是一个数值
    null可以简单的理解为什么都没有...
      

  14.   

    我现在还有一个问题,就是这个数字是从数据库里面调出来的,按理说不可能有null的情况啊,为什么会有null的情况呢?10楼vbman提示的对,如果真的有null的情况怎么处理呢?您的方法只是去除掉了这种情况。可是按照他说的方法我就会出错。
      

  15.   

    只有variant类型才可以赋值null,其它类型赋值null都出错是....
      

  16.   

    我就有些不明白,我写进数据库的是一个电压值,不应该出现啥也没有的情况,不会有Null的情况啊,可是有是会出现这种状况,就是这一句,rst.Fields("voltage").Value = num(2) * 0.001 '电压单位为伏
    右边不为零或者空,可是左边的值还是为空。我想调出数据时的null,可能是在这个地方出的问题,写进的时候为null。