Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2014, 07:58 AM
jpb103's Avatar
jpb103 jpb103 is offline Run-time error 5941 when deleting a row from a table Windows 7 64bit Run-time error 5941 when deleting a row from a table Office 2007
Advanced Beginner
Run-time error 5941 when deleting a row from a table
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Question Run-time error 5941 when deleting a row from a table


I have a command button on a word document that opens a user form for inputting values which are placed into a new row on a table. One of the columns on each row contains a checkbox. There is a second command button that, when clicked, deletes the rows that have checked check boxes. My problem is with this second command button; it does what it is supposed to in that it deletes the correct row, it just also happens to cause a run-time error. I know what block of code is causing the problem but I haven't got any ideas on how to resolve the problem.
Code:
Private Sub CommandButton2_Click()
    Dim Answer As VbMsgBoxResult
    Dim iRowCount As Integer
    Dim oCtr As InlineShape
    Dim iCounter As Integer
    Dim iCheckCount As Integer
    Dim strName As String
    'Select the correct table and set the table variable
    Answer = MsgBox("Are you sure you want to delete the checked items?", vbYesNo, "Confirm Deletion")
    'Give the user a chance to cancel row deletion
    iRowCount = Tables(1).Rows.Count - 2
    'Don't count the first two rows (Title and column labels)
    If (iRowCount < 1) Then
        'Check if there are no data rows
        MsgBox "There must be at least one item in the table to remove items", , "Error!"
        'Provide feedback to user
        End
    End If
    Select Case Answer
        Case vbYes:
            'Deletion Confirmed
            'Must delete rows with checked "Completion" check boxes
            For iCounter = 3 To iRowCount + 2
                For iCheckCount = 1 To 10
                    strName = "CheckBox" + CStr(iCheckCount)
                    If (CheckExists(strName, iCounter)) Then
                        Set oCtr = ActiveDocument.Tables(1).Rows(iCounter).Cells(3).Range.InlineShapes(1)
                        If (oCtr.Field.OLEFormat.Object.Value) Then
                            ActiveDocument.Tables(1).Rows(iCounter).Delete
                        End If
                    End If
                Next iCheckCount
            Next iCounter
        Case vbNo:
            'Deletion Cancelled
            'No action required
        Case Else
            'It is not possible to enter this section of code
            MsgBox "Invalid Selection!"
    End Select
 
End Sub
*EDIT - Of coarse as soon as I post this thread I figure out the problem; after deleting the row, it tries to do it again...9 more times*

Last edited by jpb103; 05-23-2014 at 08:09 AM. Reason: I'm a total dummy
Reply With Quote
  #2  
Old 05-26-2014, 07:08 AM
jpb103's Avatar
jpb103 jpb103 is offline Run-time error 5941 when deleting a row from a table Windows 7 64bit Run-time error 5941 when deleting a row from a table Office 2007
Advanced Beginner
Run-time error 5941 when deleting a row from a table
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

*FIXED CODE*
Code:
Private Sub CommandButton2_Click()
    Dim Answer As VbMsgBoxResult
    Dim iRowCount As Integer
    Dim oCtr As InlineShape
    Dim oTbl As Table
    Dim iCounter As Integer
    Dim iCheckCount As Integer
    Dim strName As String
    Dim delFlag As Boolean
    'Select the correct table and set the table variable
    Answer = MsgBox("Are you sure you want to delete the checked items?", vbYesNo, "Confirm Deletion")
    'Give the user a chance to cancel row deletion
    iRowCount = Tables(1).Rows.Count - 2
    'Don't count the first two rows (Title and column labels)
    If (iRowCount < 1) Then
        'Check if there are no data rows
        MsgBox "There must be at least one item in the table to remove items", , "Error!"
        'Provide feedback to user
        End
    End If
    Select Case Answer
        Case vbYes:
            'Deletion Confirmed
            'Must delete rows with checked "Completion" check boxes
AGAIN:
            Set oTbl = Tables(1)
            iRowCount = oTbl.Rows.Count
            For iCounter = 3 To iRowCount
                For iCheckCount = 1 To 10
                    strName = "CheckBox" + CStr(iCheckCount)
                    If (CheckExists(strName, iCounter)) Then
                        Set oCtr = ActiveDocument.Tables(1).Rows(iCounter).Cells(3).Range.InlineShapes(1)
                        If (oCtr.Field.OLEFormat.Object.Value) Then
                            delFlag = True
                        End If
                    End If
                Next iCheckCount
                If (delFlag) Then
                    'MsgBox iCounter
                    oTbl.Rows(iCounter).Delete
                    delFlag = False
                    GoTo AGAIN
                End If
            Next iCounter
        Case vbNo:
            'Deletion Cancelled
            'No action required
        Case Else
            'It is not possible to enter this section of code
            MsgBox "Invalid Selection!"
    End Select
End Sub
The goto might be bad programming practice but I was getting tired of pulling my hair out.
Reply With Quote
Reply

Tags
checkbox, run-time error, tables

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
5941 requested member of collection does not exist Prevents Userform from Showing marksm33 Word VBA 1 02-22-2014 08:56 AM
Get Run-time Error 11 Jamtart PowerPoint 2 08-31-2012 05:04 AM
Run-time error 5941 when deleting a row from a table Run time error 1004 yonasan Excel Programming 3 06-12-2012 11:08 PM
Word Visual Basic error - run time error 504 crazymorton Word 11 01-13-2012 04:32 AM
Run-time error 5941 when deleting a row from a table Run-time error 438 cksm4 Word 2 01-12-2011 03:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft