Hi Guys,
Ok I'm really stuck now ! Apologies in advance for the following wedge of code !
I have 4 navigation buttons, MoveFirst, MoveLast, MovePrev, MoveNext. I want to Enable/Disable the buttons where the user could not press the button: example pressing MovePrevious whilst the First record is select. However I also want to filter the records by field typ which can be A,B or C.
'(removed error handling to improve readability)
' ms access 97
private bFirst as boolean
' event procedures :
Private Sub cmdFirst_Click()
DoCmd.GoToRecord , , acFirst
End Sub
Private Sub cmdLast_Click()
DoCmd.GoToRecord , , acLast
End Sub
Private Sub cmdNext_Click()
DoCmd.GoToRecord , , acNext
End Sub
Private Sub cmdPrev_Click()
DoCmd.GoToRecord , , acPrevious
End Sub
Private Sub Form_Current()
If bFirst = False Then
' skip the first one on form_open
bFirst = True
Exit Sub
End If
CheckNavigation
End Sub
Private Sub Form_Open(Cancel As Integer)
Filter = "typ = 'A'"
FilterOn = True
End Sub
Private Sub Combo10_Click()
' select a new filter
Filter = "typ = '" & Trim$(Combo10.Value) & "'"
FilterOn = True
End Sub
' private functions ...
Private Function GetCurrentRecord() As Recordset
Dim rs As Recordset
On Error GoTo ErrorHandle
Set rs = Me.RecordsetClone
rs.Bookmark = Me.Bookmark
Set GetCurrentRecord = rs
Exit Function
ErrorHandle:
Set GetCurrentRecord = Nothing
End Function
Private Sub CheckNavigation()
Dim rsCurrent As Recordset, rs As Recordset
txtName.SetFocus
If IsNull(Combo10.Value) = True Then
' combo10 must be empty, assume A
Set rs = CurrentDb.OpenRecordset("select count(*) as NumRecords from Addresses where typ = 'A'", dbOpenSnapshot)
Else
Set rs = CurrentDb.OpenRecordset("select count(*) as NumRecord from Addresses where typ = '" & Trim$(Combo10.Value) & "'", dbOpenSnapshot)
End If
Set rsCurrent = GetCurrentRecord
If rsCurrent Is Nothing Then
cmdFirst.Enabled = False
cmdNext.Enabled = False
cmdPrev.Enabled = False
cmdLast.Enabled = False
Else
cmdFirst.Enabled = (rs!NumRecords > 0 And BOF(rsCurrent) = False)
cmdLast.Enabled = (rs!NumRecords > 0 And EOF(rsCurrent) = False)
cmdNext.Enabled = cmdLast.Enabled
cmdPrev.Enabled = cmdFirst.Enabled
End If
End Sub
Private Property Get EOF(rsCurrent As Recordset) As Boolean
' returns TRUE if the Recordset is on the last record
' NOT the same as Recordset.EOF
EOF = (rsCurrent.AbsolutePosition = rsCurrent.RecordCount - 1)
End Property
Private Property Get BOF(rsCurrent As Recordset) As Boolean
' returns TRUE if the Recordset is on the first record
' NOT the same as Recordset.BOF
BOF = (rsCurrent.AbsolutePosition = 0)
End Property
'
The problem being when I open the form all is well, the buttons are being greyed out at the correct record positions, BUT as soon as i select a value from the combo and the filter is invoked all the buttons become Disabled, unless i step through the code in which case it works ! I think it has something to do with the fact that the filter takes some time to process thus the checknavigation sub is not getting the right recordset.
Please Help.
Thanks in advance.
Ok I'm really stuck now ! Apologies in advance for the following wedge of code !
I have 4 navigation buttons, MoveFirst, MoveLast, MovePrev, MoveNext. I want to Enable/Disable the buttons where the user could not press the button: example pressing MovePrevious whilst the First record is select. However I also want to filter the records by field typ which can be A,B or C.
'(removed error handling to improve readability)
' ms access 97
private bFirst as boolean
' event procedures :
Private Sub cmdFirst_Click()
DoCmd.GoToRecord , , acFirst
End Sub
Private Sub cmdLast_Click()
DoCmd.GoToRecord , , acLast
End Sub
Private Sub cmdNext_Click()
DoCmd.GoToRecord , , acNext
End Sub
Private Sub cmdPrev_Click()
DoCmd.GoToRecord , , acPrevious
End Sub
Private Sub Form_Current()
If bFirst = False Then
' skip the first one on form_open
bFirst = True
Exit Sub
End If
CheckNavigation
End Sub
Private Sub Form_Open(Cancel As Integer)
Filter = "typ = 'A'"
FilterOn = True
End Sub
Private Sub Combo10_Click()
' select a new filter
Filter = "typ = '" & Trim$(Combo10.Value) & "'"
FilterOn = True
End Sub
' private functions ...
Private Function GetCurrentRecord() As Recordset
Dim rs As Recordset
On Error GoTo ErrorHandle
Set rs = Me.RecordsetClone
rs.Bookmark = Me.Bookmark
Set GetCurrentRecord = rs
Exit Function
ErrorHandle:
Set GetCurrentRecord = Nothing
End Function
Private Sub CheckNavigation()
Dim rsCurrent As Recordset, rs As Recordset
txtName.SetFocus
If IsNull(Combo10.Value) = True Then
' combo10 must be empty, assume A
Set rs = CurrentDb.OpenRecordset("select count(*) as NumRecords from Addresses where typ = 'A'", dbOpenSnapshot)
Else
Set rs = CurrentDb.OpenRecordset("select count(*) as NumRecord from Addresses where typ = '" & Trim$(Combo10.Value) & "'", dbOpenSnapshot)
End If
Set rsCurrent = GetCurrentRecord
If rsCurrent Is Nothing Then
cmdFirst.Enabled = False
cmdNext.Enabled = False
cmdPrev.Enabled = False
cmdLast.Enabled = False
Else
cmdFirst.Enabled = (rs!NumRecords > 0 And BOF(rsCurrent) = False)
cmdLast.Enabled = (rs!NumRecords > 0 And EOF(rsCurrent) = False)
cmdNext.Enabled = cmdLast.Enabled
cmdPrev.Enabled = cmdFirst.Enabled
End If
End Sub
Private Property Get EOF(rsCurrent As Recordset) As Boolean
' returns TRUE if the Recordset is on the last record
' NOT the same as Recordset.EOF
EOF = (rsCurrent.AbsolutePosition = rsCurrent.RecordCount - 1)
End Property
Private Property Get BOF(rsCurrent As Recordset) As Boolean
' returns TRUE if the Recordset is on the first record
' NOT the same as Recordset.BOF
BOF = (rsCurrent.AbsolutePosition = 0)
End Property
'
The problem being when I open the form all is well, the buttons are being greyed out at the correct record positions, BUT as soon as i select a value from the combo and the filter is invoked all the buttons become Disabled, unless i step through the code in which case it works ! I think it has something to do with the fact that the filter takes some time to process thus the checknavigation sub is not getting the right recordset.
Please Help.
Thanks in advance.