delphi - need to read all occurrences of Recurring Outlook Appt - delphi

Using Delphi to read Outlook appointments via COM...
Code is working fine, with the exception of Recurring appointments.
Everything I read says that I need to using RecurrencePattern and GetOccurrence and determine where the next appointment should be, and then try to get it, and see if it fails... This seems like a really "kludged" way of doing it.
Has anyone already written something like this? Apparently, there is some code on experts-exchange, but I don't have a subscription there... Can't find anything else.
IDEALLY (and I will take what I can get), I would like a routine that says.. this appointment has 6 occurrences, and here is an array of all the TDateTimes of each occurrence.
Note that all of this code works fine. I just need help filling out the BOTTOM section of code to build recurrance patterns.
CODE FRAGMENTS --- Not all code is shown---... as per request...
Access Outlook...
try
Outlook := GetActiveOleObject('outlook.application');
Form1.SB1.SimpleText := 'Outlook already started';
except
try
Outlook := CreateOleObject('outlook.application');
Created := True;
Form1.SB1.SimpleText := 'Outlook not running. Starting Outlook API';
except
// Unable to access or start OUTLOOK
MessageDlg(
'Unable to start or access Outlook. Possibilities include: permission problems, server down, or VPN not enabled. Exiting...', mtError, [mbOK], 0);
exit;
end;
end;
... Get the Calendar of my recipient...
// Now get the calendar entry
Calendar := Namespace.GetSharedDefaultFolder(Recip, 9);
Now set the filter to restrict appoints to be within a date range, and include recurrences.
// If here, everything is good so far...
// user name, email, and Calendar is accessible
MyItems := Calendar.Items;
MyItems.Sort('[Start]', False);
MyItems.IncludeRecurrences := True;
// Set the filter dates... SECONDS can NOT be shown...
FilterStartDate := FormatDateTime('mmmm dd, yyyy', StartDate);
FilterStartDate := FilterStartDate + ' 12:00 AM';
FilterEndDate := FormatDateTime('mmmm dd, yyyy', EndDate);
FilterEndDate := FilterEndDate + ' 11:59 PM';
RestrictDateFilter := ('[Start]>' + CHR(34) + FilterStartDate + CHR(34) + 'and ' + '[Start]<' + CHR(34)
+ FilterEndDate + CHR(34));
DebugIt('RestrictFilter:', RestrictDateFilter);
Application.ProcessMessages;
ItemCollection := MyItems.Restrict(RestrictDateFilter);
ItemCollection.Sort('[Start]', False);
Read my first appointment
// Try to read the first appoint, or error message if no appointments
try
Appointment := ItemCollection.GetFirst;
except
DebugIt('No appointments found', '');
MessageDlg('Unable to retrieve any appointments in this time frame.', mtError, [mbOK], 0);
exit;
end;
While looping through all the appointments...
if Appointment.IsRecurring = True then
begin
// Recurring Appointment, in a Valid RANGE
DebugIt('Repeating appointment starting on ' + DateToStr(Appointment.Start), '');
// If yearly repeating, we want to ignore
RP := Appointment.GetRecurrencePattern;
DebugIt('Determining appointment recurrence pattern', '');
if ((RP.RecurrenceType = olRecursYearly) or (RP.RecurrenceType = olRecursYearNth)) then
begin
// ignore these appointments
end
else
begin
// HERE IS WHERE I NEED HELP
// How do I determine all of the appointments based on the recurrences?
end;
end;
Thanks
GS

Figured out an answer....
Here is a routine I wrote which will go from MinDate to MaxDate, and test to see if the appointment exists on that date. This was the only way that I could get recurrences to work...
procedure IdentifyOutlookRecurrences(Appt: Variant; EmailID: Integer; MinDateAllowed, MaxDateAllowed: TDateTime);
var
recurStart, recurEnd: TDateTime;
RP: Variant;
dt: TDate;
PatternEndDate: TDate;
TestAppt: Variant;
year, month, day, hour, minute, second, ms: Word;
CheckDateTime: TDateTime;
OccurrenceEndDate: TDateTime;
OccurrenceNumber: Integer;
begin
if Appt.IsRecurring then
begin
RP := Appt.GetRecurrencePattern;
DebugIt('Recurring Appt:', Appt.Subject);
// Get the date range for our occurrences
recurStart := RP.PatternStartDate;
recurEnd := RP.PatternEndDate;
DebugIt('Recur Start:End', DateToStr(recurStart) + ':' + DateToStr(recurEnd));
DebugIt('RecurPattern Start Time', DateTimeToStr(RP.StartTime));
// Identify the end point for looping...
if recurEnd < MaxDateAllowed then
PatternEndDate := recurEnd
else
PatternEndDate := MaxDateAllowed;
// Get the minimum date allowed...
dt := trunc(MinDateAllowed);
DecodeDate(dt, year, month, day);
DecodeTime(RP.StartTime, hour, minute, second, ms);
OccurrenceNumber := 0;
repeat
DecodeDate(dt, year, month, day);
CheckDateTime := EncodeDateTime(year, month, day, hour, minute, second, 0);
DebugIt('Check for recurrance', DateTimeToStr(CheckDateTime));
// Now check it the appointment exists.
try
TestAppt := RP.GetOccurrence(CheckDateTime);
OccurrenceEndDate := CheckDateTime + (RP.Duration / 1440);
DebugIt('Appt Recurrence *** IS *** found', DateTimeToStr(CheckDateTime));
// Now write it to the database
InsertApptIntoDB(Appt, EmailID, OccurrenceNumber, CheckDateTime, OccurrenceEndDate);
Inc(OccurrenceNumber);
except
DebugIt('Appt Recurrence *** NOT *** found', DateTimeToStr(CheckDateTime));
end;
// Increment our date
dt := dt + 1;
until dt > PatternEndDate;
end;
end;
DebugIt is just a logging routine I use...

Related

Delphi TFDQuery - Filter for multiple expressions and YearOf

I'm trying to set up a filter with the actual year and month.
Database is MSSQL.
I added DateUtils to my uses. How can I set up the filter expression to use the YearOf() function?
How can I add more than one line filter expression? Is this possible?
This is working:
with FDQuery2 do begin
Filtered := False;
Filter := '"Posting Year" = 2021'; //+ 'Posting month = January'
Filtered := True;
end;
Unfortunately, this returns me an error:
with FDQuery2 do begin
Filtered := False;
Filter := '"Posting Year" = YearOf';
Filtered := True;
end;
How can I add a second filter expression?
UPDATE: I used now a little workaround to avoid much more work due to converting TDateTime, etc.
var
YearFilter: string;
YearFilter := IntToStr(CurrentYear);
I managed to get the filtering now correctly.
To get the month of the year I have to declare a TDate variable.
MonthOfTheYear returns me the month as Dword - for use in a filter expression it needs converting to a string via IntToStr.
Date is the function System.SysUtils.Date.
http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.Date
Date1 := Date;
We assign Date1 variable just the actual date with System.SysUtils.Date
var
Date1: TDate;
YearFilter, MonthFilter: string;
with FDQuery2 do begin
Filtered := False;
YearFilter := IntToStr(CurrentYear);
Date1 := Date();
MonthFilter := IntToStr(MonthOfTheYear(Date1));
Filter := '"Posting Year" = ' + QuotedStr(YearFilter) + ' AND ' + '"Posting Month" = ' + QuotedStr(MonthFilter);
Filtered := True;
end;

Fast Report displaying incorrect data from ADOQuery

I have a problem with Fast Report displaying incorrect data from an ADOquery. I use the following sql.text
SELECT * FROM JOB_DATA
INNER JOIN CUSTOMER ON JOB_DATA.CUST_CODE = CUSTOMER.CUST_CODE
WHERE JOB_DATA.SHIP_DATE Between [Date1] And [Date2]
ORDER by SHIP_DATE
Fast Report only shows the data where SHIP_DATE = null.
If I throw up a TDBgrid and attach it to a data source attached to the same ADOquery, then the dbgrid shows exactly the correct information.
I'm out of ideas, any suggestions?
To answer questions about where the dates come from:
var
date1:string;
date2:string;
sql_str:string;
begin
date1:=inputbox('Date Range','Enter Beginning Date','');
Try
StrToDate(date1);
Except
On EConvertError Do
Begin
MessageDlg('Please enter a valid date. Format xx/xx/xx',
mtError, [mbOK], 0);
//ShowMessage('Please enter a valid date. Format `enter code here`xx/xx/xx');
Exit;
End;
End;
date2:=inputbox('Date Range','Enter Ending Date','');
Try
StrToDate(date2);
Except
On EConvertError Do
Begin
MessageDlg('Please enter a valid date. Format xx/xx/xx',
mtError, [mbOK], 0);
//ShowMessage('Please enter a valid date. Format `enter code here`xx/xx/xx');
Exit;
End;
End;
sql_str:= 'SELECT * FROM JOB_DATA INNER JOIN CUSTOMER ON ' +
'JOB_DATA.CUST_CODE = CUSTOMER.CUST_CODE ' +
'WHERE JOB_DATA.SHIP_DATE Between ';
sql_str:= sql_str+ ''' ';
sql_st:=sql_str + date1;
sql_str:= sql_str+ '''';
sql_str:= sql_str+ ' AND ';
sql_str:= sql_str+ ''' ';
sql_str:= sql_str+ date2;
sql_str:= sql_str+ ' ''';
with ADOQuery5 do
begin
Close;
SQL.Clear;
SQL.text:= sql_str;
Open;
end;
frxreport2.ShowReport();
end;
The ADOquery is attached to frxDBDataset2 which is attached to frxReport2. I am doing nothing to alter the results in the query.
No, I have no code in the report, it was all generated from the wizard.
FastReport cannot display records only where SHIP_DATE is NULL, because your query shouldn't be returning them based on your WHERE clause if Date1 and Date2 are properly assigned. This means that either your dataset and the FastReport aren't connected properly or that something in your code assigning the date values for the BETWEEN clause is wrong, and the dates aren't being provided to the query correctly.
The first place to start looking is to make sure that all of the report columns are correctly assigned the proper TfrxDataSet and the proper database column. (Click on the report item (text object or whatever it might be), and check its DataSet and DataField properties to ensure they are correct.)
If that's not the problem, it may be the way you're building your query, which probably isn't correctly formatting the dates for ADO. (You're just using whatever format happens to pass the StrToDate calls without raising an exception.)
The way you're setting up your SQL is really unadviseable. It's unreadable and unmaintainable when you try to manage quoting yourself in code.
You should use parameters, which first and foremost protects you against SQL injection, but also allows the database driver to properly format quoted values and dates for you and keeps things readable. (You can also use readable names for the parameters, so that when you see them six months from now you'll know what they mean.)
var
// Your other variable declarations here
StartDate, EndDate: TDateTime;
begin
Date1 := InputBox(Whatever);
try
StartDate := StrToDate(Date1);
except
// Handle EConvertError
end;
Date2 := InputBox(Whatever);
try
EndDate := StrToDate(Date2);
except
// Handle EConvertError
end;
sql_str := 'SELECT * FROM JOB_DATA J'#13 +
'INNER JOIN CUSTOMER C'#13 +
'ON J.CUST_CODE = C.CUST_CODE'#13 +
'WHERE J.SHIP_DATE BETWEEN :StartDate AND :EndDate';
with ADOQuery5 do
begin
Close;
// No need to clear. If you're using the same query more than once,
// move the SQL assignment and the Parameter.DataType somewhere
// else, and don't set them here.
// The query can be reused just by closing, changing parameter values,
// and reopening.
SQL.Text := sql_str;
with Parameters.ParamByName('StartDate') do
begin
DataType := ftDate;
Value := StartDate;
end;
with Parameters.ParamByName('EndDate') do
begin
DataType := ftDate;
Value := EndDate;
end;
Open;
end;
frxReport2.ShowReport;
end;
When I start having problems with ADO, I log the information.
You'll need to create your own logger...but here's the jest of it...Note it will log the parameter values that are being passed to the query, including the SQL.
procedure TLogger.SetUpConnectionLogging(aParent: TComponent);
var
a_Index: integer;
begin
for a_Index := 0 to aParent.ComponentCount - 1 do
if aParent.Components[a_Index] is TAdoConnection then
begin
TAdoConnection(aParent.Components[a_Index]).OnWillExecute := WillExecute;
TAdoConnection(aParent.Components[a_Index]).OnExecuteComplete := ExecuteComplete;
end;
end;
procedure TLogger.ExecuteComplete(Connection: TADOConnection;
RecordsAffected: Integer; const Error: Error; var EventStatus: TEventStatus;
const Command: _Command; const Recordset: _Recordset);
var
a_Index: integer;
begin
AddLog('AdoConnection ExecuteComplete', True);
AddLog('Execution In MilliSeconds', IntToStr(MilliSecondsBetween(Time, FDif)));
AddLog('Execution In Seconds', IntToStr(SecondsBetween (Time, FDif)));
AddLog('Execution In Minutes', IntToStr(MinutesBetween (Time, FDif)));
AddLog('CommandText', Command.CommandText);
if Assigned(Command) then
begin
AddLog('Param Count', IntToStr(Command.Parameters.Count));
for a_Index := 0 to Command.Parameters.Count - 1 do
begin
AddLog(Command.Parameters.Item[a_Index].Name, VarToWideStr(Command.Parameters.Item[a_Index].Value));
end;
AddLog('CommandType', GetEnumName(TypeInfo(TCommandType),Integer(Command.CommandType)));
end;
AddLog('EventStatus', GetEnumName(TypeInfo(TEventStatus),Integer(EventStatus)));
if Assigned(RecordSet) then
begin
AddLog('CursorType', GetEnumName(TypeInfo(TCursorType),Integer(Recordset.CursorType)));
AddLog('LockType', GetEnumName(TypeInfo(TADOLockType),Integer(Recordset.LockType)));
end;
AddLog('RecordsAffected', IntToStr(RecordsAffected));
AddLog('AdoConnection ExecuteComplete', False);
end;
procedure TLogger.WillExecute(Connection: TADOConnection;
var CommandText: WideString; var CursorType: TCursorType;
var LockType: TADOLockType; var CommandType: TCommandType;
var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
const Command: _Command; const Recordset: _Recordset);
begin
AddLog('Connection WillExecute', True);
AddLog('Connection Name', Connection.Name);
AddLog('CommandText', CommandText);
AddLog('CommandType', GetEnumName(TypeInfo(TCommandType),Integer(CommandType)));
AddLog('EventStatus', GetEnumName(TypeInfo(TEventStatus),Integer(EventStatus)));
AddLog('CursorType', GetEnumName(TypeInfo(TCursorType),Integer(CursorType)));
AddLog('Connection WillExecute', False);
FDif := Time;
end;

GetDateFileModified for Daylight Savings Time

function DateTimeToFileTime(FileTime: TDateTime): TFileTime;
var
LocalFileTime, Ft: TFileTime;
SystemTime: TSystemTime;
begin
Result.dwLowDateTime := 0;
Result.dwHighDateTime := 0;
DateTimeToSystemTime(FileTime, SystemTime);
SystemTimeToFileTime(SystemTime, LocalFileTime);
LocalFileTimeToFileTime(LocalFileTime, Ft);
Result := Ft;
end;
function ExtractShortDate(ATimeIn: TDateTime): string;
// Convert DateTime to short date string
begin
Result := FormatDateTime('mm/dd/yyyy', ATimeIn);
end;
function ExtractTime(ATimeIn: TDateTime): string;
// Convert DateTime to am/pm time string
begin
Result := FormatDateTime('hh:mm AM/PM', ATimeIn);
end;
function GetDateFileModified(AFileName: string): string;
// Return the file modified date as a string in local time
var
SR: TSearchRec;
UTCTime: Windows.TFileTime;
GMTST: Windows.TSystemTime;
LocalST: Windows.TSystemTime;
ModifyDT: TDateTime;
TZ: Windows._TIME_ZONE_INFORMATION;
begin
Result := '';
if FindFirst(AFileName, faAnyFile, SR) = 0 then
begin
UTCTime := SR.FindData.ftLastWriteTime;
if FileTimeToSystemTime(UTCTime, GMTST) then
begin
// Get Timezone Information
if GetTimeZoneInformation(TZ) <> 0 then
if SystemTimeToTzSpecificLocalTime(#TZ, GMTST, LocalST) then
begin
ModifyDT := SystemTimeToDateTime(LocalST);
Result := ExtractShortDate(ModifyDT) + ' ' + ExtractTime(ModifyDT);
end
else
begin
TaskMessageDlg('Unable To Convert Time', 'Unable to convert SystemTime To LocalTime',
mtInformation, [mbOk], 0);
Result := '';
exit;
end;
end
else
begin
TaskMessageDlg('Unable To Convert Time', 'Unable to convert FileTime To SystemTime',
mtInformation, [mbOk], 0);
Result := '';
exit;
end;
end
else
TaskMessageDlg('File Not Found', ExtractFileName(AFileName) + ' does not exist.',
mtInformation, [mbOk], 0);
FindClose(SR);
end;
The original code posted did not return the correct time. The original code was replaced with working code so that others may find this beneficial.
Update: The code provides the correct time now thanks to all that assisted.
The problem is highlighted in the MSDN docs for FileTimeToLocalFileTime:
FileTimeToLocalFileTime uses the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, this function will take daylight saving time into account, even if the time you are converting is in standard time. You can use the following sequence of functions as an alternative.
FileTimeToSystemTime / SystemTimeToTzSpecificLocalTime / SystemTimeToFileTime
You need to use the three functions specified whenever you look at a file after a daylight savings change that was created before the change (but this method of course also works when you and the file creation are both on the same side of the daylight savings change).

Elapsed/time interval in Delphi?

I need to calculate the elapsed time (nicely formatted) between now and a file's last modification date/time, ie. something like this, only in my case, the difference can be in days, months or even years.
I tried this:
var
TimeDiff : Double;
begin
TimeDiff := Now - FileAgeEx('C:\my-file.txt');
if (TimeDiff >= 1) then
Caption := FormatDateTime('dd hh:nn:ss', TimeDiff)
else
Caption := FormatDateTime('hh:nn:ss', TimeDiff);
end;
But (1) it doesn't work and (2) I'd like a better formatting.
Ultimately my goal is to have something like this:
Time Diff < 1 day ==> display this: 12:00:01
Time Diff >= 1 day ==> display this: 25 days, 12:00:01
Time Diff >= 1 year ==> display this: 2 years, 3 months, 10 days, 12:00:01
Anyone knows how can I do that?
Thanks!
The main problem would appear to be getting hold of the last modified time of the file. I use the following code:
function LastWriteTime(const FileName: string): TFileTime;
var
AttributeData: TWin32FileAttributeData;
begin
if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, #AttributeData) then
RaiseLastOSError;
Result := AttributeData.ftLastWriteTime;
end;
function UTCFileTimeToSystemTime(const FileTime: TFileTime): TSystemTime;
//returns equivalent time in current locality, taking account of daylight saving
var
LocalFileTime: Windows.TFileTime;
begin
Windows.FileTimeToLocalFileTime(FileTime, LocalFileTime);
Windows.FileTimeToSystemTime(LocalFileTime, Result);
end;
function UTCFileTimeToDateTime(const FileTime: TFileTime): TDateTime;
begin
Result := SystemTimeToDateTime(UTCFileTimeToSystemTime(FileTime));
end;
You call LastWriteTime to get the last modified time in file time format. Then call UTCFileTimeToDateTime to convert into TDateTime accounting for the prevailing local time zone of the machine. You can then compare that value with Now.
As regards the formatting, you already appear to know how to do that. You basic approach will work and you just need to flesh out the details.
In the comments you say that
FormatDateTime('dd hh:nn:ss', 2.9);
shows a 1 for the day when you would expect a 2. The problem is that this function formats dates rather than time intervals. The value 2.9 is not treated as an elapsed time, rather it is treated as an absolute date/time, 2.9 days after the Delphi epoch. I would use Trunc and Frac to obtain number of days, and the part of days respectively, and work from there.
Days := Trunc(TimeDiff);
Time := Frac(TimeDiff);
The following code, extracted directly from my codebase, may give you some pointers. Note that its input is in seconds, but it should set you on the right path.
function CorrectPlural(const s: string; Count: Integer): string;
begin
Result := IntToStr(Count) + ' ' + s;
if Count<>1 then begin
Result := Result + 's';
end;
end;
function HumanReadableTime(Time: Double): string;
//Time is in seconds
const
SecondsPerMinute = 60;
SecondsPerHour = 60*SecondsPerMinute;
SecondsPerDay = 24*SecondsPerHour;
SecondsPerWeek = 7*SecondsPerDay;
SecondsPerYear = 365*SecondsPerDay;
var
Years, Weeks, Days, Hours, Minutes, Seconds: Int64;
begin
Try
Years := Trunc(Time/SecondsPerYear);
Time := Time - Years*SecondsPerYear;
Weeks := Trunc(Time/SecondsPerWeek);
Time := Time - Weeks*SecondsPerWeek;
Days := Trunc(Time/SecondsPerDay);
Time := Time - Days*SecondsPerDay;
Hours := Trunc(Time/SecondsPerHour);
Time := Time - Hours*SecondsPerHour;
Minutes := Trunc(Time/SecondsPerMinute);
Time := Time - Minutes*SecondsPerMinute;
Seconds := Trunc(Time);
if Years>5000 then begin
Result := IntToStr(Round(Years/1000))+' millennia';
end else if Years>500 then begin
Result := IntToStr(Round(Years/100))+' centuries';
end else if Years>0 then begin
Result := CorrectPlural('year', Years) + ' ' + CorrectPlural('week', Weeks);
end else if Weeks>0 then begin
Result := CorrectPlural('week', Weeks) + ' ' + CorrectPlural('day', Days);
end else if Days>0 then begin
Result := CorrectPlural('day', Days) + ' ' + CorrectPlural('hour', Hours);
end else if Hours>0 then begin
Result := CorrectPlural('hour', Hours) + ' ' + CorrectPlural('minute', Minutes);
end else if Minutes>0 then begin
Result := CorrectPlural('minute', Minutes);
end else begin
Result := CorrectPlural('second', Seconds);
end;
Except
Result := 'an eternity';
End;
end;

On a very slow query, how do I indicate percentage of progress

I'm using ZEOS components to connect to an (ancient) MDB database.
I'm doing a query that reads in lots of data to bridge into a different database.
Is there a way to indicate progress as a percentage?
procedure TForm13.ActionReadInMemoryExecute(Sender: TObject);
var
QueryLine: string;
FullQuery: string;
Tablename: string;
i: integer;
begin
i:= 0;
TableMeter.DisableControls;
try
TableMeter.First;
FullQuery:= '';
while not TableMeter.eof do begin
Tablename:= TableMeter.FieldByName('tabelnaam').AsString;
QueryLine:= ReplaceStr(ImportQuerySjabloon, cTabelname, Tablename);
FullQuery:= FullQuery + QueryLine;
if (TableMeter.RecNo < (TableMeter.RecordCount -1)) then begin
FullQuery:= FullQuery + ' UNION ALL ';
end;
TableMeter.Next;
end; {while}
QueryImportMeterreadings.Close;
QueryImportMeterreadings.SQL.Text:= FullQuery;
QueryImportMeterreadings.Open; <<-- takes a long time
finally
TableMeter.EnableControls;
end;
end;
Is there a way to indicate progress of the query, or can I only do this if I split up the individual queries and eliminate the UNION's.
It takes about 1 minute to run, involving 8 unions.
I don't see any event that I can use for this purpose:
Or should I fake an OnCalcField on a field in the Query to do this (not sure if that will even work in principle).
Or attach a sequence? nope, gives unsupported operation on a Access DB
I say split up the individual queries and eliminate the union, make a timer around each query, depending on the avg time taken * number of queries remaining you should give an estimate / update a text field to say x out of y queries completed (time remaining: -time-)
I would split the huge query into individual queries; in code, you iterate over each query's result set and manually insert the values into a clientdataset (cds). The cds can be connected to a dbgrid. Then you can show when each query completes - you could also show progress after each tuple is handled, but you won't know how many tuples in total there are, unless you perform a separate query which returns a count of tuples. The problem with using such an unconnected cds is that you have to define the fields in code. Here is an example of something similar which I wrote last night - the queries all update one field in the cds.
const
field1 = 'id';
field2 = 'customer name';
field3 = 'total debt';
procedure TTotalCustDebt.FormCreate(Sender: TObject);
var
strings: tstrings;
begin
with qTotalDebt do // this is the clientdataset
begin
fielddefs.add (field1, ftInteger, 0, false);
fielddefs.add (field2, ftString, 32, false);
fielddefs.add (field3, ftInteger, 0, false);
createdataset;
fieldbyname (field1).visible:= false;
open;
addindex ('idx0', field2, [], '', '', 0);
addindex ('idx1', field2, [ixDescending], '', '', 0);
addindex ('idx2', field3, [], '', '', 0);
addindex ('idx3', field3, [ixDescending], '', '', 0);
strings:= tstringlist.create;
getindexnames (strings);
strings.free;
end;
end;
procedure TTotalCustDebt.PopulateCDS;
begin
dsTotalDebt.dataset:= nil;
with qTotalDebt do
begin
emptydataset;
indexfieldnames:= field1; // initially sort by customer.id
end;
with qDBills do
begin
params[0].asdate:= dt;
open;
while not eof do
begin
qTotalDebt.append;
qTotalDebt.fieldbyname (field1).asinteger:= qDBillsID.asinteger;
qTotalDebt.fieldbyname (field2).asstring:= qDBillsName.asstring;
qTotalDebt.fieldbyname (field3).asinteger:= qDBillsTot.asinteger;
qTotalDebt.post;
next
end;
close
end;
// show progress indicator
with qDReceipts do
begin
params[0].asdate:= dt;
open;
while not eof do
begin
if qTotalDebt.findkey ([qDReceiptsID.asinteger]) then
begin // customer already exists
qTotalDebt.edit;
qTotalDebt.fieldbyname (field3).asinteger:= - qDReceiptsTot.asinteger
+ qTotalDebt.fieldbyname (field3).asinteger;
end
else
begin // add new record
qTotalDebt.append;
qTotalDebt.fieldbyname (field1).asinteger:= qDReceiptsID.asinteger;
qTotalDebt.fieldbyname (field2).asstring:= qDReceiptsName.asstring;
qTotalDebt.fieldbyname (field3).asinteger:= - qDReceiptsTot.asinteger;
end;
qTotalDebt.post;
next
end;
close
end;
// show progress indicator
// more queries
// at end, attach the clientdataset to the TDataSource
dsTotalDebt.dataset:= qTotalDebt;
end;

Resources