关于数据库纪录自动刷新的问题
我程序里设计了两个窗体,其中一个是纪录展示窗体f1,另一个是添加纪录的窗体f2,有没有办法在我添加完j纪录,关闭f2后,怎样实现f1中的dataggrid控件自动更新,显示出我刚刚添加的纪录呢?下面的程序是f1的,f2的程序很简单,最简单的那种向数据库中添加纪录的程序。
Option Explicit
Dim conn As ADODB.Connection
Dim lCurrentPage As Long
Dim adoPrimaryRS As ADODB.Recordset
Dim lPageCount As Long
Dim nPageSize As Integer
Dim lCount As Long
Private Sub C1_Click()
lCurrentPage = C1.Text
Call Loadcontrol(lCurrentPage)
End Sub
Private Sub cmdNext_Click()
lCurrentPage = lCurrentPage + 1
Call Loadcontrol(lCurrentPage)
End Sub
Private Sub cmdPrevious_Click()
If lCurrentPage > 1 Then
lCurrentPage = lCurrentPage - 1
Call Loadcontrol(lCurrentPage)
End If
End Sub
Private Sub first_Click()
lCurrentPage = 1
Call Loadcontrol(lCurrentPage)
End Sub
Private Sub Form_Load()
Dim i As Integer
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\net1.mdb; "
lCurrentPage = 1
nPageSize = 30
Set adoPrimaryRS = New ADODB.Recordset
adoPrimaryRS.Open "select * from ask ", conn, 1, 1
adoPrimaryRS.PageSize = nPageSize
'页数
lPageCount = adoPrimaryRS.PageCount
If lCurrentPage > lPageCount Then
lCurrentPage = lPageCount
End If
For i = 1 To lPageCount
C1.AddItem i
Next
Call Loadcontrol(lCurrentPage)
End Sub
Private Sub Loadcontrol(lPage As Long)
adoPrimaryRS.AbsolutePage = lCurrentPage
'定义另一个记录集
Dim objrs As New ADODB.Recordset
'添加字段名称
For lCount = 0 To adoPrimaryRS.Fields.Count - 1
objrs.Fields.Append adoPrimaryRS.Fields(lCount).Name, adVarChar, adoPrimaryRS.Fields(lCount).DefinedSize
Next
'打开记录集
objrs.Open
'将指定记录数循环添加到objrs中
For lCount = 1 To nPageSize
If adoPrimaryRS.EOF Or adoPrimaryRS.BOF Then
Exit For
Else
objrs.AddNew
objrs!姓名 = adoPrimaryRS!姓名
objrs!密码 = adoPrimaryRS!密码 & " "
adoPrimaryRS.MoveNext
End If
Next
'绑定
Set DataGrid1.DataSource = objrs
'在文本框显示页数
txtPage = lPage & "/ " & adoPrimaryRS.PageCount
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not conn Is Nothing Then
conn.Close
End If
Set conn = Nothing
End Sub
Private Sub last_Click()
lCurrentPage = adoPrimaryRS.PageCount
Call Loadcontrol(lCurrentPage)
End Sub
Private Sub txtPage_KeyDown(KeyCode As Integer, Shift As Integer)
lCurrentPage = Val(txtPage.Text)
Call Loadcontrol(lCurrentPage)
End Sub
[解决办法]
If adoPrimaryRS.EOF Or adoPrimaryRS.BOF Then
Exit For
Else
objrs.AddNew
objrs!姓名 = adoPrimaryRS!姓名
objrs!密码 = adoPrimaryRS!密码 & " "
adoPrimaryRS.Update
adoPrimaryRS.MoveNext
End If