“修改窗体”刚打开时能显示完整的记录,但是一运行“查询”以后,再回到“修改”窗体时,修改里面的记录竟然只有上个“查询”窗体查询的结果了。如下图:
“修改窗体”的代码
Sub RefreshGrid()
DataGrid1.DataMember = ""
DataGrid1.Refresh
Dataenv.rsdepartment.Requery
DataGrid1.DataMember = "department"
DataGrid1.Refresh
End Sub
Private Sub Cmdcancel_Click()
Dataenv.rsdepartment.CancelUpdate
DataGrid1.Refresh
DataGrid1.AllowAddNew = False
DataGrid1.AllowUpdate = False
cmdmodify.Enabled = True
cmddel.Enabled = True
cmdcancel.Enabled = False
cmdupdate.Enabled = False
Unload Me
End Sub
Private Sub cmddel_Click()
On Error GoTo errHandler
If MsgBox("要删除记录?", vbYesNo + vbQuestion + vbDefaultButton2, "确认") = vbYes Then
''通过在DataEnv.Con中执行SQL命令,来删除记录
Dataenv.con.Execute "delete from department where id =" & DataGrid1.Columns(0).CellText(DataGrid1.Bookmark) & " "
Dataenv.rsdepartment.MoveNext
If Dataenv.rsdepartment.EOF Then Dataenv.rsdepartment.MoveLast
''刷新用户导航的网格控件
Call RefreshGrid
End If
Exit Sub
errHandler:
MsgBox Err.Description, vbCritical, "错误"
End Sub
Private Sub Cmdmodify_Click()
Dim answer As String
On Error GoTo cmdmodify
cmddel.Enabled = False
cmdmodify.Enabled = False
cmdupdate.Enabled = True
cmdcancel.Enabled = True
DataGrid1.AllowUpdate = True
cmdmodify:
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub
Private Sub Cmdupdate_Click()
If Not IsNull(DataGrid1.Bookmark) Then
Dataenv.rsdepartment.Update
End If
cmdmodify.Enabled = True
cmddel.Enabled = True
cmdcancel.Enabled = True
cmdupdate.Enabled = False
DataGrid1.AllowUpdate = True
MsgBox "修改成功!", vbOKOnly + vbExclamation, ""
End Sub
Private Sub Form_Load()
On Error GoTo loaderror
cmdupdate.Enabled = False
'设定datagrid控件属性
DataGrid1.AllowAddNew = False '不可增加
DataGrid1.AllowDelete = False '不可删除
DataGrid1.AllowUpdate = False
Exit Sub
loaderror:
MsgBox Err.Description
End Sub
查询窗体的代码
Sub RefreshGrid()
DataGrid1.DataMember = ""
DataGrid1.Refresh
If Dataenv.rsdepartment.State <> adStateClosed Then Dataenv.rsdepartment.Close
Dataenv.rsdepartment.Open
DataGrid1.DataMember = "department"
DataGrid1.Refresh
End Sub
Private Sub Command1_Click()
Dim rsdep As New Recordset
Dim sql As String
sql = "select * from department where "
If Check1.Value = 1 Or Check2.Value = 1 Then
If Check1.Value = 1 And Check2.Value = 1 Then
sql = sql & "id= " & Trim(Text1.Text) & " " & "and" & " name = '" & Trim(cbodep.Text) & "'"
Else
If Check1.Value = 1 Then
sql = sql & "id=" & Text1.Text & " "
End If
If Check2.Value = 1 Then
sql = sql & "name='" & cbodep.Text & "'"
End If
End If
End If
With Dataenv.rsdepartment
If .State = adStateOpen Then .Close
.Open sql, Dataenv.con
End With
Call RefreshGrid
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Text1.Text = Empty
cbodep.Text = Empty
With Dataenv.rsdepartment
cbodep.Clear
If .State <> adStateOpen Then .Open
.MoveFirst
While Not .EOF
cbodep.AddItem Dataenv.rsdepartment("Name")
.MoveNext
Wend
End With
End Sub
[此贴子已经被作者于2006-5-20 22:00:19编辑过]