Preventing Record Advances from the MouseWheel event

K

Kelly Ford

I needed a method of preventing record advances with the MouseWheel event
without using external DLL's (too much work for a distributed app) so I
developed a bit of code. It prevents a record advance when in a new record
or a changed record. On existing records with no changes it cannot cancel
the move but instead jumps back to the previous record so there is a slight
flicker effect on the screen controls.

Hope someone finds this helpful.

' 1)Set two global variables on the form
Dim blScrolled As Boolean
Dim lngID As Long 'this data type should match the key field
type of your forms recordset

'2)Add code to the MouseWheel event
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
If IsNull(Me.RecordID) Or Me.RecordID = 0 Then 'change this somewhat
if key field in recordset is not numeric, replace RecordID with field name
of key field of recordset
blScrolled = False
Else
blScrolled = True
lngID = Me.RecordID 'again, replace "RecordID" with key
field name
End If
End Sub

'3)Add code to Current Event - this saves the record identifier and
immediately jumps back to it
Private Sub Form_Current()
If blScrolled = True Then
Dim rst As Object
Set rst = Me.RecordsetClone
rst.FindFirst "RecordID = " & lngID 'modify if non-numeric
key field
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
rst.Close
blScrolled = False
Exit Sub
End If
End Sub

'4)Add code to BeforeUpdate Event - this prevents record advances for NEW
records
Private Sub Form_BeforeUpdate(Cancel As Integer)
If blScrolled = True Then
Cancel = True
blScrolled = False
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top