![]() |
|
#1
|
||||
|
||||
|
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
Last edited by jpb103; 05-23-2014 at 08:09 AM. Reason: I'm a total dummy |
|
#2
|
||||
|
||||
|
*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
|
|
| Tags |
| checkbox, run-time error, tables |
|
|
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 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 438
|
cksm4 | Word | 2 | 01-12-2011 03:41 PM |