VB中DataGrid1控件中新增加一条记录
用的是access2003数据库,表名为b1,主键为ID,其他列名都可为空更新按钮的代码为:PrivateSubCommand2_Click()DimconnAsNewA...
用的是access2003数据库,表名为b1,主键为ID,其他列名都可为空
更新按钮的代码为:
Private Sub Command2_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\bike.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
rs.Open "select * from b1 ", conn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
On Error Resume Next
rs.AddNew
rs!ID = wjxm
rs!品牌 = zumc
rs!描述 = timex
rs.Update
rs.Close
conn.Close
Adodc1.Refresh
DataGrid1.Refresh
Set DataGrid1.DataSource = Adodc1
End Sub
点击后没有增加新的记录,也没有报错!! 展开
更新按钮的代码为:
Private Sub Command2_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\bike.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
rs.Open "select * from b1 ", conn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
On Error Resume Next
rs.AddNew
rs!ID = wjxm
rs!品牌 = zumc
rs!描述 = timex
rs.Update
rs.Close
conn.Close
Adodc1.Refresh
DataGrid1.Refresh
Set DataGrid1.DataSource = Adodc1
End Sub
点击后没有增加新的记录,也没有报错!! 展开
1个回答
展开全部
1.亲,您已经对代码容错了哦,
请将代码中的
On Error Resume Next
这行注释掉
'On Error Resume Next
2.您的datasheet中主键是"编号"吗?是否有格式要求?这些都会导致代码不成功.
建议将代码改为:
Private Sub Command2_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\bike.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
rs.Open "select * from b1 ", conn, adOpenStatic, adLockOptimistic
'Set DataGrid1.DataSource = rs
'On Error Resume Next
rs.AddNew
rs.Fields("ID") = wjxm
rs.Fields("品牌") = zumc
rs.Fields("描述") = timex
rs.Fields("编号") = rs.RecordCount + 1
rs.Update
rs.Close
conn.Close
Adodc1.Refresh
DataGrid1.Refresh
End Sub
追答
亲,昨天1:20多睡觉了,晚睡了会,早晨嗓子疼...55,对不起啊,我是基于保守考虑,将记录绑定的语言注释掉了,加我QQ吧:896376191,顺便试下如下代码:
Private Sub Command2_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\bike.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
rs.Open "select * from b1 ", conn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
'On Error Resume Next
rs.AddNew
rs.Fields("ID") = wjxm
rs.Fields("品牌") = zumc
rs.Fields("描述") = timex
rs.Fields("编号") = rs.RecordCount + 1
rs.Update
rs.Close
conn.Close
Adodc1.Refresh
DataGrid1.Refresh
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询