Problem with writing to table

Grashopr

Member
Licensed User
First, thank you for the terrific program. It's working terrifically, however I'm stuck on one part... I'm attempting to write 3 tuples in a new row to the invisible table table1. I am emulating the subs that were in the Accounts program code that was included with the Basic4ppc software. Here is my file:

B4X:
Sub Globals
   'Declare the global variables here.
   row = 0 'record ID
   dataFile = "BikeShow.csv"
   changes = False 'Used for the 'save changes before exit' question
End Sub

Sub App_Start
   frmStart.Show
   'ErrorLabel (Err)
   'Adds the table columns
   table1.AddCol(cString,"EntryNumber",1,True)
   table1.AddCol(cString,"JudgeName",1)
   table1.AddCol(cString,"Category",1)
   table1.LoadCSV(dataFile,",",True,False)
   table1.TableSort("EntryNumber ASC") 'Sets the Number (entry number) column as the sort field
   ShowRow1
   Return
   Err:
   Msgbox("Error Loading Data File.","",cMsgboxOK,cMsgboxHand)
End Sub

Sub ShowRow1 'Shows first page rows from table if they exist
   cmbJudgeName.SelectedIndex = table1.Cell("JudgeName",row)
   cmbCategory.SelectedIndex = table1.Cell("Category",row)

End Sub

Sub mnuSave_Click
   changes = False
   table1.SaveCSV(dataFile,",",True) 'Saves the data as a CSV File.
End Sub

Sub mnuAdd_Click
   txtEntryNum.Text = ""
   cmbJudgeName.Clear
   cmbCategory.Clear
   row = -1
End Sub

Sub btnRecord_Click
   ErrorLabel (Err1)
   changes = True
   If Row = -1 Then
      table1.AddRow(txtEntryNum.Text,cmbJudgeName.SelectedIndex,cmbCategory.SelectedIndex)
      row = table1.RowCount - 1
      ShowRow1
   Else
      table1.Cell("EntryNumber",row) = txtEntryNum.Text
      table1.Cell("JudgeName",row) = cmbJudgeName.SelectedIndex
      table1.Cell("Category",row) = cmbCategory.SelectedIndex
   End If
   Return
   Err1:
   Msgbox("Entry Number must be unique.","",cMsgboxOK,cMsgboxHand)
End Sub

Sub Form1_Close
   If changes = True Then
      i = Msgbox("Do you want to save changes?","",cMsgboxYesNoCancel,cMsgboxQuestion)
      If i = cYes Then
         mnuSave_Click
      Else If i = cCancel Then
         frmStart.CancelClose
      End If
   End If
End Sub

I get an Err1 "Entry Number must be unique." error on save, no matter what. Any ideas where I'm going wrong?
 

specci48

Well-Known Member
Licensed User
Longtime User
Hello Grashopr,

welcome to basic4ppc. :)

In your program you added the column "EntyNumber" as
B4X:
   table1.AddCol(cString,"EntryNumber",1,[COLOR="Red"]True[/COLOR])
The parameter "True" defines, that all entries in this colum must be unique (see help file). If you create the column as
B4X:
   table1.AddCol(cString,"EntryNumber",1,[COLOR="SeaGreen"]False[/COLOR])
duplicate values are accepted.


specci48
 

Grashopr

Member
Licensed User
I meant to have it be a Unique Entry Number for each entry... they are ID'd by the entry number. In the Accounts program code, it shows that they use the Number tuple as the Unique identifier:

B4X:
Sub App_Start
   Form1.Show
   'ErrorLabel(Err)
   'Adds the table columns
   table1.AddCol(cString, "FamilyName",1)
   table1.AddCol(cString,"FirstName",1)
   table1.AddCol(cNumber,"Number",1,True) 'Account Number must be unique.
   table1.AddCol(cNumber,"Type",1)
   table1.AddCol(cString,"Remarks",1)
   table1.AddCol(cString,"Active",1)
   table1.LoadCSV(dataFile,",",True,False) 'Loads the data from the data file.
   table1.TableSort("Number ASC") 'Sets the Number (Account Number) column as the sort field.
   ShowRow
   Return
   Err:
   Msgbox("Error loading data file.","",cMsgboxOK,cMsgboxHand)
End Sub


I really NEED each of the EntryNumber tuples to be unique... is there another way to do this?
 

Grashopr

Member
Licensed User
This is the sample called Accounts that comes with the distribution.. this is the code that I was working from to try to make this program:


B4X:
'All the data is stored in Table1 which is not visible.
Sub Globals
   row = 0 'Record ID
   dataFile =   "AccountsData.csv"
   changes = False 'Used for the "save changes before exit" question.
End Sub

Sub App_Start
   Form1.Show
   'ErrorLabel(Err)
   'Adds the table columns
   table1.AddCol(cString, "FamilyName",1)
   table1.AddCol(cString,"FirstName",1)
   table1.AddCol(cNumber,"Number",1,True) 'Account Number must be unique.
   table1.AddCol(cNumber,"Type",1)
   table1.AddCol(cString,"Remarks",1)
   table1.AddCol(cString,"Active",1)
   table1.LoadCSV(dataFile,",",True,False) 'Loads the data from the data file.
   table1.TableSort("Number ASC") 'Sets the Number (Account Number) column as the sort field.
   ShowRow
   Return
   Err:
   Msgbox("Error loading data file.","",cMsgboxOK,cMsgboxHand)
End Sub

Sub ShowRow 'Shows 
   txtFamily.Text = table1.Cell( "FamilyName",row)
   txtFirst.Text = table1.Cell("FirstName",row)
   txtNumber.Text = table1.Cell("Number",row)
   cmbType.SelectedIndex = table1.Cell("Type",row)
   txtRemarks.Text = table1.Cell("Remarks",row)
   chkActive.Checked = table1.Cell("active",row)
   lblRecords.Text = "Record " & row+1 & " out of " & table1.RowCount
End Sub

Sub mnuSave_Click
   changes = False
   table1.SaveCSV(dataFile,",",True) 'Saves the data as CSV file.
End Sub

Sub mnuNext_Click
   row = (row+1) Mod table1.RowCount
   ShowRow
End Sub

Sub mnuPrev_Click
   row = (row+table1.RowCount-1) Mod table1.RowCount
   ShowRow
End Sub

Sub mnuAdd_Click
   txtFirst.Text = ""
   txtFamily.Text = ""
   txtRemarks.Text = ""
   cmbType.SelectedIndex = 1
   txtNumber.Text = ""
   chkActive.Checked = True
   row = -1
End Sub

Sub btnKeep_Click
   ErrorLabel(Err1)
   changes = True
   If row = -1 Then 'Adds new row.
      table1.AddRow(txtFamily.Text,txtFirst.Text, txtNumber.Text, cmbType.SelectedIndex,                 txtRemarks.Text, chkActive.Checked)
      row = table1.RowCount-1
      ShowRow
   Else 'Changes current row.
      table1.Cell("FamilyName",row) = txtFamily.Text
      table1.Cell("FirstName",row) = txtFirst.Text
      table1.Cell("Number",row) = txtNumber.Text
      table1.Cell("Type",row) = cmbType.SelectedIndex
      table1.Cell("Remarks",row) = txtRemarks.Text
      table1.Cell("Active",row) = chkActive.Checked
   End If
   Return
   Err1:
   Msgbox("Account Number must be unique.","",cMsgboxOK,cMsgboxHand)
End Sub

Sub mnuDelete_Click
   If row = -1 Then Return
   If table1.RowCount = 1 Then
      Msgbox("Can't delete last row.")
      Return
   End If
   changes = True
   table1.RemoveRow(row)
   mnuPrev_Click
End Sub

Sub Form1_Close
   If changes = True Then
      i = Msgbox("Do you want to save changes?","",cMsgboxYesNoCancel,cMsgboxQuestion)
      If i = cYes Then
         mnuSave_Click
      Else If i = cCancel Then
         form1.CancelClose
      End If
   End If
End Sub

Sub mnuAccountNumber_Click
   mnuFamily.Checked = False
   mnuFirst.Checked = False
   mnuAccountNumber.Checked = True
   table1.TableSort("Number ASC")
   row = 0
   ShowRow
End Sub

Sub mnuFamily_Click
   mnuFamily.Checked = True
   mnuFirst.Checked = False
   mnuAccountNumber.Checked = False
   table1.TableSort("FamilyName ASC")
   row = 0
   ShowRow
End Sub

Sub mnuFirst_Click
   mnuFamily.Checked = False
   mnuFirst.Checked = True
   mnuAccountNumber.Checked = False
   table1.TableSort("FirstName ASC")
   row = 0
   ShowRow
End Sub

Sub mnuFilter_Click
   mnuFilter.Checked = Not(mnuFilter.Checked)
   If mnuFilter.Checked = True Then
      btnKeep.Visible = False
      btnFilter.Visible = True
      txtFirst.Text = ""
      txtFamily.Text = ""
      txtRemarks.Text = ""
      cmbType.Enabled = False
      txtNumber.Text = ""
      chkActive.Enabled = False
      mnuNext.Enabled = False
      mnuPrev.Enabled = False
      mnuAdd.Enabled = False
      menu8.Enabled = False
      Msgbox("Enter the search criteria.")
      row = -1
   Else
      table1.Filter("")
      btnKeep.Visible = True
      btnFilter.Visible = False
      chkActive.Enabled = True
      cmbType.Enabled = True
      btnFilter.Visible = False
      btnKeep.Visible = True   
      mnuNext.Enabled = True
      mnuPrev.Enabled = True
      mnuAdd.Enabled = True
      menu8.Enabled = True   
      form1.Text = "Accounts"
      row = 0
      ShowRow
   End If
End Sub

Sub btnFilter_Click 'Sets the Filter property.
   If txtFirst.Text <> "" Then 
      table1.Filter("FirstName = '" & txtFirst.Text & "'")
   Else If txtFamily.Text <> "" Then
      table1.Filter("FamilyName = '" & txtFamily.Text & "'")
   Else If txtNumber.Text <> "" Then
      table1.Filter("Number = '" & txtNumber.Text & "'")
   Else If txtRemarks.Text <> "" Then
      table1.Filter("Remarks Like '*" & txtRemarks.Text & "*'") 'Use LIKE and wildcards to check if the string appears anywhere inside the text.
   Else
      Msgbox("No search criteria entered.")
      Return
   End If
   If table1.RowCount = 0 Then
      Msgbox("No matching records.")
      Return
   End If
   form1.Text = "Accounts (Filtered)"
   row = 0
   ShowRow
   chkActive.Enabled = True
   cmbType.Enabled = True
   btnFilter.Visible = False
   btnKeep.Visible = True   
   mnuNext.Enabled = True
   mnuPrev.Enabled = True
   mnuAdd.Enabled = True
   menu8.Enabled = True


what part of that am I missing, because that code works. I checks for unique id on Number, and returns a unique row number each time. I appreciate the help. Thank you.
 

Grashopr

Member
Licensed User
My first post is my entire code without the form, which has two combo boxes, one table and one textbox. There is nothing special at all about any of them, the cmb's are combo boxes, and the txt is the textbox.
 

Grashopr

Member
Licensed User
I will attempt to get it zipped tonight. I didn't really think that the rest of the code other than the types of input boxes would really be a possible cause of this, I was planning on it being some part of my code.
 

Grashopr

Member
Licensed User
Any help at all on trying to figure out why this isn't writing to the table would be appreciated. If it's something completely obvious and noone is responding because it should be something I see, if you could give me a pointer as to where I messed up, I'll find it.
 

Grashopr

Member
Licensed User
I am requesting a refund, as if there is no obvious problem with the code that I have written, then I must assume that the problem is in the program when it compiles the code. How do I go about getting a refund?
 

Ariel_Z

Active Member
Licensed User
Grashopr,

I've managed to reproduce the bug - I've created a new .csv.
Please have a look at the "btnRecord_Click" sub.
Your code is:
B4X:
table1.Cell("EntryNumber",row) = txtEntryNum.Text

While it should be:
B4X:
table1.Cell("EntryNum",row) = txtEntryNum.Text

I'll try to explain what went wrong: the entire sub's code was
B4X:
Sub btnRecord_Click
   ErrorLabel(Err1)
   changes = True
   If Row = -1 Then
      table1.AddRow(txtEntryNum.Text,cmbJudgeName.SelectedIndex,cmbCategory.SelectedIndex)
      row = table1.RowCount - 1
      ShowRow1
   Else
      table1.Cell("EntryNumber",row) = txtEntryNum.Text
      table1.Cell("JudgeName",row) = cmbJudgeName.SelectedIndex
      table1.Cell("Category",row) = cmbCategory.SelectedIndex
   End If
   Return
   Err1:
   Msgbox("Entry Number must be unique.","",cMsgboxOK,cMsgboxHand)
End Sub

The first line indicates that any error occurring inside the sub should cause execution to jump to the Label Err1. This is where your program displayed the message box ("Entry number ... " ). The assumption in the example you've based on was that this is the most frequent error (of course it is an example, so no complete error handling algorithm was supplied). When you've changed it you've misspelled the column's name. This caused an error, which in turn was handled with the error label. This is also the reason why you got it always the same way. It is difficult to trap these errors - it took me some iterations to understand, as the string representing the column's name is not a real variable. Anyway, what helps a lot is removing the first line (ErrorLabel...). This way errors will be popped the line they occur.
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Eheh, I've done several times this error, to misspell some component name.
After some bad words, I've found the problem...
 

Grashopr

Member
Licensed User
I changed sub btnRecord_Click to show EntryNum instead of EntryNumber as the label for the first column, however the program still is not recording to the table. The first column of the .csv file is inserting four garbage characters in front of the label EntryNum:

EntryNum

with no input in any row beyond the header row.

I have included all of the files that I have availble... including the .csv file. I start with an empty .csv file labeled BikeShow.cvs and run the program, which populates the .csv file with the header rows of EntryNum / JudgeName / Category. When I compile the program, I am compiling it as a Windows EXE. When I run the BikeShow.exe file, I get a Basic4ppc error of:

An error ocurred on sub_main_showrow1.
Index 0 is either negative or above rows count. Continue?

This is a different error than I was getting before, now when I input data into the fields and click Record, I get the following error:

An error occurred on sub__main_btnrecord_click.

Index 0 is either negative or above rows count.
Continue?

what I find interesting is that regardless of the fact that the sub is labeled btnRecord_click, the error shows a lowercase R in record. Between the negative index on showrow1 (where is the index of showrow1??) and the btnRecord problem, I am still in the dark. Then the garbage that is being put in before EntryNum in the .csv file.. is the file heading building done during SaveCSV in mnuSave_Click, or in LoadCSV in App_Start?
 
Last edited:

IoSonoPiero

Active Member
Licensed User
Longtime User
Sorry, but your code is a little strange.

I think there are logic errors about the row counter.

This is the complete code? Or you modified some code?
 
Top