I have a form where I have to grids connected to 2 tables in master-detail
This works like it is supposed to except for one thing - I have a button that allows me to put a record in the detail table using the key from master table. But I can't get the detail view to update unless I scroll away from the current record in master grid and then back.
How do I force the query for the detail grid to update?
(I am using a version 4.6 of UniDAC and Delphi XE3 - it is an old application that I am trying to fix. We don't use Delphi anymore)
Force details to update
Re: Force details to update
Hello,
We cannot reproduce the problem even on the version 4.6.11. If you are using a non-standard DBGrid, check this behavior on the standard grid. If the situation repeats, please send the code you use for data insert
We cannot reproduce the problem even on the version 4.6.11. If you are using a non-standard DBGrid, check this behavior on the standard grid. If the situation repeats, please send the code you use for data insert
Re: Force details to update
The grid used is cxGrid from DevExpress
The code for data insert could be like this
procedure TfrmContacts.actCreateMailExecute(Sender: TObject);
var
Logbook: TLogbook;
SubjText: string;
begin
GrabVariables;
Logbook := TLogbook.Create;
if lContactMail <> '' then
begin
try
try
SubjText := Format('Email til %s!', [lContactName]);
Mailing.Send(Settings.Mail.SenderMail, lContactMail, SubjText, '', True);
Logbook.EntryDate := Date;
Logbook.EntryText := Format('Sendt email til %s!', [lContactName]);
Logbook.EntryType := GuidLogbookMail;
Logbook.EntryContact := lContactGuid;
Logbook.Save;
except
on E:exception do
Logfile.Error('F_Contacts.actCreateMailExecute: ' + E.Message);
end;
finally
Logbook.Free;
end;
end
else
ShowMessage(Format(cxGetResourceString(@sLangMailAddressError), [lContactMail]));
GoHome(lContactGuid);
end;
Using the class
//*********************************************************
// Funktioner brugt til vedligehold af logbog
//*********************************************************
unit U_Logbook;
interface
uses
{$Include UniDACCommon.inc}
Db, MemDS, DbAccess, Uni,
Classes;
{$M+}
type
TLogbook = Class
private
fEntryGuid: string;
fEntryDate: TDate;
fEntryType: string;
fEntryText: string;
fEntryContact: string;
procedure SetText(const Value: string);
protected
procedure AddFields; virtual;
procedure AddParams; virtual;
procedure AddValues; virtual;
published
fQuery: TUniQuery;
constructor Create;
destructor Destroy; override;
property EntryGuid: string read fEntryGuid write fEntryGuid;
property EntryDate: TDate read fEntryDate write fEntryDate;
property EntryType: string read fEntryType write fEntryType;
property EntryText: string read fEntryText write SetText;
property EntryContact: string read fEntryContact write fEntryContact;
public
procedure Save;
end;
const
GuidLogbookMail = '{11111111-1111-1111-1111-111111111111}';
NewLogText = 'Ny log post!';
implementation
uses
System.SysUtils, System.StrUtils, System.DateUtils,
Vcl.Forms, Vcl.Dialogs, Vcl.Controls,
F_UniConn,
U_FormatSettingsHelper,
U_Logfile, U_Utilities, U_Network,
U_AppDb, U_User;
{ TLogbook }
constructor TLogbook.Create;
begin
inherited;
fQuery := frmUniConn.CreateQuery;
fEntryGuid := GuidEmpty;
fEntryDate := Date;
fEntryType := GuidEmpty;
fEntryText := '';
fEntryContact := GuidEmpty;
end;
destructor TLogbook.Destroy;
begin
try
if fQuery.SQL.Text <> '' then
fQuery.Execute;
finally
fQuery.Free;
inherited;
end;
end;
procedure TLogbook.AddFields;
begin
try
fQuery.SQL.Clear;
fQuery.SQL.Add('INSERT INTO ' + TableLogbook);
fQuery.SQL.Add(' ( ');
fQuery.SQL.Add(' fldlog_guid');
fQuery.SQL.Add(' ,fldlog_day');
fQuery.SQL.Add(' ,fldlog_date');
fQuery.SQL.Add(' ,fldlog_year');
fQuery.SQL.Add(' ,fldlog_month');
fQuery.SQL.Add(' ,fldlog_week');
fQuery.SQL.Add(' ,fldlog_text');
fQuery.SQL.Add(' ,fldlog_type');
fQuery.SQL.Add(' ,fldlog_contact');
fQuery.SQL.Add(' ,fldcreated_computer');
fQuery.SQL.Add(' ,fldcreated_by');
fQuery.SQL.Add(' ,fldcreated_date');
fQuery.SQL.Add(' ,fldupdated_computer');
fQuery.SQL.Add(' ,fldupdated_by');
fQuery.SQL.Add(' ,fldupdated_date');
except
on E:exception do
Logfile.Error('U_Logbook.AddFields: ' + E.Message);
end;
end;
procedure TLogbook.AddParams;
begin
try
fQuery.SQL.Add(' ) values ( ');
fQuery.SQL.Add(' :fldlog_guid');
fQuery.SQL.Add(' ,:fldlog_day');
fQuery.SQL.Add(' ,:fldlog_date');
fQuery.SQL.Add(' ,:fldlog_year');
fQuery.SQL.Add(' ,:fldlog_month');
fQuery.SQL.Add(' ,:fldlog_week');
fQuery.SQL.Add(' ,:fldlog_text');
fQuery.SQL.Add(' ,:fldlog_type');
fQuery.SQL.Add(' ,:fldlog_contact');
fQuery.SQL.Add(' ,:fldcreated_computer');
fQuery.SQL.Add(' ,:fldcreated_by');
fQuery.SQL.Add(' ,:fldcreated_date');
fQuery.SQL.Add(' ,:fldupdated_computer');
fQuery.SQL.Add(' ,:fldupdated_by');
fQuery.SQL.Add(' ,:fldupdated_date');
except
on E:exception do
Logfile.Error('U_Logbook.AddParams: ' + E.Message);
end;
end;
procedure TLogbook.AddValues;
var
myYear, myMonth, myDay: Word;
myHour, myMin, mySec, myMilli : Word;
LogTime: TDate;
begin
try
fQuery.SQL.Add(' ) ');
fQuery.ParamByName('fldlog_guid').AsString := GuidCreate;
fQuery.ParamByName('fldlog_day').AsString := FormatSettings.ISOLongDayNames(DayOfTheWeek(fEntryDate));
fQuery.ParamByName('fldlog_date').AsDateTime := fEntryDate;
fQuery.ParamByName('fldlog_year').AsInteger := YearOf(fEntryDate);
fQuery.ParamByName('fldlog_month').AsInteger := MonthOf(fEntryDate);
fQuery.ParamByName('fldlog_week').AsInteger := WeekOf(fEntryDate);
fQuery.ParamByName('fldlog_text').AsString := fEntryText;
fQuery.ParamByName('fldlog_type').AsString := fEntryType;
fQuery.ParamByName('fldlog_contact').AsString := fEntryContact;
fQuery.ParamByName('fldcreated_computer').AsString := Network.ComputerName;
fQuery.ParamByName('fldcreated_by').AsString := User.ID;
fQuery.ParamByName('fldupdated_computer').AsString := Network.ComputerName;
fQuery.ParamByName('fldupdated_by').AsString := User.ID;
LogTime := DateOf(fEntryDate);
DecodeDate(LogTime, myYear, myMonth, myDay);
DecodeTime(Now, myHour, myMin, mySec, myMilli);
LogTime := EncodeDateTime(myYear, myMonth, myDay, myHour, myMin, mySec, myMilli);
fQuery.ParamByName('fldcreated_date').AsDateTime := LogTime;
fQuery.ParamByName('fldupdated_date').AsDateTime := LogTime;
except
on E:exception do
Logfile.Error('U_Logbook.AddValues: ' + E.Message);
end;
end;
procedure TLogbook.Save;
begin
try
AddFields;
AddParams;
AddValues;
except
on E:exception do
Logfile.Error('U_Logbook.Save: ' + E.Message);
end;
end;
// Getters and setters
procedure TLogbook.SetText(const Value: string);
begin
if Value = '' then
fEntryText := NewLogText
else
fEntryText := Value;
end;
end.
But the record is added OK the only problem is that is is not shown in the detail grid until i scroll away from the master record and then back - what I need is a way of forcing the detail to reload its records.
But if that isn't possible then the users will have to live with the way it works now
The code for data insert could be like this
procedure TfrmContacts.actCreateMailExecute(Sender: TObject);
var
Logbook: TLogbook;
SubjText: string;
begin
GrabVariables;
Logbook := TLogbook.Create;
if lContactMail <> '' then
begin
try
try
SubjText := Format('Email til %s!', [lContactName]);
Mailing.Send(Settings.Mail.SenderMail, lContactMail, SubjText, '', True);
Logbook.EntryDate := Date;
Logbook.EntryText := Format('Sendt email til %s!', [lContactName]);
Logbook.EntryType := GuidLogbookMail;
Logbook.EntryContact := lContactGuid;
Logbook.Save;
except
on E:exception do
Logfile.Error('F_Contacts.actCreateMailExecute: ' + E.Message);
end;
finally
Logbook.Free;
end;
end
else
ShowMessage(Format(cxGetResourceString(@sLangMailAddressError), [lContactMail]));
GoHome(lContactGuid);
end;
Using the class
//*********************************************************
// Funktioner brugt til vedligehold af logbog
//*********************************************************
unit U_Logbook;
interface
uses
{$Include UniDACCommon.inc}
Db, MemDS, DbAccess, Uni,
Classes;
{$M+}
type
TLogbook = Class
private
fEntryGuid: string;
fEntryDate: TDate;
fEntryType: string;
fEntryText: string;
fEntryContact: string;
procedure SetText(const Value: string);
protected
procedure AddFields; virtual;
procedure AddParams; virtual;
procedure AddValues; virtual;
published
fQuery: TUniQuery;
constructor Create;
destructor Destroy; override;
property EntryGuid: string read fEntryGuid write fEntryGuid;
property EntryDate: TDate read fEntryDate write fEntryDate;
property EntryType: string read fEntryType write fEntryType;
property EntryText: string read fEntryText write SetText;
property EntryContact: string read fEntryContact write fEntryContact;
public
procedure Save;
end;
const
GuidLogbookMail = '{11111111-1111-1111-1111-111111111111}';
NewLogText = 'Ny log post!';
implementation
uses
System.SysUtils, System.StrUtils, System.DateUtils,
Vcl.Forms, Vcl.Dialogs, Vcl.Controls,
F_UniConn,
U_FormatSettingsHelper,
U_Logfile, U_Utilities, U_Network,
U_AppDb, U_User;
{ TLogbook }
constructor TLogbook.Create;
begin
inherited;
fQuery := frmUniConn.CreateQuery;
fEntryGuid := GuidEmpty;
fEntryDate := Date;
fEntryType := GuidEmpty;
fEntryText := '';
fEntryContact := GuidEmpty;
end;
destructor TLogbook.Destroy;
begin
try
if fQuery.SQL.Text <> '' then
fQuery.Execute;
finally
fQuery.Free;
inherited;
end;
end;
procedure TLogbook.AddFields;
begin
try
fQuery.SQL.Clear;
fQuery.SQL.Add('INSERT INTO ' + TableLogbook);
fQuery.SQL.Add(' ( ');
fQuery.SQL.Add(' fldlog_guid');
fQuery.SQL.Add(' ,fldlog_day');
fQuery.SQL.Add(' ,fldlog_date');
fQuery.SQL.Add(' ,fldlog_year');
fQuery.SQL.Add(' ,fldlog_month');
fQuery.SQL.Add(' ,fldlog_week');
fQuery.SQL.Add(' ,fldlog_text');
fQuery.SQL.Add(' ,fldlog_type');
fQuery.SQL.Add(' ,fldlog_contact');
fQuery.SQL.Add(' ,fldcreated_computer');
fQuery.SQL.Add(' ,fldcreated_by');
fQuery.SQL.Add(' ,fldcreated_date');
fQuery.SQL.Add(' ,fldupdated_computer');
fQuery.SQL.Add(' ,fldupdated_by');
fQuery.SQL.Add(' ,fldupdated_date');
except
on E:exception do
Logfile.Error('U_Logbook.AddFields: ' + E.Message);
end;
end;
procedure TLogbook.AddParams;
begin
try
fQuery.SQL.Add(' ) values ( ');
fQuery.SQL.Add(' :fldlog_guid');
fQuery.SQL.Add(' ,:fldlog_day');
fQuery.SQL.Add(' ,:fldlog_date');
fQuery.SQL.Add(' ,:fldlog_year');
fQuery.SQL.Add(' ,:fldlog_month');
fQuery.SQL.Add(' ,:fldlog_week');
fQuery.SQL.Add(' ,:fldlog_text');
fQuery.SQL.Add(' ,:fldlog_type');
fQuery.SQL.Add(' ,:fldlog_contact');
fQuery.SQL.Add(' ,:fldcreated_computer');
fQuery.SQL.Add(' ,:fldcreated_by');
fQuery.SQL.Add(' ,:fldcreated_date');
fQuery.SQL.Add(' ,:fldupdated_computer');
fQuery.SQL.Add(' ,:fldupdated_by');
fQuery.SQL.Add(' ,:fldupdated_date');
except
on E:exception do
Logfile.Error('U_Logbook.AddParams: ' + E.Message);
end;
end;
procedure TLogbook.AddValues;
var
myYear, myMonth, myDay: Word;
myHour, myMin, mySec, myMilli : Word;
LogTime: TDate;
begin
try
fQuery.SQL.Add(' ) ');
fQuery.ParamByName('fldlog_guid').AsString := GuidCreate;
fQuery.ParamByName('fldlog_day').AsString := FormatSettings.ISOLongDayNames(DayOfTheWeek(fEntryDate));
fQuery.ParamByName('fldlog_date').AsDateTime := fEntryDate;
fQuery.ParamByName('fldlog_year').AsInteger := YearOf(fEntryDate);
fQuery.ParamByName('fldlog_month').AsInteger := MonthOf(fEntryDate);
fQuery.ParamByName('fldlog_week').AsInteger := WeekOf(fEntryDate);
fQuery.ParamByName('fldlog_text').AsString := fEntryText;
fQuery.ParamByName('fldlog_type').AsString := fEntryType;
fQuery.ParamByName('fldlog_contact').AsString := fEntryContact;
fQuery.ParamByName('fldcreated_computer').AsString := Network.ComputerName;
fQuery.ParamByName('fldcreated_by').AsString := User.ID;
fQuery.ParamByName('fldupdated_computer').AsString := Network.ComputerName;
fQuery.ParamByName('fldupdated_by').AsString := User.ID;
LogTime := DateOf(fEntryDate);
DecodeDate(LogTime, myYear, myMonth, myDay);
DecodeTime(Now, myHour, myMin, mySec, myMilli);
LogTime := EncodeDateTime(myYear, myMonth, myDay, myHour, myMin, mySec, myMilli);
fQuery.ParamByName('fldcreated_date').AsDateTime := LogTime;
fQuery.ParamByName('fldupdated_date').AsDateTime := LogTime;
except
on E:exception do
Logfile.Error('U_Logbook.AddValues: ' + E.Message);
end;
end;
procedure TLogbook.Save;
begin
try
AddFields;
AddParams;
AddValues;
except
on E:exception do
Logfile.Error('U_Logbook.Save: ' + E.Message);
end;
end;
// Getters and setters
procedure TLogbook.SetText(const Value: string);
begin
if Value = '' then
fEntryText := NewLogText
else
fEntryText := Value;
end;
end.
But the record is added OK the only problem is that is is not shown in the detail grid until i scroll away from the master record and then back - what I need is a way of forcing the detail to reload its records.
But if that isn't possible then the users will have to live with the way it works now
Re: Force details to update
Since you are inserting data calling INSERT ..., you should update data manually only.
Re: Force details to update
'you should update data manually only' - I am not sure what you mean by this
Re: Force details to update
You should call explicitly the refresh method of DataSet