Results 1 to 10 of 10
  1. #1
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211

    Access Crashes when I put Break Point in code (Access 2016)

    I'm having a very strange problem.

    My Access database works just fine, but I notice that when I put a "Stop" in the code (in order to debug new modifications), seconds after the code stops, access just crashes, and immediately offers to backup my database. If I re-open the database, then try the same operation after putting in a Stop command, the same thing happens.

    It's not trying to do something illegal or anything, the code is just "sitting there" with the next statement highlighted as expected if you put in a break point or a Stop command.



    It's kind of important, because I want to debug my changes, but can't. I've gotten around this in the past by not stopping, but putting in a Debug.Print statement and an "Exit Sub" or "Exit Function" statement, then saving my code and running the functionality again, no errors occur. I then go into code window and see what's in the immediate window area.

    I can then move these statements further down in the code, then rinse and repeat. This works fine.

    As you can imagine, this is a quite cumbersome way to debug changes. Is anyone aware of this situation? Why does Access crash if I stop the code, but not if I don't. This seems like a serious and hard-to-understand problem.

    This issue is not dependent on what Sub or Function i'm executing.

    Thanks...

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    can you post a copy of the db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I tended to use breakpoints as well?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Thanks for answering my post.

    I am trying to figure out how I can cleanse my data in order to send a copy of the database.

    Meantime, I have identified where the issue is occurring, and can turn the crashing on or off depending on a setting. To explain:


    • One of my forms is used to import data (example: new customers) from an Excel workbook.
    • The workbook/worksheet is imported to a work table.
    • My form has a subform which displays the work table records.
    • My form also has a subform which displays the current customer table. This is so I can see if a customer to be imported (on the first subform) is already a customer (as found in the second subform).
    • I have code to sync the 2 subforms, so if I scroll the first subform horizontally (the subforms are pretty wide), the second subform will also scroll horizontally to the same position. This code is a re-creation of some old Stephen Lebans code to do this functionality, which worked up to Access 2003, but not in Access 2007 and beyond. I got it to work properly, but it seems to cause issues when I break point the code, probably because it is using code timers to cause the syncing, and the timers don't stop just because I stopped the code.


    I'm sharing this information, because now I can create a sample database with the proper code classes which perform the syncing, and fill it with dummy data to upload to this forum. Meantime, I realize that I can turn off this functionality temporarily (by setting a Constant) when I need to do some debugging.

    I will try to create the sample database shortly, and include the code which is causing the issue.

    Meantime, if anyone has any ideas based on this new information, I'd be so pleased if you could shed some light on the problem.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I'm not sure if this is any different to your code - but worth mentioning:
    https://codekabinett.com/page.php?Th...ollBars-Sample

    Link taken from here
    https://www.access-programmers.co.uk.../#post-1626539
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by RMittelman View Post
    Thanks for answering my post.

    I am trying to figure out how I can cleanse my data in order to send a copy of the database.

    Meantime, I have identified where the issue is occurring, and can turn the crashing on or off depending on a setting. To explain:


    • One of my forms is used to import data (example: new customers) from an Excel workbook.
    • The workbook/worksheet is imported to a work table.
    • My form has a subform which displays the work table records.
    • My form also has a subform which displays the current customer table. This is so I can see if a customer to be imported (on the first subform) is already a customer (as found in the second subform).
    • I have code to sync the 2 subforms, so if I scroll the first subform horizontally (the subforms are pretty wide), the second subform will also scroll horizontally to the same position. This code is a re-creation of some old Stephen Lebans code to do this functionality, which worked up to Access 2003, but not in Access 2007 and beyond. I got it to work properly, but it seems to cause issues when I break point the code, probably because it is using code timers to cause the syncing, and the timers don't stop just because I stopped the code.


    I'm sharing this information, because now I can create a sample database with the proper code classes which perform the syncing, and fill it with dummy data to upload to this forum. Meantime, I realize that I can turn off this functionality temporarily (by setting a Constant) when I need to do some debugging.

    I will try to create the sample database shortly, and include the code which is causing the issue.

    Meantime, if anyone has any ideas based on this new information, I'd be so pleased if you could shed some light on the problem.
    See this thread for hiding your data.
    https://www.accessforums.net/showthr...ight=randomize
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by Welshgasman View Post
    See this thread for hiding your data.
    https://www.accessforums.net/showthr...ight=randomize
    Thanks Welshgasman. That's a good idea to use ongoing...

  9. #9
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Ok, I found the problem. Somehow my clsSyncScrollbars class was crashing whenever my code hit a break point, and causing Access to crash. I put a compile constant in my form code, and surrounded anything calling my class with #If's. Setting the constant to False allowed me to run the code with break points without crashing.

    Minty: Thanks for the kodekabinett link. I tried that, but it wouldn't work on my 64-bit Access. I tried to convert it to 64-bit using the updated APIs, but still couldn't get it to properly work. What I DID see in the code, however, was that he was activating a form timer in the master form using code in the class. This was a good find. I removed the Timer class I was using, which used API timers, and switched to the form timer technique, and it works great, even if I have break points in my code.

    My original code was self-contained in a class, so all I needed was to fix the class and use the kodekabinett method of declaring the form WithEvents and processing the timer event right in the class. Following is the entire class code, including how to declare and use it in the comments section at the top.

    I haven't yet gotten it to synchronize 2 seperate forms like the kodekabinett article, but only 2 subforms on a main form. It should be fairly easy to make this change, though...

    Code:
    Option Compare Database
    Option Explicit
    
    
    'Module     : clsSyncScrollbars
    'Created by : RMittelman@gmail.com
    '               Recreated from Stephen Lebans' code in GetSetScrollbars.mdb project,
    '               updated and simplified for 64-bit Access 2016
    '
    'Purpose    : Gets / sets a form's vertical or horizontal scrollbar positions, 
    '                  or synchronizes 2 different SubForms' scrollbar positions.
    '
    'History    :     6/25/2022   Initial release.
    '                 6/26/2022   Added ability to sync scrollbar positions between 2 scrollbars.
    '                 6/29/2022   Added clsTimer so this class can auto-sync without form timer.
    '                 12/14/2022  Removed clsTimer. Now uses a form timer to prevent crashing during debugging
    '
    'Notes      : - Typically used for a continuous SubForm in a main form, but can be also used
    '               on main form without a SubForm.
    '             - For syncing scrollbars, requires having 2 SubForms on your form,
    '               each SubForm typically configured as continuous form.
    '             - Requires setting the MasterForm property of the class.
    '             - If syncing, also requires setting the SlaveForm property.
    '             - Requires modSyncScrollbars
    '             - Requires clsTimer, modTimer
    '
    '             - Created as a class rather than a module, because there are no events triggered
    '               on Access Forms when scrollbar position is changed.
    '             - Requires clsTimer to periodically test if scrollbar positions changed.
    '
    'Usage      :  At top of form:
    '              Dim ss As clsSyncScrollbars
    '              Public masterForm As Form
    '              Public slaveForm  As Form
    '
    '              In Form_Close event:
    '              Set ss = Nothing
    '
    '              In Form_Load event:
    '
    '              ' so we don't have to continuously use the ".Form" property on subforms:
    '              Set masterForm = Me.SubformControl1.Form 'replace "SubformControl1" with actual name
    '              Set slaveForm = Me.SubformControl2.Form 'replace "SubformControl2" with actual name
    '
    '              With ss
    '
    '                  ' initial setup
    '                  Set .MasterForm = masterForm
    '                  Set .SlaveForm = slaveForm
    '
    '                  ' for getting Scrollbar positions:
    '                  ' (maybe put these in a button_click event?)
    '                  Me.txtHoriz = .GetScrollbarPos(frm, sbtHorizontal)
    '                  Me.txtVert = .GetScrollbarPos(frm, sbtVertical)
    '
    '                  ' for setting Scrollbar positions:
    '                  ' (maybe put these in a button_click event?)
    '                  .SetScrollbarPos frm, sbtHorizontal, Me.txtHoriz, newPosition
    '                  .SetScrollbarPos frm, sbtVertical, Me.txtVert, newPosition
    '
    '                  ' for syncing Scrollbars:
    '                  .SyncType = stHorizontal ' or stVertical or stBoth
    '                  ' - optional, default is stNone.
    '                  ' - if not stNone, requires both MasterForm & SlaveForm properties to be set.
    '                  ' - stHorizontal, stVertical, stBoth set which scrollbars will be sync'd.
    '
    '                  ' to manually sync Scrollbars:
    '                  isOK = .SyncScrollbars
    '
    '                  ' to Auto-Sync Scrollbars:
    '                  .AutoSyncInterval = 250
    '                  ' - timer interval in milliseconds for how often to sync
    '                  ' - set to > 0 to start auto-syncing
    '                  ' - set to 0 to stop auto-syncing
    '                  ' - optional, defaults to 0
    '
    '              End With
    
    
    Public Enum ScrollBarTypeEnum
        sbtHorizontal
        sbtVertical
    End Enum
    
    
    Public Enum SyncTypeEnum
        stNone
        stHorizontal
        stVertical
        stBoth
    End Enum
    
    
    Public Enum SyncDirectionEnum
        sdNone
        sdFirstToSecond
        sdSecondToFirst
    End Enum
    
    
    ' Scroll Bar Constants
    Private Const SB_CTL = 2
    'Private Const SB_HORZ = 0
    'Private Const SB_VERT = 1
    
    
    ' ScrollInfo fMask's
    Private Const SIF_RANGE = &H1
    Private Const SIF_PAGE = &H2
    Private Const SIF_POS = &H4
    Private Const SIF_DISABLENOSCROLL = &H8
    Private Const SIF_TRACKPOS = &H10
    Private Const SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
    
    
    ' windows message constants
    Private Const WM_VSCROLL = &H115
    Private Const WM_HSCROLL = &H114
    
    
    Private Const GWL_STYLE = (-16)
    Private Const GW_CHILD = 5
    Private Const GW_HWNDNEXT = 2
    
    
    ' scrollbar commands
    Private Const SB_THUMBPOSITION = 4
    
    
    'Public WithEvents oTimer    As clsTimer
    
    
    Private WithEvents m_MasterForm As Form
    Private m_SlaveForm             As Form
    'Private m_sourceForm            As Form
    'Private m_destForm              As Form
    
    
    Private m_syncDirection     As SyncDirectionEnum
    Private m_syncType          As SyncTypeEnum
    Private m_syncInterval      As Long
    
    
    Private oldHorizValue       As Long
    Private oldVertValue        As Long
    
    
    Private Type SCROLLINFO
        cbSize As Long
        fMask As Long
        nMin As Long
        nMax As Long
        nPage As Long
        nPos As Long
        nTrackPos As Long
    End Type
    
    
    Private Declare PtrSafe Function GetScrollInfo Lib "user32" (ByVal hWnd As LongPtr, ByVal n As Long, lpScrollInfo As SCROLLINFO) As Long
    
    
    '<Events>
    
    
    Private Sub m_MasterForm_Timer()
        SyncScrollbars
    End Sub
    
    
    '</Events>
    
    
    '<Properties>
    
    
    Public Property Get AutoSyncInterval() As Long
        AutoSyncInterval = m_syncInterval
    End Property
    
    
    Public Property Let AutoSyncInterval(vData As Long)
    
    
        ' verify prerequisites
        If m_MasterForm Is Nothing Then
            MsgBox "You must first set the MasterForm property.", vbExclamation, "Error"
            Exit Property
        End If
        If vData > 0 And m_SlaveForm Is Nothing Then
            MsgBox "You must first set the SlaveForm property.", vbExclamation, "Error"
            Exit Property
        End If
        If vData > 0 And m_syncType = stNone Then
            MsgBox "You must first set SyncType property", vbExclamation, "Error"
            Exit Property
        End If
        m_syncInterval = vData
        With m_MasterForm
            .OnTimer = "[Event Procedure]"
            .TimerInterval = m_syncInterval
        End With
    
    
    End Property
    
    
    Public Property Get SyncType() As SyncTypeEnum
        SyncType = m_syncType
    End Property
    
    
    Public Property Let SyncType(vData As SyncTypeEnum)
    
    
        If vData <> stNone Then
            If m_MasterForm Is Nothing Then
                MsgBox "You must first set the MasterForm property.", vbExclamation, "Error"
                Exit Property
            End If
            If m_SlaveForm Is Nothing Then
                MsgBox "You must first set the SlaveForm property.", vbExclamation, "Error"
                Exit Property
            End If
            If vData <> stHorizontal And vData <> stVertical And vData <> stBoth Then
                MsgBox "You must select either sdFirstToSecond, sdSecondToFirst, or asdBoth", vbExclamation, "Error"
                Exit Property
            End If
        End If
        m_syncType = vData
    
    
    End Property
    
    
    Public Property Get MasterForm() As Form
        Set MasterForm = m_MasterForm
    End Property
    
    
    Public Property Set MasterForm(ByVal vData As Form)
        Set m_MasterForm = vData
    End Property
    
    
    Public Property Get SlaveForm() As Form
        Set SlaveForm = m_SlaveForm
    End Property
    
    
    Public Property Set SlaveForm(ByVal vData As Form)
        Set m_SlaveForm = vData
    End Property
    
    
    '</Properties>
    
    
    '<Public Methods>
    
    
    Public Function GetScrollbarPos(frm As Form, scrollbarType As ScrollBarTypeEnum) As Long
    
    
        Dim hWndSB  As LongPtr
        Dim retVal  As Long
        Dim sInfo   As SCROLLINFO
        
        GetScrollbarPos = 0
        
        If Not frm Is Nothing Then
            
            sInfo.fMask = SIF_ALL
            sInfo.cbSize = Len(sInfo)
            sInfo.nPos = 0
            sInfo.nTrackPos = 0
            
            ' if scrollbar visible, get handle and continue
            hWndSB = get_scrollbar_handle(frm, scrollbarType)
            If hWndSB <> -1 Then
                retVal = GetScrollInfo(hWndSB, SB_CTL, sInfo)
                GetScrollbarPos = sInfo.nPos + 1
    '            Debug.Print "hWndSB:" & hWndSB & "  nPos:" & sInfo.nPos & "  nPage:" & sInfo.nPage & "  nMax:" & sInfo.nMax
            End If
            
        End If
    
    
    End Function
    
    
    Public Function SetScrollbarPos(frm As Form, scrollbarType As ScrollBarTypeEnum, newPosition As Long) As Boolean ' LongPtr
    
    
        Dim hWndForm    As LongPtr
        Dim hWndSB      As LongPtr
        Dim retVal      As LongPtr
        Dim thumb       As LongPtr
        Dim wMsg        As Long
        
        SetScrollbarPos = False
        
        If Not frm Is Nothing Then
            
            hWndForm = frm.hWnd
            
            ' if scrollbar visible, get handle and continue
            hWndSB = get_scrollbar_handle(frm, scrollbarType)
            If hWndSB <> -1 Then
                wMsg = IIf(scrollbarType = sbtHorizontal, WM_HSCROLL, WM_VSCROLL)
                thumb = make_dword(SB_THUMBPOSITION, CInt(newPosition - 1))
                retVal = SendMessage(hWndForm, wMsg, ByVal thumb, ByVal hWndSB)
                SetScrollbarPos = CBool(retVal) <> 0
            End If
            
        End If
    
    
    End Function
    
    
    Public Function SyncScrollbars() As Boolean
    
    
        Dim scrollPos   As Long
        Dim sbType      As ScrollBarTypeEnum
        
        SyncScrollbars = False
        
        ' verify prerequisites
        If m_MasterForm Is Nothing Then
            MsgBox "You must first set the MasterForm property.", vbExclamation, "Error"
            Exit Function
        End If
        If m_SlaveForm Is Nothing Then
            MsgBox "You must first set the SlaveForm property.", vbExclamation, "Error"
            Exit Function
        End If
        If m_syncType = stNone Then
            MsgBox "You must first set SyncType property", vbExclamation, "Error"
            Exit Function
        End If
        
        If m_syncType = stVertical Or m_syncType = stBoth Then
            sbType = sbtVertical
            scrollPos = GetScrollbarPos(m_MasterForm, sbType)
            SyncScrollbars = SetScrollbarPos(m_SlaveForm, sbType, scrollPos)
        End If
        If m_syncType = stHorizontal Or m_syncType = stBoth Then
            sbType = sbtHorizontal
            scrollPos = GetScrollbarPos(m_MasterForm, sbType)
            SyncScrollbars = SetScrollbarPos(m_SlaveForm, sbType, scrollPos)
        End If
    
    
    End Function
    
    
    '</Public Methods>
    
    
    '<Private Methods>
    
    
    Private Function get_class_name(hWnd As LongPtr) As String
    
    
        Const max_len   As Long = 255
        Dim sBuffer     As String
        Dim retLen      As Long
        
        get_class_name = ""
        sBuffer = Space$(max_len)
        retLen = GetClassName(hWnd, sBuffer, max_len)
        If retLen > 0 Then get_class_name = Left$(sBuffer, retLen)
    
    
    End Function
    
    
    Private Function get_scrollbar_handle(frm As Form, scrollbarType As ScrollBarTypeEnum) As LongPtr
    
    
        Const cls_name      As String = "NUIScrollbar"
        
        Dim hWndForm        As LongPtr
        
        Dim hWndSB          As LongPtr
    
    
        Dim sbStyle         As LongPtr
        Dim clsName         As String
        
        get_scrollbar_handle = -1
        
        If Not frm Is Nothing Then
            
            ' get form's first child window
            hWndForm = frm.hWnd
            hWndSB = GetWindow(hWndForm, GW_CHILD)
            
            ' loop thru each child window looking for scrollbars
            Do
                
                clsName = get_class_name(hWndSB)
                If clsName = cls_name Then
                    
                    ' scrollbar found, get style and verify it's the one we want
                    ' (horiz = 137573172, vert = 137573173)
                    #If Win64 Then
                        sbStyle = GetWindowLongPtr(hWndSB, GWL_STYLE)
                    #Else  'added by isladogs
                        sbStyle = GetWindowLong(hWndSB, GWL_STYLE)
                    #End If
                    If (sbStyle And scrollbarType) = scrollbarType Then
                        get_scrollbar_handle = hWndSB
                        Exit Do
                    End If
                End If
                hWndSB = GetWindow(hWndSB, GW_HWNDNEXT)
                
            Loop While hWndSB <> 0
            
        End If
    
    
    End Function
    
    
    Private Function make_dword(loword As Integer, hiword As Integer) As Long
        make_dword = (hiword * &H10000) Or (loword And &HFFFF&)
    End Function
    
    
    '</Private Methods>
    
    
    Private Sub Class_Initialize()
        m_syncType = sdNone
        m_syncInterval = 0
    End Sub
    
    
    Private Sub Class_Terminate()
        AutoSyncInterval = 0
        Set m_MasterForm = Nothing
        Set m_SlaveForm = Nothing
    End Sub

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Glad to have assisted in a small way.
    Good luck with the rest of your project.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. VBA break point not working
    By gg80 in forum Programming
    Replies: 4
    Last Post: 10-15-2019, 09:53 PM
  2. Replies: 7
    Last Post: 10-06-2019, 07:34 PM
  3. Replies: 9
    Last Post: 04-24-2018, 10:22 AM
  4. Replies: 1
    Last Post: 11-29-2017, 04:30 PM
  5. VBA code missing in ACCDE - Access 2016
    By pawjer in forum Access
    Replies: 9
    Last Post: 07-31-2017, 03:33 PM

Tags for this Thread

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