匹配的数据!"
End If
Set ufPos = Nothing
End Sub
在用户窗体模块中,声明一些模块级的变量来包含传递的区域以及当前显示的区域。
Private mrAllFound As Range Private mrCurrent As Range Property Set AllFound(RHS As Range) Set mrAllFound = RHS End Property |
在显示用户窗体之前,要初始化该窗体,使用查找到的第一条记录填充窗体中的相应控件。
Public Sub Initialize() ''设置当前记录为第一条记录 If Not mrAllFound Is Nothing Then Set mrCurrent = mrAllFound(1) Me.txtName.Text = mrCurrent.Value Me.txtWork.Text = mrCurrent.Next.Value Me.txtY.Text = mrAllFound.Cells.Count Me.txtX.Text = 1 End If End Sub |
前一条按钮和后一条按钮使用FindPrevious方法和FindNext方法将记录移动到合适的位置。
Private Sub cmdNext_Click() ''设置当前单元格 Set mrCurrent = mrAllFound.FindNext(mrCurrent) Me.txtName.Text = mrCurrent.Value Me.txtWork.Text = mrCurrent.Next.Value ''增加计数器值 Me.txtX.Text = Me.txtX.Text + 1 End Sub Private Sub cmdPrev_Click() Set mrCurrent = mrAllFound.FindPrevious(mrCurrent) Me.txtName.Text = mrCurrent.Value Me.txtWork.Text = mrCurrent.Next.Value Me.txtX.Text = Me.txtX.Text - 1 End Sub |
最后,当第几条文本框中的值变化时,启用或禁用按钮使得用户不能试图到达不存在的记录。
Private Sub txtX_Change() ''启用/禁用按钮 If Me.txtX.Text = 1 Then Me.cmdPrev.Enabled = False Else Me.cmdPrev.Enabled = True End If If Me.txtX.Text = Me.txtY.Text Then Me.cmdNext.Enabled = False Else Me.cmdNext.Enabled = True End If End Sub |