Subject Re: [IBO] TIB_Cursor Education Needed
Author Helen Borrie
At 08:27 PM 26/05/2003 +0000, you wrote:
>I am attempting to use tib_cursor to populate the Items and
>ItemValues of a TIB_ComboBox. Using some sample code from one of the
>Tech Info Sheets I tried the following in the FormCreate event:
>
>HMSDataModule.ReadOnlyTransaction.StartTransaction;
>cBox.Items.Clear;
>cBox.ItemValues.Clear;
>with myCursor do
> begin
> First;
> while not Eof do
> begin
> cBox.Items.Add(myCursor.FieldByName('myCode').AsString);
> cBox.ItemValues.Add(myCursor.FieldByName('myKey').AsString);
> Next;
> end;
> Close;
> end;
>HMSDataModule.ReadOnlyTransaction.Commit;

The above code always works for me.

>The cursor was configured with both AutoFetchAll and AutoFetchFirst
>set to true, and retrieves 19 records.

I never touch these properties when using tib_cursor to populate a TStrings.

>Oddly enough, this would NOT work as expected and the ComboBox would
>always ended up empty. Debugging the code I discovered that Eof was
>true on the first pass of the loop.

This would be the case if the row buffer were empty. It indicates that the
dataset isn't active. Could it be that you didn't set its ib_transaction
property correctly?

When I'm populating a TStrings from a "hit-and-run" transaction like this,
I always make a public method in the datamodule to do it. The datamodule
is always created before any forms. Then, in the FormCreate, I call the
method in the datamodule. I never access the internal methods of a
datamodule from a form. I can call the public method any time from a form
- so can refresh ib_combobox lists at other times besides FormCreate.

I would not mess around with a whole lot of dataset properties from the
form. If the timing is right and the homework is done properly, this type
of operation is squeaky-clean.

Here's a typical such public method in a datamodule:

procedure TdmFFoundation.LoadCountries (cItems,cList: TStrings);
begin
cList.Clear;
cItems.Clear;
with qrCountry do
begin
First;
while not EOF do
begin
cList.Add (Fields[0].AsString);
cItems.Add (Fields[1].AsString);
Next;
end;
First;
end;
end;

Here's the code in the form:

procedure TfmFFoundation.FormCreate(Sender: TObject);
var
Bm: String;
begin
with dmFFoundation do
begin
Bm := qrCountry.Bookmark;
LoadCountries(cbCountry.Items, cbCountry.ItemValues);
qrCountry.Bookmark := Bm;
// other stuff
end;
end;

(The bookmark stuff isn't relevant to your problem - it's there to re-point
the selected list item to the connected main record.)

hth
Helen