Results 1 to 3 of 3
  1. #1
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38

    Code runs in break/debug mode but not in normal mode

    Hi all,
    Wondering if any of you have seen this before and if you have any suggestions...it's driving me nuts.

    I have code behind an "Ok" button (cmdOK_Click) on a form that simply saves the record if the form is dirty then closes the form. If the data validation check (behind the Form_BeforeUpdate procedure) cancels the save, an error (3021) is generated, which I trap and ignore in the cmdOK_Click procedure. This works fine when I step through the code in debug mode, but when I run the code normally (i.e., without any break points set), I get different errors (2455, 3075), seemingly at random. Why would the code behave differently when in debug mode vs when running in "normal" mode? Code below.

    Thanks,
    Dean

    Code:
    Private Sub cmdOK_Click()
        On Error GoTo ErrorHandler
        bOkClicked = True
        If Me.Dirty Then
            Me.Dirty = False
        End If
        Call CloseForm
    
    
    ExitHere:
        Exit Sub
    ErrorHandler:
        Select Case Err.Number
            Case 3021 'Save canceled by data validation code
                bOkClicked = False
                Resume ExitHere
            Case Else
                MsgBox Err.Number & ": " & Err.Description, vbExclamation
                Call LogError("frmProjectNew.cmdOK_Click", Err.Number, Err.Description)
                Resume ExitHere
        End Select
    End Sub
    
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Dim varReturn As Variant
        
        'Data validation code here
        Select Case True
            Case IsNull(Me.Title) Or Len(Me.Title) = 0
                MsgBox "You must specify a project title before saving.", vbExclamation + vbOKOnly
                Cancel = True
                Me.Title.SetFocus
                Exit Sub
            Case IsNull(Me.PrimaryWorkTypeID)
                MsgBox "You must specify a primary work type before saving.", vbExclamation + vbOKOnly
                Cancel = True
                Me.PrimaryWorkTypeID.SetFocus
                Exit Sub
        End Select
        'Check if form is dirty and prompt to save changes
        If Me.Dirty And Not bOkClicked Then
            varReturn = MsgBox("Do you want to save your changes?", vbExclamation + vbYesNoCancel)
            If varReturn = vbNo Then
                Me.Undo
                Exit Sub
            ElseIf varReturn = vbCancel Then
                Cancel = True
                Exit Sub
            End If
        End If
        
        'Log changes
        Call AuditTrail_Update(Me, Me.Controls(conPrimaryKey).Value)
    End Sub


  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Maybe is a timing issue. Stepping through in debug slows down the process - actions have a chance to finish before next action started. Try placing a DoEvents command in crucial locations. Example from my db:
    Code:
            CreateObject("Scripting.FileSystemObject").CopyFile _
                gstrBasePath & "Program\Install\MaterialsDatabase.accdb", "c:\", True
            'allow enough time for file to completely copy before opening
            Dim Start As Double
            Start = Timer
            While Timer < Start + 5
                DoEvents
            Wend
    I'm just not sure where to place code. Maybe after calling the AuditTrail_Update procedure
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38
    Thanks, June7. I had inserted a DoEvents command (by itself) into the code with no success, but I like your construct of using the Timer to slow things down even more. As it is, I ended up modifying my code to perform the data validation step before attempting to save, which avoids the problem altogether. I've run into this same issue a couple of times now, however, so will keep your Timer code in mind if I run into it again.

    Thanks!
    Dean

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

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2014, 05:48 PM
  2. Replies: 4
    Last Post: 11-26-2013, 10:47 AM
  3. Replies: 2
    Last Post: 09-01-2011, 10:48 PM
  4. Replies: 5
    Last Post: 08-29-2011, 04:06 PM
  5. Replies: 4
    Last Post: 01-14-2011, 10:37 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