主题:高手请进,关于ADODB记录集的问题!!
问题是:
list总是少显示最后一个加入的。如果在标号为 行100 处加入一个MSGBOX就可以同步及时刷新 新加入的记录。
其中:executesql 是个自定义执行SQL的函数。
Private Sub Command1_Click(Index As Integer)
If Index = 1 Then Unload Me '控件数组的取消
Dim strSQL As String
Dim mrc As ADODB.Recordset
If Index = 0 Then '控件数组的确定
If Text1.Text <> "" Then
strSQL = "select * from cxb where cx='" & Text1.Text & "'"
Set mrc = ExecuteSQL(strSQL, "")
If mrc.RecordCount > 0 Then
MsgBox "此车型已经有了~,无须再加入!"
mrc.Close
Exit Sub
Else
strSQL = "select * from cxb"
Set mrc = ExecuteSQL(strSQL, "")
If mrc.RecordCount > 0 Then
mrc.AddNew
mrc.Fields("cx") = Trim(Text1.Text)
mrc.Update
End If
mrc.Close
'此处如果加入msgbox 延时就能正确刷新 行100
InitList
End If
End If
End If
End Sub
Private Sub Form_Load()
InitList
End Sub
Private Sub InitList()
Dim i As Integer
Dim strSQL As String
List1.Clear
Dim mrc2 As ADODB.Recordset
strSQL = "select cx from cxb"
Set mrc2 = ExecuteSQL(strSQL, "")
If mrc2.RecordCount > 0 Then
For i = 0 To mrc2.RecordCount - 1
List1.AddItem mrc2.Fields("cx")
mrc2.MoveNext
Next
End If
mrc2.Close
End Sub
'执行指定的SQL语句,返回结果集
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
'连接Access数据库
Dim ConnectACString As String
Dim rst As ADODB.Recordset
Dim sTokens() As String
sTokens = Split(SQL)
ConnectACString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\lsgx.mdb;Persist Security Info=False"
cnn.Open ConnectACString
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & " query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & " 条记录 "
End If
'cnn.Close
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & Err.Description
Resume ExecuteSQL_Exit
End Function
list总是少显示最后一个加入的。如果在标号为 行100 处加入一个MSGBOX就可以同步及时刷新 新加入的记录。
其中:executesql 是个自定义执行SQL的函数。
Private Sub Command1_Click(Index As Integer)
If Index = 1 Then Unload Me '控件数组的取消
Dim strSQL As String
Dim mrc As ADODB.Recordset
If Index = 0 Then '控件数组的确定
If Text1.Text <> "" Then
strSQL = "select * from cxb where cx='" & Text1.Text & "'"
Set mrc = ExecuteSQL(strSQL, "")
If mrc.RecordCount > 0 Then
MsgBox "此车型已经有了~,无须再加入!"
mrc.Close
Exit Sub
Else
strSQL = "select * from cxb"
Set mrc = ExecuteSQL(strSQL, "")
If mrc.RecordCount > 0 Then
mrc.AddNew
mrc.Fields("cx") = Trim(Text1.Text)
mrc.Update
End If
mrc.Close
'此处如果加入msgbox 延时就能正确刷新 行100
InitList
End If
End If
End If
End Sub
Private Sub Form_Load()
InitList
End Sub
Private Sub InitList()
Dim i As Integer
Dim strSQL As String
List1.Clear
Dim mrc2 As ADODB.Recordset
strSQL = "select cx from cxb"
Set mrc2 = ExecuteSQL(strSQL, "")
If mrc2.RecordCount > 0 Then
For i = 0 To mrc2.RecordCount - 1
List1.AddItem mrc2.Fields("cx")
mrc2.MoveNext
Next
End If
mrc2.Close
End Sub
'执行指定的SQL语句,返回结果集
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
'连接Access数据库
Dim ConnectACString As String
Dim rst As ADODB.Recordset
Dim sTokens() As String
sTokens = Split(SQL)
ConnectACString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\lsgx.mdb;Persist Security Info=False"
cnn.Open ConnectACString
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & " query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & " 条记录 "
End If
'cnn.Close
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & Err.Description
Resume ExecuteSQL_Exit
End Function