1、VB中MSFlexGrid中保存多行数据至ACCESS数据库
我直接在msflexgrid表中添加数据的,
还有一个问题.
If IsNumeric(MSFlexGrid1.TextMatrix(0, 3)) = False Then
MsgBox "数量不是数字!", , "提示!"
End If
这样判断是不行,麻烦帮改进一下,谢谢!
1、VB中MSFlexGrid中保存多行数据至ACCESS数据库
我直接在msflexgrid表中添加数据的,
还有一个问题.
If IsNumeric(MSFlexGrid1.TextMatrix(0, 3)) = False Then
MsgBox "数量不是数字!", , "提示!"
End If
这样判断是不行,麻烦帮改进一下,谢谢!
可以参考下面的代码:
Dim js As Integer
For i = 1 To flex1.Rows - 1
If flex1.TextMatrix(i, 1) <> "" And flex1.TextMatrix(i, 2) <> "" And flex1.TextMatrix(i, 4) = "" Then
MsgBox "第" & i & "行录入错误!", , "提示窗口"
Exit Sub
End If
If flex1.TextMatrix(i, 1) = "" Then
js = js + 1
End If
Next i
If js = flex1.Rows - 1 Then
MsgBox "没有要保存的数据!", , "提示窗口"
Exit Sub
End If
For i = 1 To flex1.Rows - 1
If flex1.TextMatrix(i, 1) <> "" And flex1.TextMatrix(i, 2) <> "" And flex1.TextMatrix(i, 5) <> "" Then
'添加新记录到"入库表"中
rs1.Open "select * from 入库表", Cnn, adOpenKeyset, adLockOptimistic
rs1.AddNew
If flex1.TextMatrix(i, 1) <> "" Then rs1.Fields("条形码") = flex1.TextMatrix(i, 1)
If flex1.TextMatrix(i, 2) <> "" Then rs1.Fields("书名") = flex1.TextMatrix(i, 2)
If flex1.TextMatrix(i, 3) <> "" Then rs1.Fields("作者") = flex1.TextMatrix(i, 3)
If flex1.TextMatrix(i, 3) <> "" Then rs1.Fields("出版社") = flex1.TextMatrix(i, 4)
rs1.Fields("数量") = Val(flex1.TextMatrix(i, 5))
rs1.Fields("单价") = Val(flex1.TextMatrix(i, 6))
rs1.Fields("金额") = Val(flex1.TextMatrix(i, 7))
If flex1.TextMatrix(i, 8) <> "" Then rs1.Fields("备注") = flex1.TextMatrix(i, 8)
If txtDate.text <> "" Then rs1.Fields("入库日期") = txtDate
If txtph <> "" Then rs1.Fields("入库单据号") = txtph
If txtjsr.text <> "" Then rs1.Fields("经手人") = txtjsr
rs1.Update
rs1.Close
rs2.Open "select * from 图书信息表 where 条形码='" + flex1.TextMatrix(i, 1) + "'and 书名='" + flex1.TextMatrix(i, 2) + "'", Cnn, adOpenKeyset, adLockOptimistic
If rs2.RecordCount > 0 Then
Cnn.Execute ("update 图书信息表 set 数量=数量+" & Val(flex1.TextMatrix(i, 5)) & ",金额=(数量+" & Val(flex1.TextMatrix(i, 5)) & ")*单价 where 条形码 ='" + flex1.TextMatrix(i, 1) + "'")
Else
rs2.AddNew
If flex1.TextMatrix(i, 1) <> "" Then rs2.Fields("条形码") = flex1.TextMatrix(i, 1)
If flex1.TextMatrix(i, 2) <> "" Then rs2.Fields("书名") = flex1.TextMatrix(i, 2)
If flex1.TextMatrix(i, 3) <> "" Then rs2.Fields("作者") = flex1.TextMatrix(i, 3)
If flex1.TextMatrix(i, 3) <> "" Then rs2.Fields("出版社") = flex1.TextMatrix(i, 4)
rs2.Fields("数量") = Val(flex1.TextMatrix(i, 5))
rs2.Fields("单价") = Val(flex1.TextMatrix(i, 6))
rs2.Fields("金额") = Val(flex1.TextMatrix(i, 7))
If flex1.TextMatrix(i, 8) <> "" Then rs2.Fields("备注") = flex1.TextMatrix(i, 8)
rs2.Update
End If
rs2.Close
End If
Next i
if int(val(MSFlexGrid1.TextMatrix(0, 3)) )=0 then
MsgBox "数量不是数字!", , "提示!"
end if
至于写入ACCESS,首先添加引用adodb
代码大概如下
Dim Conn As ADODB.Connection Set Conn = New ADODB.Connection Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ACCESS数据库路径 & ";Persist Security Info=False;"
Conn.Execute " insert into 表名 values。。。。。。。。。'"
Conn.Close