+ Reply to Thread
Results 1 to 5 of 5

Error not being handled, instead pops up error prompt

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2024
    Location
    USA
    MS-Off Ver
    Office 365 build 2404
    Posts
    3

    Question Error not being handled, instead pops up error prompt

    I am tying to raise an error under an error handler to allow for a DB transaction to be rolled back when an attempt to create duplicate model in the database happens. However, the error handler does not handle this error and instead shows me the error prompt. Please advise.

    Private Sub createModelButton_Click()
        'Create a new model object and set its properties
        Set currentModel = New Model
        currentModel.Barcode = UCase(targetBarCodeTextBox.Value)
        currentModel.InBuilding29 = building29CheckBox.Value
        currentModel.ModelNumber = UCase(targetModelTextBox.Value)
        currentModel.ParentID = sourceComboBox.List(sourceComboBox.ListIndex, 1)
        
        'Set the sequence number based on the selected option
        If seq61Option.Value Then
            currentModel.SequenceNumber = 61
        ElseIf seq102Option.Value Then
            currentModel.SequenceNumber = 102
        ElseIf seq103Option.Value Then
            currentModel.SequenceNumber = 103
        End If
        
        Dim dbConnection As ADODB.Connection
        Set dbConnection = getDBConnection
        
        'Begin a transaction
        dbConnection.BeginTrans
        On Error GoTo rollbackTransaction
        
        Dim matchingBarcodes As ADODB.Recordset
        Dim matchingModelNums As ADODB.Recordset
        
        Set matchingBarcodes = executeCommand(dbConnection, "select * from model where barcode='" & currentModel.Barcode & "'")
        Set matchingModelNums = executeCommand(dbConnection, "select * from model where modelnumber='" & currentModel.ModelNumber & "'")
        
        'If there is a matching barcode or modelnumber in the database then reject the new model
        If Not matchingBarcodes.EOF Or Not matchingModelNums.EOF Then
            Err.Raise vbObjectError + 513, "createModelButton_Click()", currentModel.Barcode & " - " & currentModel.ModelNumber & " already exists in the database"
        End If
    
        'Insert the new model and get its ID
        Dim newModelID As Long
        newModelID = insertNewModel(dbConnection, currentModel.Barcode, currentModel.ModelNumber, 1)
        
        'Get all model parameters
        Dim modelParameterRecordSet As ADODB.Recordset
        Set modelParameterRecordSet = getAllModelParameters(dbConnection)
        
        Dim modelParameterID As Variant
        Dim currentValue As Variant
        
        'Insert default parameter values for the new model
        If Not modelParameterRecordSet.EOF Then
            Do Until modelParameterRecordSet.EOF
                modelParameterID = modelParameterRecordSet.Fields("ModelParameter_ID").Value
                currentValue = modelParameterRecordSet.Fields("DefaultValue").Value
    
                insertModelParameterValue dbConnection, modelParameterID, newModelID, currentValue
    
                modelParameterRecordSet.MoveNext
            Loop
        End If
        
        modelParameterRecordSet.Close
        Set modelParameterRecordSet = Nothing
        
        'Copy parameter values from the parent model
        copyModelParameterValues dbConnection, newModelID, currentModel.ParentID
        
        'Copy limits from the parent model
        copyModelLimits dbConnection, currentModel.ParentID, newModelID, "Model Copy"
        
        'Commit the transaction
        dbConnection.CommitTrans
        
        'If the model is in Building 29, copy necessary files
        If currentModel.InBuilding29 Then
            
            'Check if the limits and sequences file already exist, if not then get the files
            If Dir(localLimits & "\Seq " & currentModel.SequenceNumber & "\" & currentModel.getLimitFileName) = vbNullString Then
                copyLocalFile tyqaLimits & "\Seq " & currentModel.SequenceNumber, localLimits & "\Seq " & currentModel.SequenceNumber, currentModel.getLimitFileName
            End If
            
            If Dir(localBase & "\ModelsSequences.csv") = vbNullString Then
                copyLocalFile tyqaSequences, localBase, "ModelsSequences.csv"
            End If
            
            Application.ScreenUpdating = False
            
            'Insert model into limits and sequence
            insertIntoLimits
            insertIntoSequence
            
            Application.ScreenUpdating = True
        End If
        
         'Set the model ID and get new model info
        currentModel.ID = newModelID
        getNewModelInfo currentModel.ID, currentModel.SequenceNumber
        
        dbConnection.Close
        Set dbConnection = Nothing
        
        Me.Hide
        
        Exit Sub
        
    rollbackTransaction:
        'Rollback the transaction in case of an error
        dbConnection.RollbackTrans
        dbConnection.Close
        Set dbConnection = Nothing
        
        MsgBox "Failed to create the new model" & vbCrLf & Err.description
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,008

    Re: Error not being handled, instead pops up error prompt

    Which line is generating the error?
    I tend to only begin a transaction before any changes are attempted with an additional boolean flag. Then in the error handler you use the flag to determine whether anything needs rolling back.

  3. #3
    Registered User
    Join Date
    07-01-2024
    Location
    USA
    MS-Off Ver
    Office 365 build 2404
    Posts
    3

    Re: Error not being handled, instead pops up error prompt

    Err.Raise vbObjectError + 513, "createModelButton_Click()", currentModel.Barcode & " - " & currentModel.ModelNumber & " already exists in the database"
    This is essentially right after I start the transaction.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Error not being handled, instead pops up error prompt

    Do you have your VBE options set to break on all errors?
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,008

    Re: Error not being handled, instead pops up error prompt

    So if you step through, does it jump to the error handler at that line?
    You could try leaving out the source parameter e.g.

    Err.Raise vbError,, currentModel.Barcode & " - " & currentModel.ModelNumber & " already exists in the database"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Error pops up in the code if sheet name is digits
    By pm.patel189 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-09-2018, 11:45 AM
  2. Error msg pops out when a button is clicked more than once
    By jlyh11 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-22-2015, 03:18 AM
  3. Prevent Loss of Data -- Error Message Pops Up
    By brit1sh in forum Excel General
    Replies: 3
    Last Post: 09-11-2013, 01:30 PM
  4. Error pops up on line On Error Resume Next
    By Erusso in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-20-2012, 04:57 PM
  5. error message pops up
    By louise2613 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-21-2012, 12:36 PM
  6. Macro error that pops up all the time
    By jmedick in forum Excel General
    Replies: 2
    Last Post: 09-24-2007, 11:40 PM
  7. Error-the message 'Device I/O error' pops up.
    By lehainam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2006, 01:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1