
请问一下,vb中增加记录之后,为什么显示“添加成功”之后,DataGrid1中不能显示刚刚添加的这条记录?
PrivateSubCommand1_Click()DimcnnAsNewConnectionDimrsAsNewRecordsetDimmrcAsNewRecordse...
Private Sub Command1_Click()
Dim cnn As New Connection
Dim rs As New Recordset
Dim mrc As New Recordset
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\data\win.mdb;Persist Security Info=False" '如果不用这个模块也行可以,这一句便是常用的引擎。
cnn.Open
rs.Open "select * from memoA", cnn, adOpenStatic, adLockOptimistic
'AdoRs2.MoveLast
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Or DTPicker1.Value = "" Or Text7.Text = "" Or Text8.Text = "" Then
MsgBox "请输入完整数据", vbInformation, "输入不完整"
Else
mrc.Open "select * from memoA where 序号 = " & Trim(Text8.Text) & "", cnn, adOpenStatic, adLockOptimistic '检验输入用户名是否存在
If mrc.RecordCount <> 0 Then
MsgBox "已有相同的序列号", vbInformation, "提示-重新输入序号"
Else
rs.AddNew
rs.Fields(0) = Text8.Text
rs.Fields(1) = Text1.Text
rs.Fields(2) = Text2.Text
rs.Fields(3) = Text3.Text
rs.Fields(4) = Text4.Text
rs.Fields(5) = DTPicker1.Value
rs.Fields(6) = Text7.Text
rs.Fields(7) = Text7.Text
MsgBox "添加成功!", , "提示"
rs.Update
rs.Close
DataGrid1.Refresh
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
'Set rs = cnn.Execute("insert into memoA values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & DTPicker1.Value & "','" & DTPicker1.Value & "','" & Text7.Text & "')")
'MsgBox "添加成功"
End If
End If
cnn.Close
End Sub 展开
Dim cnn As New Connection
Dim rs As New Recordset
Dim mrc As New Recordset
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\data\win.mdb;Persist Security Info=False" '如果不用这个模块也行可以,这一句便是常用的引擎。
cnn.Open
rs.Open "select * from memoA", cnn, adOpenStatic, adLockOptimistic
'AdoRs2.MoveLast
If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Or DTPicker1.Value = "" Or Text7.Text = "" Or Text8.Text = "" Then
MsgBox "请输入完整数据", vbInformation, "输入不完整"
Else
mrc.Open "select * from memoA where 序号 = " & Trim(Text8.Text) & "", cnn, adOpenStatic, adLockOptimistic '检验输入用户名是否存在
If mrc.RecordCount <> 0 Then
MsgBox "已有相同的序列号", vbInformation, "提示-重新输入序号"
Else
rs.AddNew
rs.Fields(0) = Text8.Text
rs.Fields(1) = Text1.Text
rs.Fields(2) = Text2.Text
rs.Fields(3) = Text3.Text
rs.Fields(4) = Text4.Text
rs.Fields(5) = DTPicker1.Value
rs.Fields(6) = Text7.Text
rs.Fields(7) = Text7.Text
MsgBox "添加成功!", , "提示"
rs.Update
rs.Close
DataGrid1.Refresh
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
'Set rs = cnn.Execute("insert into memoA values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & DTPicker1.Value & "','" & DTPicker1.Value & "','" & Text7.Text & "')")
'MsgBox "添加成功"
End If
End If
cnn.Close
End Sub 展开
3个回答
2011-04-09
展开全部
你绑定的是没添加之前数据库里的内容,要让DataGrid1显示最新内容,应该在提交后对他进行重新绑定,这样就可以了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'MsgBox "添加成功"
rs.resh什么的 不看就忘了,就刷新下完了
rs.resh什么的 不看就忘了,就刷新下完了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
rs.Close
DataGrid1.Refresh
以上两句,改成以下两:
rs.Requery
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
以上两句,改成以下两:
rs.Requery
Set DataGrid1.DataSource = rs
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询