At a total loss for weird behavior

I swear I am sober…

I set up a little test to check something out and I am more at a loss now than before. Here’s what I did, the results and the code…

Xojo 2017 r 1.1
Max OS X 10.11.5 El Capitan on a MacBook Pro
Created a new window with a listbox, a textfield for RowCount, a textfield for RecordCount and a PushButton to add a new record.
There are no other Applications open and no other windows open.
When the window opens, everything is nominal, blank listbox, both textfields are zero
Click the button the first time and everything is still nominal, listbox has 1 row with the number 1 in it. Both textfields contain 1
Click the button a second time and things go South, the listbox is correct - 2 rows 1 & 2, the RecordCount textfield contains 2,
BUT THE ROWCOUNT TEXTFIELD CONTAINS 3
Click the button a third time and the RowCount textfield contains 6
A Fourth time and it contains 10!!!
The RowCount textfield always shows the sum of the numbers in the listbox rows.

All I am trying to get is the number of rows in the listbox…

The code in the Open event of the Window

[code]dim sSQL As String
dim iRows As Integer
dim iRecords As Integer

sSQL = “DROP TABLE IF EXISTS DBtest”
dbSQL.SQLExecute(sSQL)

sSQL = "CREATE TABLE DBtest( "
sSQL = sSQL + "pkRecID INTEGER PRIMARY KEY, "
sSQL = sSQL + “Comments TEXT)”
dbSQL.SQLExecute(sSQL)
if dbSQL.Error then MsgBox(dbSQL.ErrorMessage)

LoadListBox
iRows = Listbox1.ListCount
iRecords = rsRecords.RecordCount
RowCount.Text = str(Listbox1.ListCount)
RecordCount.Text = str(rsRecords.RecordCount)

Return[/code]

The code in the Action event of the pushbutton

[code]dim iRows As Integer
dim iRecords As Integer
dim NewRecord As new DatabaseRecord

NewRecord.Column(“Comments”) = “”
dbSQL.InsertRecord(“DBtest”, NewRecord)
if dbSQL.Error then MsgBox(dbSQL.ErrorMessage)

LoadListBox

iRows = Listbox1.ListCount
iRecords = rsRecords.RecordCount
RowCount.Text = str(Listbox1.ListCount)
RecordCount.Text = str(rsRecords.RecordCount)

Return
[/code]

The code in the LoadListBox method (the only method on the window)

[code]dim i As Integer
dim j As Integer

rsRecords = dbSQL.SQLSelect(“select * from DBtest”)
i = 0
j = rsRecords.RecordCount
rsRecords.MoveFirst
while not rsRecords.eof
ListBox1.addRow
ListBox1.cell( i, 0 ) = rsRecords.Field(“pkRecID”).StringValue
i = i + 1
rsRecords.MoveNext
Wend
[/code]

the only property is the rsRecords RecordSet (Private)

Can someone tell me what the blank is going on…

humour me.
Add

msgbox   rsRecords.Field("pkRecID").StringValue

just before i = i+1

Each time you press the button to add another row you call LoadListbox, but I can’t see where you are deleting all rows.

Wayne is correct. I duplicated your results and adding ListBox1.DeleteAllRows before the loop in LoadListbox corrects the count. Otherwise, you’re adding blank rows to the end of the list (click below the visible cells and you will see the highlight). Your code adds a new blank row and then sets the Cell() of a previously added row.

Another way to see the problem would be to change LoadListbox to use LastIndex instead of i.

ListBox1.cell( ListBox1.LastIndex, 0 ) = rsRecords.Field(“pkRecID”).StringValue

Yep, Wayne is correct. I think I need to get some sleep,

Thanks everyone…

Remember also… that if you DELETE any records in your table, OR add any that insert a PKID greater than the next one in sequence (which is quite legal)… that the values shown in your Listbox will be sequential, but NOT consecutive. (ie. there might be “holes”)

The initial problem was actually just my misconception that the first record added to a table would have a primary key of 0 instead of 1. This just proved to me that the primary key is 1-based and not 0-based.