vb addnew 每次只更新第一行的记录 只有一行 Update也不好用

PrivateSubCommand1_Click()SetadoRecordset=NewADODB.Recordsetdate1=Format(Now,"yyyy-mm... Private Sub Command1_Click()
Set adoRecordset = New ADODB.Recordset
date1 = Format(Now, "yyyy-mm-dd") ' 日期
time1 = Format(Now, "hh:mm:ss") ' 时间
k = k + 1
adoRecordset.Fields.Append "序号", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "日期", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "时间", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "二维码", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "电池内阻", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "电池电压", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Fields.Append "检测结果", adVariant, adFldMayBeNull + adFldIsNullable + adFldUpdatable
adoRecordset.Open
'For k = 0 To k

adoRecordset.AddNew '加四条记录,即为DataGrid添加四空行
adoRecordset.Fields("序号") = k
adoRecordset.Fields("日期") = date1
adoRecordset.Fields("时间") = time1
adoRecordset.Fields("二维码") = k
adoRecordset.Fields("电池内阻") = k
adoRecordset.Fields("电池电压") = k
adoRecordset.Fields("检测结果") = k
'Next k
adoRecordset.Update
Set DataGrid1.DataSource = adoRecordset '绑定 DataGrid 的数据源

'total = adoRecordset.RecordCount
adoRecordset.MoveNext
End Sub
展开
 我来答
syx54
2015-06-11 · TA获得超过7379个赞
知道大有可为答主
回答量:6567
采纳率:83%
帮助的人:2767万
展开全部
addnew 是添加一条空记录
Update 是更新记录
你想单击一次,添加多行,就要恢复里边的循环呀???
循环用
for i = 1 to 4
试试,应该可以添加4条记录?
追问
之前用的for循环 但是所有记录都是最后更新的那一条   程序里那个FOR K=0 TO K-1 就是  我屏蔽掉了
追答
你试试我的这段代码:

Private Sub Command1_Click()
Dim Rst As New ADODB.Recordset

Rst.CursorLocation = adUseClient

' Add columns to the Recordset
Rst.Fields.Append "Key", adInteger
Rst.Fields.Append "Field1", adVarChar, 40, adFldIsNullable
Rst.Fields.Append "Field2", adDate

' Open the Recordset
Rst.Open , , adOpenStatic, adLockBatchOptimistic

' Add data to the Recordset
Rst.AddNew Array("Key", "Field1", "Field2"), _
Array(1, "string1", Date)
Rst.AddNew Array("Key", "Field1", "Field2"), _
Array(2, "string2", #1/1/2000#)

' Populate the Data in the DataGrid
Set DataGrid1.DataSource = Rst

End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式