Results 1 to 15 of 15
  1. #1
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12

    Detecting if scrollbar is currently displayed

    Hi,


    I'm using the following code to detect whether a scrollbar (horizontal/vertical) is displayed in Access form/control:

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Const GWL_STYLE = (-16)
    Private Const WS_HSCROLL = &H100000
    Private Const WS_VSCROLL = &H200000
    
    
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
    
    Public Function IsHScrollVisible(Hwnd As Long) As Boolean
        Dim Style As Long
        Style = GetWindowLong(Hwnd, GWL_STYLE)
     
        If (Style And WS_HSCROLL) Then
            IsHScrollVisible = True
            Exit Function
        End If
     
    End Function
     
    
    
    Public Function IsVScrollVisible(Hwnd As Long) As Boolean
        Dim Style As Long
        Style = GetWindowLong(Hwnd, GWL_STYLE)
     
        If (Style And WS_VSCROLL) Then
            IsVScrollVisible = True
            Exit Function
        End If
     
    End Function
    Then, I'm calling the functions on a form with a button:

    Code:
    Private Sub Command1_Click()
    
    
    MsgBox IsVScrollVisible(Me.Hwnd)
    MsgBox IsHScrollVisible(Me.Hwnd)
    'MsgBox IsVScrollVisible(Forms!Form1.Hwnd) 'Direct pointer doesn't work either
    'MsgBox IsHScrollVisible(Forms!Form1.Hwnd) '
    
    End Sub
    Problem is, the result is always 'False', even if I shrink the form to display a scrollbar.
    I would appreciate an insight as to any errors in my approach, thank you.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I see you are using Access 2016 (uses 64 bit handlers and pointers) but the code is written for 32 bit. Just a guess, but maybe your issue is that you should be using GetWindowLongPtr.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by Micron View Post
    I see you are using Access 2016 (uses 64 bit handlers and pointers) but the code is written for 32 bit. Just a guess, but maybe your issue is that you should be using GetWindowLongPtr.
    Hmm, I'm writing on the assumption the code should run on 32bit machines.
    Wouldn't declaring 32bit DLL and pointers be not enough to assure 32bit execution? I'm running Access in 32bit too, but on Win10 x64. I'd rather not switch to 64bit; that would raise unnecessary compatibility issues when I reuse the code.

    Stepping through the functions, looks like the code is resolving just fine. Maybe constant declarations are wrong pointers?
    Code:
    Private Const WS_HSCROLL = &H100000
    Private Const WS_VSCROLL = &H200000
    My makeshift form looks like this:
    Click image for larger version. 

Name:	screenie.gif 
Views:	20 
Size:	27.2 KB 
ID:	24617

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Just curious--- why do you need to do this in code? I'm sure there is an explanation, but this is not a common need.

  5. #5
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by orange View Post
    Just curious--- why do you need to do this in code? I'm sure there is an explanation, but this is not a common need.
    Well, I'm probably OCD... Basically, for quite a few things I want to do in Access I will have some subforms on a main form. And when I populate those sub forms with controls, let's say text boxes, I want to resize them so that a vertical scrollbar doesn't cover their contents. I know, you'll say "just asume the scrollbar is always there", but my OCD...
    And second purpose is, in general, I have a code that resizes/rearranges controls (especially subform containers) on a main form as you resize the main form, or when you make the main form DoCmd.Maximize
    If this code worked, it'd let me use every inch of space on the form more effectively. Sorry for bein pain...

    NB, I've tried the code in Access2010 and it too gives always False as answer to the presence of scrollbars.

    The code from the first post is self contained, just put it in a module, assign a call to a Command1 button and step through. Maybe you'll see something I don't

  6. #6
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    I guess my issue is with 32/64 value conversions. Tried to rewrite the code with PtrSafe versions of Long etc, but no luck yet.

    So far it looks more like this

    Code:
    Option Compare Database
    Option Explicit
    
    Public Const GWL_STYLE = (-16)
    Public Const WS_HSCROLL = &H100000
    Public Const WS_VSCROLL = &H200000
    
    
    'Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long ' original version
    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As LongPtr) As LongPtr  ' no dice
    
    
    Public Function IsHScrollVisible(hwnd As Long) As Boolean
        Dim Style As LongPtr
        Style = GetWindowLong(hwnd, GWL_STYLE)  ' stepping here I get something like 3994356 and -16 for hwnd and GWL_Style
     
        If (Style And WS_HSCROLL) Then  ' stepping here I ver numbers like -1753456456 and 10453345 for Style and WS_HSCROLL
            IsHScrollVisible = True
            Exit Function
        End If
     
    End Function
     
    
    Public Function IsVScrollVisible(hwnd As Long) As Boolean
        Dim Style As LongPtr
        Style = GetWindowLong(hwnd, GWL_STYLE)
     
        If (Style And WS_VSCROLL) Then
            IsVScrollVisible = True
            Exit Function
        End If
     
    End Function
    This was some interesting read
    http://www.utteraccess.com/forum/ind...post&p=1914378

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Very interesting dialog indeed.
    Since you are interested in forms and making full use of available space, you may find this article of interest.
    I found it accidentally just recently.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I tried your code with the same results. Only if I reduced the value of the scroll value could I ever get either of the two comparisons (e.g. (Style And WS_VSCROLL)) to be true. This type of comparison AFAIK, is a bit-wise compare, which I don't see how it could (almost) ever be true, but I'm no expert on that. Since IsHScrollVisible and IsVScrollVisible appear to be methods of an Access form (check intellisense), I also suspect an issue with having a function with the same name as a method. In fact, I created a continuous loop in my attempts to figure it out (luckily I was always in break mode). I also see that most of the examples related to your code are based in C+ or vb.net - so there may not be a direct application of your posted code when using Access. However, this seems to work:
    Code:
    Dim bytBars As Byte
    
    bytBars = Me.Form.ScrollBars
    
      If bytBars = 0 Then
       MsgBox "There are currently no scrollbars detected."
        Else
       MsgBox "There is at least one scrollbar detected."
      End If
    Wish I could take credit for the code, especially if it works for you as it did for me, but I found it here:
    http://www.tek-tips.com/viewthread.cfm?qid=1258350
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    See if this helps https://msdn.microsoft.com/en-us/lib...ice.14%29.aspx I adjusted my TestMessage form as per the attached jpg.

    then in immediate window (form was open)
    ?forms("TestMessage").ScrollBars
    1

    The 1, as per the info in link above, indicates Horizontal scroll bar.

    My jpg shows form in Design view. I also tried this in Form view and it still returns 1.

    Good luck.
    Attached Thumbnails Attached Thumbnails ScrollBar.jpg  

  10. #10
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by orange View Post
    Very interesting dialog indeed.
    Since you are interested in forms and making full use of available space, you may find this article of interest.
    I found it accidentally just recently.
    Thank you Orange, and I'll return the favour by giving something I've found recently myself.
    The code is neat and not too hard to follow.
    http://www.databasejournal.com/featu...ike-Access.htm
    Main features you might enjoy looking at:
    - resizes 4 subforms to fill in the whole of the main form, resizes subform content too
    - Back and Forward buttons, webbrowser style
    - records presented as heperlink-fakes
    - neat solution at displaying each form's title and description (check out lblCaption, lblDescription and imgLogo formatting)

  11. #11
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by Micron View Post
    I tried your code with the same results. Only if I reduced the value of the scroll value could I ever get either of the two comparisons (e.g. (Style And WS_VSCROLL)) to be true. This type of comparison AFAIK, is a bit-wise compare, which I don't see how it could (almost) ever be true, but I'm no expert on that. Since IsHScrollVisible and IsVScrollVisible appear to be methods of an Access form (check intellisense), I also suspect an issue with having a function with the same name as a method. In fact, I created a continuous loop in my attempts to figure it out (luckily I was always in break mode). I also see that most of the examples related to your code are based in C+ or vb.net - so there may not be a direct application of your posted code when using Access. However, this seems to work:
    Code:
    Dim bytBars As Byte
    
    bytBars = Me.Form.ScrollBars
    
      If bytBars = 0 Then
       MsgBox "There are currently no scrollbars detected."
        Else
       MsgBox "There is at least one scrollbar detected."
      End If
    Wish I could take credit for the code, especially if it works for you as it did for me, but I found it here:
    http://www.tek-tips.com/viewthread.cfm?qid=1258350
    Thanks Micron, I'll play with your code tomorrow (zzz). I'm just beginning to go seriously at programming albeit been script kiddie for years, so please bear with my incompetence

    My api code was based on example showcased on Microsoft's page
    https://support.microsoft.com/en-us/kb/299686
    I guess Microsoft were convinced it would work, or maybe I've implemented it sloppily trying to port VB.

  12. #12
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by orange View Post
    See if this helps https://msdn.microsoft.com/en-us/lib...ice.14%29.aspx I adjusted my TestMessage form as per the attached jpg.

    then in immediate window (form was open)
    ?forms("TestMessage").ScrollBars
    1

    The 1, as per the info in link above, indicates Horizontal scroll bar.

    My jpg shows form in Design view. I also tried this in Form view and it still returns 1.

    Good luck.
    Thanks, I'm aware of that property, but I've just realised that I haven't checked if the result of ?forms("TestMessage").ScrollBars would show 0 if the scrollbar is not needed and not displayed ("Hey, at the moment I'm not displaying a scrollbar, but not gonna tell you whether I'm allowed to display one of need be"). My assumption was, it would always show 1 ("I'm capable of displaying a scrollbar, but not gonna tell you whether I'm displaying one right now or not") if the setting is as shown in your Property Sheet screenshot. That's something to play with tomorrow.

  13. #13
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I checked your link. VB / VBA often inter-compatible, but not always.
    Also of interest is that the page shows this comparison (wndStyle And WS_HSCROLL) <> 0 Then
    which is not a bit-wise comparison, but your code example is If (Style And WS_HSCROLL).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Just checked it by setting scrollbars on TestMessage form

    0 ---neither
    1-- horizontal
    2 --vertical
    3-- both

  15. #15
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Quote Originally Posted by Micron View Post
    I checked your link. VB / VBA often inter-compatible, but not always.
    Also of interest is that the page shows this comparison (wndStyle And WS_HSCROLL) <> 0 Then
    which is not a bit-wise comparison, but your code example is If (Style And WS_HSCROLL).
    It... evolved somehow I wanted to make a function that returns true/false state to be able to call it from other subs and the original code is just a sub that displays msgbox. I carved out most of the stuff from the original sub, but maybe will go back tomorrow and check if it works as is if I just plug it in. Good night ^^

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

Similar Threads

  1. Looking for Scrollbar in text box example
    By Middlemarch in forum Access
    Replies: 6
    Last Post: 10-26-2015, 05:29 PM
  2. Detecting Trailing Spaces in a nvarchar Field
    By EddieN1 in forum Queries
    Replies: 2
    Last Post: 10-29-2014, 11:22 AM
  3. Detecting a Before Update Cancel
    By EddieN1 in forum Programming
    Replies: 3
    Last Post: 06-09-2013, 08:04 PM
  4. Replies: 8
    Last Post: 04-07-2011, 05:52 AM
  5. detecting a null date
    By tedpottel in forum Queries
    Replies: 3
    Last Post: 03-02-2010, 01:45 PM

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