I have a section of code that builds a table as the user enters information. I'm having trouble figuring out how to navigate through this table. There is a NEXT button and a BACK button. When they hit NEXT, either the next row loads, or the textboxes are emptied out. When they hit BACK, if there is text in the textboxes, save it, then load the row above current one. The code I have now will work until you go back too many times. You can go back, then forward, but it has trouble seeing the end of the table. And for some reason, I get phantom rows in between, but not every time. The rest of the code, where this table gets saved to the server, all works. Just navigating the table is giving me trouble. Here is what I have so far:
Private Sub nextNewTr()
Try
'check that the itemNo and item boxes have text
'if not, exit
If txtItemNo.Text = "" Or txtItem.Text = "" Then
MsgBox("PLEASE ENTER INFORMATION FIRST")
Exit Sub
End If
'check to see if there are rows beyond the current one.
'if the "BACK" button was pressed, this should be true
If Me.tblTraining.Rows.Count > Me.rwNo Then
Me.tblTraining.Rows(rwNo).Item("itemNo") = Me.txtItemNo.Text
Me.tblTraining.Rows(rwNo).Item("task") = Me.txtItem.Text
Me.tblTraining.Rows(rwNo).Item("ques1") = Me.txtQues1.Text
Me.tblTraining.Rows(rwNo).Item("ques2") = Me.txtQues2.Text
Me.tblTraining.Rows(rwNo).Item("ques3") = Me.txtQues3.Text
Me.rwNo = Me.rwNo + 1
Me.txtItemNo.Text = Me.tblTraining.Rows(rwNo).Item("itemNo").ToString
Me.txtItem.Text = Me.tblTraining.Rows(rwNo).Item("task").ToString
Me.txtQues1.Text = Me.tblTraining.Rows(rwNo).Item("ques1").ToString
Me.txtQues2.Text = Me.tblTraining.Rows(rwNo).Item("ques2").ToString
Me.txtQues3.Text = Me.tblTraining.Rows(rwNo).Item("ques3").ToString
Else
'we can only end up here if there are no rows beyond the current one.
'create a new row
rwTr = tblTraining.NewRow
'accept the text from the window when NEXT is pressed.
Try
rwTr.Item("itemNo") = Me.txtItemNo.Text
rwTr.Item("task") = Me.txtItem.Text
rwTr.Item("ques1") = Me.txtQues1.Text
rwTr.Item("ques2") = Me.txtQues2.Text
rwTr.Item("ques3") = Me.txtQues3.Text
rwTr.Item("docID") = Me.hdrID
rwTr.Item("itemID") = Me.starthere
'add the row to the table
Me.tblTraining.Rows.Add(rwTr)
'set the screen controls empty, and increment the row
Me.btnBack.Enabled = True
Me.lblPrev.Visible = True
Me.lblPrevA.Visible = True
Me.lblPrev.Text = Me.txtItemNo.Text
Me.txtItemNo.Text = ""
Me.txtItem.Text = ""
Me.txtQues1.Text = ""
Me.txtQues2.Text = ""
Me.txtQues3.Text = ""
Me.rwNo = Me.rwNo + 1
Me.starthere = Me.starthere + 1
Catch r As Exception
MsgBox(r.ToString)
End Try
End If
If Me.rwNo > 0 Then
Me.btnBack.Enabled = True
End If
Catch w As Exception
MsgBox(w.ToString)
End Try
End Sub
Private Sub backNewTr()
If Me.tblTraining.Rows.Count > Me.rwNo Then
rwTr = tblTraining.NewRow
Me.tblTraining.Rows(rwNo).Item("itemNo") = Me.txtItemNo.Text
Me.tblTraining.Rows(rwNo).Item("task") = Me.txtItem.Text
Me.tblTraining.Rows(rwNo).Item("ques1") = Me.txtQues1.Text
Me.tblTraining.Rows(rwNo).Item("ques2") = Me.txtQues2.Text
Me.tblTraining.Rows(rwNo).Item("ques3") = Me.txtQues3.Text
'add the row to the table
'Me.tblTraining.Rows.Add(rwTr)
End If
Me.lblPrev.Text = Me.txtItemNo.Text
Me.rwNo = Me.rwNo - 1
Me.txtItemNo.Text = Me.tblTraining.Rows(rwNo).Item("itemNo").ToString
Me.txtItem.Text = Me.tblTraining.Rows(rwNo).Item("task").ToString
Me.txtQues1.Text = Me.tblTraining.Rows(rwNo).Item("ques1").ToString
Me.txtQues2.Text = Me.tblTraining.Rows(rwNo).Item("ques2").ToString
Me.txtQues3.Text = Me.tblTraining.Rows(rwNo).Item("ques3").ToString
If rwNo = 0 Then
Me.btnBack.Enabled = False
End If
End Sub
Please, if anyone has any ideas, let me know. I have a deadline approaching, and this needs to work. Thanks!!