在命令VBA中将命令按钮事件与记录集集成

时间:2015-02-17 14:51:44

标签: vba ms-access-2007

我正在使用未绑定的表单,以便我可以在几个字段中输入/编辑数据(必须从绑定表单移出来处理这个)。我在表单上添加了prev,next,first和last按钮。如何将其与我显示第一条记录的FORM_OPEN事件集成。我不知道如何添加cmdLast_Click,cmdNext_Click,cmdFirst_Click,cmdBack_Click事件。任何帮助将不胜感激。

我当前的FORM_OPEN事件列在下面,只显示第一条记录。

Private Sub Form_Open(Cancel As Integer)

  sSQL = "SELECT TOP 1 Proc.ProcedureName, Proc.Manager,   Proc.AnnualApprovalDueDate, AprList.ApprovalDate " & _
"FROM Proc INNER JOIN (SELECT AprTrac.ProcedureId, AprTrac.ApprovalDate FROM AprTrac) " & _
"AS AprList ON Proc.ProcedureId=AprList.ProcedureId WHERE MONTH(Proc.AnnualApprovalDueDate)=MONTH(DATE())"

 Set db = CurrentDb
 Set rs = db.OpenRecordset(sSQL)

 Me.txtProcedureName = rs!ProcedureName
 Me.txtManagerName = rs!Manager
 Me.txtAppDueDate = rs!AnnualApprovalDueDate
 Me.txtAppDate = rs!ApprovalDate

rs.Close

' clear main variable values
Set rs = Nothing
Set db = Nothing
sSQL = ""

End Sub

这是我添加的cmdNext_Click按钮事件,但似乎没有用,特别是rs.movenext

Private Sub cmdNext_Click()

On Error Resume Next
rs.MoveNext

Debug.Print rs!ProcedureName
Me.txtProcedureName = rs!ProcedureName
Forms!frmProcedures!txtProcedureName = rs!ProcedureName
Debug.Print Forms!frmProcedures!txtProcedureName.Value
Me.txtManagerName = rs!Manager
Me.txtAppDueDate = rs!AnnualApprovalDueDate
Me.txtAppDate = rs!ApprovalDate

End Sub

2 个答案:

答案 0 :(得分:0)

如果你提到rs是全局的,那么你想在Form_Open事件结束时保持它是开放的。当您关闭记录集然后将其设置为空,然后尝试在按钮事件中稍后访问它时,您正在访问设置为空的rs。删除rs.close并设置rs = nothing lines。

执行此操作后,cmdNext_Click事件应该可以正常工作。作为警告,您应该检查记录集,而不是“rs.MoveNext”:

If rs.EOF Then
    MsgBox("You are already at the end of the recordset!")
Else
    rs.MoveNext
    'rest of code

End If

您应该同样检查cmdPrev_Click和其他事件。

此外,在form_open事件中,您应该选择要开始的记录,方法是将其设置为rs.MoveFirst或rs.MoveLast,具体取决于您要默认的值。您还应该进行一些检查以确保记录集不为空:

If rs.EOF and rs.BOF Then
    'Do not initialize fields
Else
    rs.MoveFirst 'or rs.MoveLast....
    'initialize textboxes 
End If

希望这有帮助。

答案 1 :(得分:0)

以下是我为此付出的努力。也许我在做它的时候我做错了什么。如果有任何建议可以提高此代码的效率,那么建议非常受欢迎

Private Sub Form_Open(Cancel As Integer)


Dim sSQL As String

sSQL = "SELECT Proc.ProcedureName, Proc.Manager, Proc.AnnualApprovalDueDate, AprList.ApprovalDate " & _
"FROM Proc INNER JOIN (SELECT AprTrac.ProcedureId, AprTrac.ApprovalDate FROM AprTrac) " & _
"AS AprList ON Proc.ProcedureId=AprList.ProcedureId WHERE MONTH(Proc.AnnualApprovalDueDate)=MONTH(DATE())"

Set cnn1 = New ADODB.Connection
mydb = "C:\Users\temp\Documents\Tasks.accdb"
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mydb
cnn1.Open strCnn

Set rs = New ADODB.Recordset
rs.Open sSQL, cnn1, adOpenKeyset, adLockOptimistic

rs.MoveLast    
rs.MoveFirst

MoveValues 'Just to pick up the values in the recordset and transfer them to the form

End Sub

Private Sub MoveValues()
With Me
    .txtProcedureName = rs("ProcedureName")
    .txtManagerName = rs("Manager")
    .txtAppDueDate = rs("AnnualApprovalDueDate")
    .txtAppDate = rs("ApprovalDate")
End With
End Sub

Private Sub cmdNext_Click()

On Error Resume Next
rs.MoveNext

cmdDummy.SetFocus

If rs.AbsolutePosition = rs.RecordCount Then
    Me.cmdNext.Enabled = False
    Me.cmdLast.Enabled = False
End If

Me.cmdFirst.Enabled = True
Me.cmdBack.Enabled = True

MoveValues
End Sub

Private Sub cmdBack_Click()

On Error Resume Next
rs.MovePrevious
cmdDummy.SetFocus

If rs.AbsolutePosition = 1 Then
    Me.cmdBack.Enabled = False
    Me.cmdFirst.Enabled = False
End If

Me.cmdNext.Enabled = True
Me.cmdLast.Enabled = True

MoveValues
End Sub

Private Sub cmdFirst_Click()

On Error Resume Next
rs.MoveFirst

cmdDummy.SetFocus

Me.cmdBack.Enabled = False
Me.cmdFirst.Enabled = False

Me.cmdNext.Enabled = True
Me.cmdLast.Enabled = True

MoveValues
End Sub

Private Sub cmdLast_Click()

On Error Resume Next
rs.MoveLast
cmdDummy.SetFocus

Me.cmdBack.Enabled = True
Me.cmdFirst.Enabled = True

Me.cmdNext.Enabled = False
Me.cmdLast.Enabled = False

MoveValues
End Sub