Problem with writing to table

soneinsa

Member
Licensed User
Longtime User
The only modification that I made was to change EntryNumber to EntryNum.

Well, Grashopr, first sorry for my English.

I the line # 3 you initialize row = 0, then when then sub ShowRow1 execute in line #24 you are not verifying if the table if empty and you are trying to assign a non exist row to the comboboxes, I think you have to test if table1.rowcount > 0 first.

Another problem I see is that in line #36 and #37 you are clearing then comoboxes values, I think that what you want to do is to clear the current value, you can do it with cmbJudgeName.SelectedIndex = -1.

I hope this help

Rafael Liriano
SONEINSA
 

klaus

Expert
Licensed User
Longtime User
In addition of soneinsa's post there are some other problems and questions.

Sub mnuAdd doesn't work because in the MenuEditor the name is Add and not mnuAdd. So when clicking onto Add in the menu nothing happens.

What exactly is mnuAdd supposed to do ?
As soneinsa already wrote with cmbJudgeName.Clear you clear the whole content of the combobox. To clear the text field you can clear it with cmbJudgeName.Text="". To use the text field on the device you must use the Door library to change the default behaviour of comboboxes. Have a look here: http://www.b4x.com/forum/questions-help-needed/3818-combobox-different-behavior-device-desktop.html

I think that when you add a new Judge or Cathegory you should also add those in the comboboxes.

Best regards.
 

Ariel_Z

Active Member
Licensed User
OK, I get the picture, I get the picture.

Please try this:

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,"EntryNum",1,True)
   table1.AddCol(cString,"JudgeName",1)
   table1.AddCol(cString,"Category",1)
   table1.LoadCSV(dataFile,",",True,False)
   table1.TableSort("EntryNum ASC") 'Sets the EntrynNum (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
   If row<table1.RowCount AND row > -1  Then
      cmbJudgeName.SelectedIndex = table1.Cell("JudgeName",row)
      cmbCategory.SelectedIndex = table1.Cell("Category",row)
   End If


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.SelectedIndex = -1
   cmbCategory.SelectedIndex = -1
   row = -1
End Sub

Sub btnRecord_Click
   
   changes = True
   If row<table1.RowCount AND row > -1 Then
      '''-------------> add more code to prevent duplicate enteries here
      '''-----------------------------------------------------------------------------
      table1.Cell("EntryNum",row) = txtEntryNum.Text
      table1.Cell("JudgeName",row) = cmbJudgeName.SelectedIndex
      table1.Cell("Category",row) = cmbCategory.SelectedIndex
   Else
      ' add new row
      row = -1
   End If
   

   If Row = -1 Then
      table1.AddRow(txtEntryNum.Text,cmbJudgeName.SelectedIndex,cmbCategory.SelectedIndex)
      row = table1.RowCount - 1
      ShowRow1
   Else
   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

Errors in the code you sent:
1. Menu item "mnuAdd" name was "Add". This is why it didn't find the event.
2. No validity checks for the value of row when inserting it to the table in a couple of places - not sure I've debugged all.
3. Using combobox.clear causes deletion of the values on the list. Changed.



Notes:


1. You might consider looking at the code - I've changed many things to make it half way a business application. Mainly - checks for the values of row. In the sample the usage of the global variable row is very rough, no validity checks.
2. You should add some code where I pointed to check for duplicate enteries. Let me know if you get into troubles.
3. All error messages you have got were correct. If I got you right, you went stratigh and compiled your application? If you did, you probably should try first debugging it - watch this video about debugging in Basic4ppc.
4. I did not check it completely. You should press everything and make your own changes (just be sure you understand what I did!) so that it works perfectly. Basically you should be aware you should have clicked "Add" before trying to record the row. I've added some code to prevent the need the first time, but you should afterwards!
5. The reason for the upper/lower cases in the error messages is that Basic4ppc translates everything internaly to lower case.

Good lock!
 
Last edited:

Grashopr

Member
Licensed User
Thank you all, I will have a little free time this afternoon to digest everything you've given me to look at and go over, I may have more questions then, but I now see the mnuAdd problem and I see what you're saying on the verifcation of the values being a problem. I was working from a program code that was included with the Basic4ppc program in 'Samples'. The file is called Accounts.spb, and is found in the Samples Folder of the program file for Basic4ppc. My small program here was essentially built off of the same code as a test, but apparently it didn't work too tell.

soneinsa, first thank you for the help. Can you look at the Accounts.spb file in your samples folder and see where that program is doing the empty table verification and the clearing of the combobox values? I was working line-by-line from the Accounts sample and I dont see it.
 

klaus

Expert
Licensed User
Longtime User
In the Accounts program there is no empty table check. I think the author supposed having always a non empty csv file.
There is no combobox clearing at all because for the entries only TextBoxes are used. The only ComboBox use has fixed values in it.
In the mnuAdd routine a default value is selected with cbmType.SelectedIndex = 1.

Best regards.
 

Ariel_Z

Active Member
Licensed User
This is where it clears combos:
B4X:
Sub mnuAdd_Click
   txtFirst.Text = ""
   txtFamily.Text = ""
   txtRemarks.Text = ""
   cmbType.SelectedIndex = 1
   txtNumber.Text = ""
   chkActive.Checked = True
   row = -1
End Sub


As for your question, where does it do the value check, well, it does not, because as a sample program shipped with an existing file there was never a regular situation of an empty table. However, business applications should check this. Note that there is a whole row changing machanism you've omitted.
 

Grashopr

Member
Licensed User
I am still getting no input into the table otehr than the headers, and the first header has the garbage in it, and I am still getting an error:

An error occurred on sub __main_btnrecord_click.
Index -1 is either negative or above rows count.
Continue?

When I click yes, it still does not record the data to the table in any row other than the header row.

Any ideas? I am going to continue looking at this, just this is the next stoping point (I am using the changes made by Ariel_Z)
 

Ariel_Z

Active Member
Licensed User
1. I tried it myself. If you click Add first, and then you enter data, and then click Record, there should be no problem.
2. On which line exactly are you getting the error (index out of range)? This is funny because the sub you mentioned does range checkings.
3. The error message allows you to continue the execution of program. However, obviously there can be nothing saved if the row number is irrelevant.
 

klaus

Expert
Licensed User
Longtime User
I have attached a modified version of your program.
- made the Table visible, for testing, so you can see what happens
- added 2 buttons to add new Judges and Categories in the ComboBoxes
- added 2 other buttons to add or modify an entry
- added the Door library to modiy the ComboBox behaviour

Now you can modify the texts in the ComboBox and add these new values at the end of the ComboBoxes. The function to modify a value is not implemented.

This is just as an example and not a final version.

If you want to use the csv files in Excel it is better to use ";" as the separator character instead of ",". By default, Excel expects ";" to put the different values directly in the columns. The attached program has been modifyed in this way.

For the UTF-8 file header characters I have no solution to avoid them in the csv files. The only way I see is to generate it on your own with ASCII characters.

Depending on what you want to do with your program there are perhaps other solutions than using the table control.

Best regards.
 

Attachments

  • BikeShow.zip
    3 KB · Views: 167
Top