Wednesday 13 June 2007

In Brisbane next week

My son is getting married so its off to Brisbane to attend the wedding.

This means that the past week has been turned upside down and I haven't done much on the blogsite because of this and other commitments.

Brisbane is a wonderful place and I wish I could have more time to see the sights. It'll be a lot warmer there than it is here at the moment so my Fiance is dragging out her summer clothes and has started packing for the trip. Being a guy, I only need a few minutes to throw some stuff into a bag before I leave, go pick her up and head to the airport.

I have just a little bit of apprehension on the length of time it will take to pick up Carol though. Based on her descriptions of decisions on things to pack, I'd best arrive at her house with plenty of time to spare.

I wouldn't miss this for the world. My son and his Fiance are the most wonderful couple and their getting married has been high on my wish list for years.

Wednesday 6 June 2007

Storing database fields to a TComboBox

While there is a TDBLookupComboBox that allows the developer to attach the TDBLookupComboBox to a table directly, it does have some issues in its use. I prefer to use a normal TComboBox and feed it from a Query. It is then a standalone ComboBox that can be used for more than just updating a field in one table with a field in another.

But how do I fill the ComboBox? And once filled, how do I know which record the user has selected?

First the Query. I am assuming that, like me, you create a field in every table that is filled with a unique ID. I can use this ID in other tables to create a join. For example, I may have a Customer table ...

CustomerID: Integer
Name: String (e.g. VarChar)

Lets say I want a ComboBox of all my customers for the user to select. The query would look something like this ...

SELECT CustomerID, Name
FROM Customer
ORDER BY Name

I am always sure to ask for the ID first and then the text that the user will want to see. Now lets fill the ComboBox with that detail. Because I may wish to do this with many comboboxes in my application and with many different queries on other tables, I have built a procedure to fill the ComboBox using AddObject.


Procedure FillCombo(cb: TComboBox; Q: TQuery);
begin
cb.items.clear;
Q.First;
while not Q.Eof do
begin
cb.Items.AddObject(Q.Fields[1].AsString, TObject(Q.Fields[0].AsInteger));
Q.Next;
end;
end;



Now I want to use that procedure to fill my ComboBox.

FillCombo(cbCustomer, MyQry);

So how then can we tell which record the user has selected? Simple, I have created a function that will return the ID of the selected record, in this case the selected Customer. If none has been selected, it will return -1.


function GetSelected(cb : TComboBox) : integer;
begin
if cb.ItemIndex = -1 then
result := -1
else
result := Integer(cb.Items.Objects[cb.ItemIndex]);
end;




So all we need to do is call it.

CustomerID := GetSelected(cbCustomer);
if CustomerID = -1 then
// none selected

Tuesday 5 June 2007

Elevate Software's new ElevateDB

I have just obtained ElevateDB from Elevate Software.

You will notice a link off this site for a Free SQL tool tool called Twenty Queries that I wrote some time ago when I wanted to find out the inner workings of SQL. That tool was for a product called DBISAM, a very good SQL database written entirely in Delphi. It compiled completely into your Delphi generated application with no BDE or other engined to install. Using my tool you could create a totally SQL call to recreate your entire database structure from the exe and therefore didn't even need to distribute the database tables with your application either.

ElevateDB is the big brother to DBISAM and I am very much looking forward to getting into developing with it. First on my list will be updating "Twenty Queries" for ElevateDB.

I'll tell you more about my experiences with it once I have had a play.