Table fields and text controls

Hi all,

Maybe this is a really stupid question as I am still quite new to Xojo. I successfully made an app that can connect to my firebird database.

I can load a record and display the data on the screen:

If rs <> Nil Then
FieldUserCode.Text = trim(rs.Field(“UserCode”).StringValue)
FieldEnglishName.Text = trim(rs.Field(“EnglishName”).StringValue)
FieldPassword.Text = trim(rs.Field(“Password”).StringValue)
FieldEngagementDate.DateValue = rs.Field(“EngagementDate”).Value
FieldPerformIndex.Text =str(rs.Field(“PerformanceIndex”).CurrencyValue)
end if

and can save updated data by:

rec.Column("UserCode") = FieldUserCode.Text
rec.Column("EnglishName") = FieldEnglishName.Text
rec.Column("Password") = app.EncryptDataString(FieldPassword.Text)
rec.DateColumn("EngagementDate") = FieldEngagementDate.DateValue
rec.CurrencyColumn("PerformanceIndex") = CDbl( FieldPerformIndex.Text)

My question now is, what about if there are 100 fields in my table, do I have to repeat each field 100 times? Is there an easier way to do this?

Thanks.

You just loop through the record set and get the value.

While not rs.EOF
Listbox1.AddRow rs.Field("EnglishName").StringValue
rs.MoveNext
Wend
rs.Close

For example :slight_smile:

And for setting the value of 100 records you’ll want to look at PreparedStatement

Sorry guys, I mean 100 fields, not records :slight_smile:

You can use this.
Checking if is textfield,label,etc and get the data you want

For i As Integer = 0 To Self.ControlCount-1 // Check if the control is a TextField If Self.Control(i) IsA TextField Then // Cast the control to a TextField in order to access the Text property TextField(Self.Control(i)).Text = "Found You!" End If Next
Isa

FWIW, a recordset will never be nil unless there is a database error. So rs <> nil doesn’t begin to be useful since it hides the real problem.

You MUST check the database.error bit and retrieve the database.errormessage for it to be useful. Xojo does not throw an exception on database error so you have to check it after every database action.

@Bob for your advise.
@Loannis, seems that still doesn’t solve my problem, even though I can get the type of the control, how can I assign the correct value in the database record to the correct field on the UI?

I personally avoid creating generic entry screens. I prefer a well crafted, intelligent data entry screen. But, if you need a generic screen, create a containercontrol with a label and a textfield. Loop through the fields in the recordset and create a new containter for each. Set the label to the field name and the textfield to the value. Then embed it at the next “row” on the screen and keep a reference to it in an array for when you want to write them back out.

Alternately, use a listbox with 2 columns - label and value - so you can scroll the view easily.

[quote=259030:@Tony Lam]@Bob for your advise.
@Loannis, seems that still doesn’t solve my problem, even though I can get the type of the control, how can I assign the correct value in the database record to the correct field on the UI?[/quote]
You can proceed with select case or with if then loop.

If TextField(Self.Control(i)).Name = "FieldUserCode" then "Your code" end if
Preferred is the select case