How can I load column names from an Access table into a variable (I'm not sure which one to use)? I'm using an ADOTable in Delphi7
I'm trying to import the column names from an MS Access Table (named Learners) to some variable (开发者_如何学Cnot sure which one to use). The column's names are dates. When btnSave (TButton) is clicked the code should determine if the column with the current date has been created yet. If not then it should create it. This is the code I got so far:
procedure TForm1.btnSaveClick(Sender: TObject);
var
bFound: boolean;
K: integer;
strColumnNames : TStringList;
begin
strColumnNames := TStringList.Create;
tblLeerders.GetFieldNames(strColumnNames);
bFound := False;
for K := 1 to tblLeerders.IndexFieldCount
do
begin
if strColumnNames.Strings[K] <> FormatDateTime('dd/mm/yyyy', Date())
then
begin
bFound := True;
end;
end;
if bFound = False
then
begin
with qryLearners
do
begin
SQL.Text := 'Alter TABLE Leerders ADD COLUMN ' + FormatDateTime('dd/mm/yyyy', Date()) + ' Boolean ';
ExecSQL;
end;
end;
end;
Please help! Any advice would be much appreciated.
Try:
if Assigned(tblLeerders.FindField(FormatDateTime('dd/mm/yyyy', Date()))) then
begin
qryLearners.SQL.Text := 'Alter TABLE Leerders ADD COLUMN ' +
FormatDateTime('dd/mm/yyyy', Date()) + ' Boolean ';
qryLearners.ExecSQL;
end;
(code edited as per Gerry, below)
You also said "Any advice would be appreciated". My advice is don't try to use a database like a spreadsheet. Store the data in normal fashion (in a table with a date & value column) and then denormalize for presentation. Access makes this easy to do with Crosstab Queries.
精彩评论