我现在遇到一个很棘手的问题,同样是增加一条记录到数据库,一个是yhxx表(注:用户信息表) 另一个是sdjl表(注:售电记录表)。 前者使用ADODC3这个控件进行增加,后者使用的是CONN.EXCUTE SQL这种方法增加,但是增加完成后,分别从这两个表中立马查询增加的用户编号,sdjl中可以立即查到,但是yhxx表,却不能立即查到,并不是说没有,需要查二、三次,才会提示查到这个用户编号。请大家帮我分析一下,原因是什么。以下是具体的代码:
'*********** 代码开始************ Adodc3.Recordset.AddNew
Adodc3.Recordset!用户编号 = CLng(Zongbianhao)
。
Adodc3.Recordset.UpdateBatch
sql = "Insert into sdjl ([用户编号],[用户表号]) values (" + Trim(Text13.Text) + "," + Trim(Text2.Text) + ")"
Conn.Execute sql Dim Rst1 As ADODB.Recordset
Set Rst1 = New ADODB.Recordset
Dim sql As String
sql = "select * from sdjl where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox "SDJL 中找不到此用户编号", vbOKOnly, "提示" Rst1.Close
sql = "select * from yhxx where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox " yhxx 中找不到此用户编号 ", vbOKOnly, "提示"'*********** 代码结束************
'*********** 代码开始************ Adodc3.Recordset.AddNew
Adodc3.Recordset!用户编号 = CLng(Zongbianhao)
。
Adodc3.Recordset.UpdateBatch
sql = "Insert into sdjl ([用户编号],[用户表号]) values (" + Trim(Text13.Text) + "," + Trim(Text2.Text) + ")"
Conn.Execute sql Dim Rst1 As ADODB.Recordset
Set Rst1 = New ADODB.Recordset
Dim sql As String
sql = "select * from sdjl where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox "SDJL 中找不到此用户编号", vbOKOnly, "提示" Rst1.Close
sql = "select * from yhxx where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox " yhxx 中找不到此用户编号 ", vbOKOnly, "提示"'*********** 代码结束************
Conn.Open (sysConnString)拜托了,猴哥。
Adodc3.Recordset.MoveFirst
Adodc3.Recordset.MoveLast
Adodc3.Refresh
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\***.mdb;Persist Security Info=False;User ID=yuejia;Password=*****;
Adodc3.Recordset!用户编号 = CLng(Zongbianhao)
Adodc3.Recordset!用户表号 = CLng(Trim(Text2.Text))
Adodc3.Recordset!用户号 = CLng(Trim(Text12.Text))
' Adodc3.Recordset.Update
Adodc3.Recordset.UpdateBatch
Adodc3.Recordset.MoveFirst
Adodc3.Recordset.MoveLast
Adodc3.Refresh
Dim Rst1 As ADODB.Recordset
Set Rst1 = New ADODB.Recordset
Dim sql As String
sql = "select * from yhxx where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox "用户信息中找不到", vbOKOnly, "提示"
程序 还是提示 用户信息中找不到
Private Declare Sub Sleep Lib Kernel32 (ByVal dwMilliseconds As Long)'加在窗体程序开始位置……
Adodc3.Recordset.AddNew
Adodc3.Recordset!用户编号 = CLng(Zongbianhao)
。
Adodc3.Recordset.UpdateBatch
sql = "Insert into sdjl ([用户编号],[用户表号]) values (" + Trim(Text13.Text) + "," + Trim(Text2.Text) + ")"
Conn.Execute sql Dim Rst1 As ADODB.Recordset
Set Rst1 = New ADODB.Recordset
Dim sql As String
sql = "select * from sdjl where 用户编号 =" & CStr(Zongbianhao) & ""
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox "SDJL 中找不到此用户编号", vbOKOnly, "提示" Rst1.Close
sql = "select * from yhxx where 用户编号 =" & CStr(Zongbianhao) & ""
sleep(1000) '时间可以调整,1000改成多少,试验看吧
If Rst1.State = 1 Then Rst1.Close
Rst1.Open sql, Conn, adOpenKeyset, adLockPessimistic
If Rst1.RecordCount = 0 Then MsgBox " yhxx 中找不到此用户编号 ", vbOKOnly, "提示" '*********** 代码结束************
Rst1.Open sql, Conn, adOpenStatic, adLockPessimistic
t=timer
do
doenvets
loop while timer-t<1'这就是延时一秒,可以是小数
直接用conn操作就没这些麻烦事了
Adodc3.Recordset.Requery
------------
完全本末倒置,用多个连接才是导致问题的原因。
既然直接操作 Recordset,就根本不需要使用 Adodc 控件。
实在不明白您的意思,能说的更详细一些吗?多个连接是指哪几个连接呀?再就是“既然直接操作 Recordset,就根本不需要使用 Adodc 控件。” 这个RECORDSET只是用来查询,以验证添加记录后的效果。