Results 1 to 9 of 9
  1. #1
    randle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    7

    Scrolling a text box with the mouse wheel

    Hi,

    I'm looking to make a text box scrollable using the mouse wheel.



    I know this question is often asked and have found the response is usually "not easily" but I'm half-way there with the following code:

    Code:
    Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
       If Count > 0 Then
          '~~> Replace Textxx with the name of your textbox
          If Screen.ActiveControl.Name = "Text34" Then
              If InStr(Me.Text34, Chr(13)) + 2 < Len(Me.Text34) Then
                  Me.Text34.SelStart = Me.Text34.SelStart + InStr(Me.Text34, Chr(13)) + 2
              End If
          End If
       End If
    End Sub
    This only scrolls down however so how do I add the functionality to scroll up as well? I've tried a few things that I thought would be obvious but ended up having the same effect.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Perhaps something like

    If Count > 0 Then
    MsgBox "Down"
    ElseIf Count < 0 Then
    MsgBox "Up"
    End If

  3. #3
    randle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    7
    Thanks ItsMe however, I was previously using
    Code:
    Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
    If Count < 0 Then
    SendKeys "{UP 16}"
    Else
    SendKeys "{DOWN 16}"
    End If
    
    End Sub
    which is pretty much the same as what you provide with SendKeys rather than just a MsgBox and works fine but this plays havoc with the numlock key and is why I was looking at alternative code. The original code works fine without any side effects but just need to amend it to go in both directions which I'm sure must be possible if one direction can be achieved!?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Well, I do not understand what this is doing.
    InStr(Me.Text34, Chr(13)) + 2

    I would suggest using InStr(Me.Text34, vbcrlf) - 2 to get the opposite effect but I do not see how it is working or what function it is providing to you in the first place. Did not test it though...

    In other words, I do not see how your code is analyzing where the cursor is at a given time. If it works then replace the + operators with -

    The way I see it, you need to first determine where the cursor is and analyze that. The code you provided seems to only analyze the position of a carriage return.

  5. #5
    randle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    7
    Me neither but it appears to function as I want in one direction and thought it must be possible to include both!

    Changing the code as you mention makes no change but still performs as before. The Text34 entry is obviously my text box but that's all I can place. I thought the '+ number' would determine the number of lines to scroll by for each increment but doesn't look to be the case.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I tested this and it worked for me. You could probably do a better job of scrolling by refactoring your Instr function. Maybe getting an absolute position vs. a carriage return.



    Code:
       If Count > 0 Then
          '~~> Replace Textxx with the name of your textbox
    '      If Screen.ActiveControl.Name = "txtSelText" Then
              If InStr(Me.txtSelText, vbCrLf) + 2 < Len(Me.txtSelText) Then
                  Me.txtSelText.SelStart = Me.txtSelText.SelStart + InStr(Me.txtSelText, vbCrLf) + 2
              End If
    Else
            
    '        If Screen.ActiveControl.Name = "txtSelText" Then
              If InStr(Me.txtSelText, vbCrLf) + 1 < Len(Me.txtSelText) Then
                  Me.txtSelText.SelStart = Me.txtSelText.SelStart - InStr(Me.txtSelText, vbCrLf) - 1
              End If
            
            
            
            
            
    '      End If
       End If

  7. #7
    randle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    7
    You are a star. Thanks very much. I appreciate you taking the time to test this out.

    I do get a 'run-time error '2101' The setting you entered isn't valid for this property' error when I scroll right to the top however. Doesn't happen when scrolling right down though for some reason. Any ideas?

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Not sure, I commented out some of the If Then statements for testing purposes. Might need to put something there so the code does not try to execute the impossible. Maybe check if you are at the begining. Might have something to do with looking for a -1 position.

  9. #9
    randle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    7
    Ok thanks for that. Strange how it doesn't error when you continue scrolling at the bottom of the field. My knowledge of VBA is a little slim so adding code for checking or troubleshooting is a little limited at the moment.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Mouse wheel changing record
    By ashu.doc in forum Forms
    Replies: 5
    Last Post: 07-11-2013, 06:49 PM
  2. Replies: 3
    Last Post: 08-22-2012, 12:49 PM
  3. mouse wheel event
    By blueraincoat in forum Access
    Replies: 0
    Last Post: 03-30-2011, 12:42 AM
  4. How do I enable the mouse wheel in forms
    By Vic Blake in forum Forms
    Replies: 4
    Last Post: 02-22-2011, 09:23 AM
  5. Mouse Wheel Off
    By Terrie in forum Forms
    Replies: 3
    Last Post: 02-10-2011, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums