我现在要把MsflexGrid1中的数据保存到数据库,但里面有几项有时候是没有数据的,最后再把数据库中的数据显示到另一个窗体的MsflexGrid控件中,同样也有没有数据的项,我写了如下代码:
Set RS = New ADODB.Recordset
        strsql = "SELECT * FROM csh优化配载查询表"
        RS.CursorLocation = adUseClient
        RS.Open strsql, cn, adOpenDynamic, adLockBatchOptimistic
RS.AddNew
With MSFlexGrid1
RS!车牌号 = Combo1.text
RS!商品车公司 = Combo2.text
RS!六up1 = .TextMatrix(2, 1)
RS!六up2 = .TextMatrix(2, 2)
RS!六up3 = .TextMatrix(2, 3)
RS!六up4 = .TextMatrix(2, 4)
RS!六up5 = .TextMatrix(2, 5)
RS!六up6 = .TextMatrix(2, 6)
RS!六up装载率 = .TextMatrix(2, 7)
End with
运行以后不会保存,不知道什么原因,也没有报错。下面是在另一个窗体显示数据库中数据的模块
Private Sub Command1_Click()
Set RS = cn.Execute("select * from csh优化配载查询表 where 车牌号='" & Combo1.text & "'and 商品车公司='" & Combo3.text & "'")
With MSFlexGrid1
 .TextMatrix(0, 0) = Combo1.text
 .TextMatrix(2, 1) = RS!六up1
 .TextMatrix(2, 2) = RS!六up2
 .TextMatrix(2, 3) = RS!六up3
 .TextMatrix(2, 4) = RS!六up4
 .TextMatrix(2, 5) = RS!六up5
 .TextMatrix(2, 6) = RS!六up6
 .TextMatrix(2, 7) = RS!六up装载率
End with
报错说无效使用“Null”,报的是没有数据的那个项,怎么样改一下可以在MsflexGrid空间中显示数据库中为Null的项呢?
谢谢指点

解决方案 »

  1.   

    另外我在前一部分保存的代码最后加
    RS.update
    MsgBox "保存成功!", vbOKOnly + vbExclamation, "提示"
    运行后能提示保存成功,但数据库中就是没有数据增加,是不是因为MsflexGrid1.TextMatrix(i, j)有值为Null啊?那要怎么改呢?谢谢了
      

  2.   

    改成這樣賦值了,試試看:.........
    '如果RS!六up1是Null,則給.TextMatrix(2, 1)賦值為空值
    .TextMatrix(2, 1) = IFF(ISNull(RS!六up1),"",RS!六up1)
    ............
      

  3.   

    RS.update是不能把數據保存到數據庫中,
    隻是在當前Rs後追了一筆記錄而已.
    為什麼不用SQL的Insert語句呢?
      

  4.   

    谢谢各位,第二部分显示的问题已经解决了! 第一部部分RS.AddNew 还是没解决,insert语句不是很习惯啊,我在其他地方都用RS.AddNew 好像也没问题,不知道是哪里错了,还请高人指点
      

  5.   

    用一下format就可以了比如:  .TextMatrix(2, 1) = format(RS!六up1)
      

  6.   

    不知道对不对:指定对应行列rs.fields("xxx")=trim(MsflexGrid.text)
      

  7.   

    Set RS = New ADODB.Recordset
            strsql = "SELECT * FROM csh优化配载查询表"
            RS.CursorLocation = adUseClient
            RS.Open strsql, cn, adOpenDynamic, adLockBatchOptimistic
    RS.AddNew
    With MSFlexGrid1
    RS!车牌号 = Combo1.text
    RS!商品车公司 = Combo2.text
    RS!六up1 = .TextMatrix(2, 1)
    RS!六up2 = .TextMatrix(2, 2)
    RS!六up3 = .TextMatrix(2, 3)
    RS!六up4 = .TextMatrix(2, 4)
    RS!六up5 = .TextMatrix(2, 5)
    RS!六up6 = .TextMatrix(2, 6)
    RS!六up装载率 = .TextMatrix(2, 7)
    End with
    运行以后不会保存,不知道什么原因,也没有报错。
    -----------------------------
    你没有用update方法更新,修改一下:    Set RS = New ADODB.Recordset
        strsql = "SELECT * FROM csh优化配载查询表 where 1=2 " '这样打开一个空记录集,快点 
        RS.Open strsql, cn, adOpenDynamic, adLockBatchOptimistic
        RS.AddNew
        With MSFlexGrid1
            RS!车牌号 = Combo1.text
            RS!商品车公司 = Combo2.text
            RS!六up1 = .TextMatrix(2, 1)
            RS!六up2 = .TextMatrix(2, 2)
            RS!六up3 = .TextMatrix(2, 3)
            RS!六up4 = .TextMatrix(2, 4)
            RS!六up5 = .TextMatrix(2, 5)
            RS!六up6 = .TextMatrix(2, 6)
            RS!六up装载率 = .TextMatrix(2, 7)
        End with
        rs.update
    如果 MsflexGrid1.TextMatrix(i, j)有空值就要加一个判断:if trim(MsflexGrid1.TextMatrix(2, 6))<>"" then RS!六up6 = .TextMatrix(2, 6)试试看