DELPHI -how can i insert tlabeledit to dynamic tabsheet in TpageControl - delphi

i need to have this ini file which i mentioned it in my dynamic ttabsheet which i created it dynamically ! as you see ini file , it has three parts each start with 001-002-003 and then let those which start with 001 in first tabsheet , those start with 002 in second tabsheet and those start with 003 in thired tabsheet in my page control .
procedure TDlg_XRechnung.INIDatei_einlesen;
var NeueSeite : TTabSheet;
i,l: Integer;
begin
try
INIDatei := TIniFile.Create(INIDateiname);
Version := INIDatei.ReadInteger('INI','Version', 1);
DebugMode := INIDatei.ReadInteger('INI','DebugMode', 0);
PDFDatei := INIDatei.ReadString('PDF','PDFName', '');
XRechnungDatei := INIDatei.ReadString('PDF','XInvoice', '');
Edit_LeitwegeID.Text := INIDatei.ReadString('InvData','BT-10', '');
Edit_Bestellnummer.Text := INIDatei.ReadString('InvData','BT-13', '');
Edit_Projektreferenz.Text := INIDatei.ReadString('InvData','BT-11', '');
Edit_Vertragsnummer.Text := INIDatei.ReadString('InvData','BT-12', '');
Edit_Rechnungsnummer.Text := INIDatei.ReadString('InvData','BT-1', '');
Edit_Rechnungsdatum.Date := INIDatei.ReadDate('InvData','BT-2', now);
Edit_Leistungsdatum.Date := INIDatei.ReadDate('InvData','BT-9', now);
Edit_Bemerkung.Text := INIDatei.ReadString('InvData','BT-22', '');
Edit_Rechnungsersteller_Name.Text := INIDatei.ReadString('InvErst','BT-27', '');
Edit_Rechnungsersteller_UST_ID.Text := INIDatei.ReadString('InvErst','BT-31', '');
Edit_Rechnungsersteller_Adresse.Text := INIDatei.ReadString('InvErst','BT-35', '') + INIDatei.ReadString('InvErst','BT-36', '');
Edit_Rechnungsersteller_PLZ.Text := INIDatei.ReadString('InvErst','BT-38', '');
Edit_Rechnungsersteller_Ort.Text := INIDatei.ReadString('InvErst','BT-37', '');
Edit_Rechnungsersteller_Land.Text := INIDatei.ReadString('InvErst','BT-40', '');
Edit_Rechnungsempfänger_Name.Text := INIDatei.ReadString('InvEmp','BT-44','');
Edit_Rechnungsempfänger_Adresse.Text := INIDatei.ReadString('InvEmp','BT-50', '') + INIDatei.ReadString('InvEmp','BT-51', '');
Edit_Rechnungsempfänger_PLZ.Text := INIDatei.ReadString('InvEmp','BT-53', '');
Edit_Rechnungsempfänger_Ort.Text := INIDatei.ReadString('InvEmp','BT-52', '');
Edit_Rechnungsempfänger_Land.Text := INIDatei.ReadString('InvEmp','BT-55', '');
AnzahlPositinen := INIDatei.ReadInteger('POS','AnzPos', 1);
for i := 1 to AnzahlPositinen do
begin
NeueSeite := TTabSheet.Create(PC_Positionen);
NeueSeite.PageControl := PC_Positionen;
NeueSeite.Caption := 'Position ' + IntToStr(i);
end;
finally
INIDatei.Free;
end;
end;
this is my ini file
[POS]
AnzPos=3
;xxx ist die Zähl-Nr der Positionen mit Vornull
001BT-155=643
001BT-153=EID32 REI90-XC1-AN5C Bereich D
001BT-129=219,85
001BT-130=M2
001BT-152=19
001BT-146=43,50
001BT-154=EID 32/120 Spannbetondeckenplatte; d = 32 cmg
001BT-131=9563,48
001BT-132=11380,54
002BT-155=299
002BT-153=Auflagerstreifen 10X70X10
002BT-129=7
001BT-130=MTR
002BT-152=19
002BT-146=42,5
002BT-154=Hartgummiauflagerstreifen 10 x 70 mm auf 10 m Roll
002BT-131=297,5
002BT-132=354,03
003BT-155=40
003BT-153=Stahlwechsel, grundiert (F0
003BT-129=411.04
001BT-130=CMT
003BT-152=19
003BT-146=3,8
003BT-154=Stahlwechsel, grundiert (Flachstahl) Netto- nicht rabattierfähig.
003BT-131=1561.95
003BT-132=1858,72
i dont know how to do !? any idea ?
Thanks

You have every data in the [POS] section in the INI file you show while your code expact section [InvData]. Fix that.
In your PageControl, it is not enough to create TabSheet, you must also create a new frame instance to be hosted into each page. All frames are the same class and host edit, checkbox and so on to host data.
Then you must read the data, except count, inside you for-loop to create the page control. To read the data, you must combine the loop variable with fixed part to build the key. Something like this:
Edit1.Text := IniFile.ReadString('POS', Format('%03.3dBT-154', [I]), 'Some default value');
In my example, Edit1 is one field of the frame.

Related

Add attachment to message in Slack API

How can I add a large text file (actually a HTML file) as an attachment to a channel in Slack? A working example would be great. I use SDriver - the included demo works fine to send some strings, but I don't find anything on how to use attachments.
What I did so far:
procedure TForm1.SendActionExecute(Sender: TObject);
var
LWebHook: IMessageBuffer;
LMessage: IMessage;
LStopWatch: TStopWatch;
vAttachment: IAttachment;
vField: IFields;
begin
LStopWatch := TStopWatch.StartNew;
LMessage := TMessage.Create(EditMessage.Text + ' [' + TimeToStr(Now) + ']');
LMessage.UserName := EditUserName.Text;
LMessage.Icon_URL := EditIcon_URL.Text;
LMessage.Icon_Emoji := EditIcon_Emoji.Text;
LMessage.Channel := EditChannel.Text;
vAttachment := LMessage.AddAttachment;
vField := vAttachment.AddFields;
vField.Title := 'Title';
vField.Value := 'Value';
///////////////////////////////////////////////////////
// How can I add a large text file as attachment here ?
///////////////////////////////////////////////////////
LWebHook := TIncomingWebHook.Create(EditWebHookURL.Text, False);
LWebHook.Push(LMessage);
LWebHook.Flush;
end;

Printing a range of pages in MS Word using OLE (Imprimir em ole object (word application) com delphi)

I'm trying to print a range of pages from my Ole Object object, but it's not working for me - I get a "type mismatch" exception when the call to Doc.PrintOut executes.
Can anyone help me to avoid this error? Below is the code used in tests:
if (OpenDialog1.Execute) then
begin
// Cria objeto principal de controle do Word
WinWord := CreateOleObject('Word.Application');
if (not (VarIsEmpty(WinWord))) then
begin
// Mostra o Word
try
WinWord.Visible := false;
Docs := WinWord.Documents;
// Abre um documento
Doc := Docs.Open(OpenDialog1.FileName);
//Doc.PrintOut(false, Range, 1, 2);
//Doc.PrintOut(Copies := 2);
vFrom := 1;
vTo := 2;
Doc.PrintOut(Background := false, Append := false, Range := wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
// erro recebido: tipo não correspondente
finally
// Fecha o Word
WinWord.Quit;
end;
end;
showmessage('Fim!');
end;
I tried your code in D7, using the English-language version of Office2007.
I get an exception error on Doc.PrintOut which is Delphi's usual "Type Mismatch". By experimenting with the arguments passed to Doc.PrintOut, I think it's the
Range := wdPrintFromTo
that's causing the exception, because a "range" would normally be a block of text, whereas wdPrintFromTo is a numeric constant (but then, the Word macro I mention below uses a numeric constant for the range, so maybe Word just doesn't like th one you used).
Then, I got Word to record a macro to print the first two pages of a 6-page doc, and found that I could then get your code to work by making these changes:
replace vFrom and vTo, which I assume are OleVariants, by two integers iFrom and iTo (I don't think that this really matters, just makes it easier to check that the page-range argument is correctly constructed.
replace the code from "vFrom := [...] to Doc.PrintOut by
iFrom := 1;
iTo := 2;
Doc.PrintOut(
Range := wdPrintRangeOfPages,
Item := wdPrintDocumentContent,
Copies := 1,
Pages := IntToStr(iFrom) + '-' + IntToStr(iTo),
PageType := wdPrintAllPages,
ManualDuplexPrint := False,
Collate := True,
Background := True,
PrintToFile := False,
PrintZoomColumn := 0,
PrintZoomRow := 0,
PrintZoomPaperWidth := 0,
PrintZoomPaperHeight := 0
);
Obviously, the arguments this PrintOut includes are ones which the Word macro included. and typically some of them would probably be superfluous.
Thanks for all!
MartynA, Jan Doggen, David Heffernan, Mason Wheeler and all others!!
I get solution with the code below:
<pre><code>
procedure TForm1.Button1Click(Sender: TObject);
var
WinWord, Docs, Doc: Variant;
vNomeImpressoraPadraoOriginal : string;
vFrom, vTo : integer;
begin
if (OpenDialog1.Execute) then
begin
try
// Cria objeto principal de controle do Word
WinWord := CreateOleObject('Word.Application');
if (not (VarIsEmpty(WinWord))) then
begin
// Mostra o Word
try
WinWord.Visible := false;
Docs := WinWord.Documents;
// Abre um documento
Doc := Docs.Open(OpenDialog1.FileName);
vFrom := 1;
vTo := 2;
// referência do comando: http://support.microsoft.com/kb/176069/EN-US
Doc.PrintOut(0, 0, '3', '', '1', '2');
// previous code
// Doc.PrintOut(Background := false, Append := false, Range := wdPrintFromTo, OutputFileName := EmptyParam, From := vFrom, To := vTo);
finally
// Fecha o Word
WinWord.ActiveDocument.Close(SaveChanges := 0);
WinWord.Quit;
WinWord := Unassigned;
Docs := Unassigned;
Doc := Unassigned;
end;
end;
showmessage('Fim!');
finally
end;
end;
end;
</code></pre>

Cannot save some fields and an image

I tried this code to save some fields and an image.
I use MySQL and zzeos for connectinf to the database.
How to fix this code ?
procedure Tfbiodata.btnSaveClick(Sender: TObject);
var
gambar : TMemoryStream;
begin
if (edtnis.Text='') or (edtname.Text='') or (cmbjk.Text='') or (edtempat.Text='') or (edtgl.Text='') or (cmbtingkatan.Text='') then
begin
ShowMessage('Maaf !!! Data Anda Belum Lengkap ....');
exit;
end;
begin
zbiodata2.Open;
zbiodata2.Append;
zbiodata2.FieldByName('NIS').AsString := edtnis.Text;
zbiodata2.FieldByName('Nama_siswa').AsString := edtname.Text;
zbiodata2.FieldByName('Jenis_kelamin').AsString := cmbjk.Text;
zbiodata2.FieldByName('Tempat_lahir').AsString := edtempat.Text;
zbiodata2.FieldByName('Tanggal_lahir').AsString := edtgl.Text;
zbiodata2.FieldByName('Tingkatan').AsString := cmbtingkatan.Text;
zbiodata2.FieldByName('Hasil_indentifkasi').AsString := lblhasil.Caption;
zbiodata2.FieldByName('Metode_pembeaaran').AsString := memo1.Text;
try
convertobmp(openpicture.FileName);
gambar := TMemorystream.Create;
image1.Picture.Graphic.SaveToStream(gambar);
zbiodata2.SQL.Text := 'insert into biodata (gambar) values (:p0)';
zbiodata2.Params[0].LoadFromStream(gambar,ftBlob);
zbiodata2.Post;
zbiodata2.ExecSQL;
except
on E:Exception do
ShowMessage('sorry this a problem .' + #13 + 'Error : ' + E.Message);
end;
end;
end;
When I run this code, I get the error "sorry this is a problem . Error: List index out of bounds(2)"
After calling image1.Picture.Graphic.SaveToStream(gambar), set gambar.Position back to 0 before then calling zbiodata2.Params[0].LoadFromStream(gambar,ftBlob):
image1.Picture.Graphic.SaveToStream(gambar);
gambar.Position := 0; // <-- add this
zbiodata2.Params[0].LoadFromStream(gambar,ftBlob);
With that said, you are using zbiodata2 for two different operations at the same time - editing a new row that is being appended, and executing an SQL statement. Don't do that! Use separate components for each operation.
If the image is being saved into the same row that is being appended, don't bother executing a separate SQL INSERT statement at all. Save the image data directly into the row's gambar TField before then calling zbiodata2.Post():
procedure Tfbiodata.btnSaveClick(Sender: TObject);
var
gambar : TStream;
begin
if (edtnis.Text='') or (edtname.Text='') or (cmbjk.Text='') or (edtempat.Text='') or (edtgl.Text='') or (cmbtingkatan.Text='') then
begin
ShowMessage('Maaf !!! Data Anda Belum Lengkap ....');
Exit;
end;
try
convertobmp(openpicture.FileName);
zbiodata2.Open;
zbiodata2.Append;
try
zbiodata2.FieldByName('NIS').AsString := edtnis.Text;
zbiodata2.FieldByName('Nama_siswa').AsString := edtname.Text;
zbiodata2.FieldByName('Jenis_kelamin').AsString := cmbjk.Text;
zbiodata2.FieldByName('Tempat_lahir').AsString := edtempat.Text;
zbiodata2.FieldByName('Tanggal_lahir').AsString := edtgl.Text;
zbiodata2.FieldByName('Tingkatan').AsString := cmbtingkatan.Text;
zbiodata2.FieldByName('Hasil_indentifkasi').AsString := lblhasil.Caption;
zbiodata2.FieldByName('Metode_pembeaaran').AsString := memo1.Text;
if (image1.Picture.Graphic <> nil) and (not image1.Picture.Graphic.Empty) then
begin
gambar := TMemoryStream.Create;
try
image1.Picture.Graphic.SaveToStream(gambar);
gambar.Position := 0;
(zbiodata2.FieldByName('gambar') as TBlobField).LoadFromStream(gambar);
finally
gambar.Free;
end;
{
Alternatively:
gambar := zbiodata2.CreateBlobStream(zbiodata2.FieldByName('gambar'), bmWrite);
try
image1.Picture.Graphic.SaveToStream(gambar);
finally
gambar.Free;
end;
}
end;
zbiodata2.Post;
except
zbiodata2.Cancel;
raise;
end;
except
on E:Exception do
ShowMessage('sorry this a problem .' + #13 + 'Error : ' + E.Message);
end;
end;
If you are still having problems after that, you need to explain what is actually going wrong, what errors you are seeing, etc.

Exporting DBgrid to CSV?

I have a DB grid which is sorted (the user clicked a few radio buttons and checkboxes to influence the display).
I would like to export all of the data (not just what is visible in the grid), sorted identically, to CSV - how do I do so? The data - not the user settings, just to clarify.
Thanks in advance for any help
[Update] I build sqlQuery bit by bit, depending on the user's settings of checkboxes & radio groups, then, when one of them changes, I
ActivityADQuery.SQL.Clear();
ActivityADQuery.SQL.Add(sqlQuery);
ActivityADQuery.Open(sqlQuery);
That is to say that there isn't a hard coded query, it varies and I want to export the current settings.
I don't know enough if I want to export from the grid or the dataset (I am just not a db guy, this is my first DBgrid), but I suspect that I want the grid, because it has a subset of fields of he dataset.
I guess that TJvDBGridCSVExport is a Jedi component(?) I have tried to avoid them so far, great as they sound, because I prefer discreet, stand-alone, components to installing a huge collection. That may not be the cleverest thing to do, but it's how I feel - ymmv (and prolly does)
Another solution, works also with (multi)selected rows:
procedure TReportsForm.ExportToCSV(const aGrid : TDBGrid; const FileName : String);
Var
I, J : Integer;
SavePlace : TBookmark;
Table : TStrings;
HeadTable : String;
LineTable : String;
First : Boolean;
Begin
HeadTable := '';
LineTable := '';
Table := TStringList.Create;
First := True;
Try
For I := 0 To Pred(aGrid.Columns.Count) Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
// Use the text from the grid, in case it has been set programatically
// E.g., we prefer to show "Date/time" than "from_unixtime(activity.time_stamp, "%D %b %Y %l:%i:%S")"
// HeadTable := HeadTable + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ','; // Previous separated wth semi-colon, not comma! (global)
First := False;
End
Else
begin
// HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ',';
end;
Delete(HeadTable, Length(HeadTable), 1); // Remove the superfluous trailing comma
Table.Add(HeadTable);
First := True;
// with selection of rows
If aGrid.SelectedRows.Count > 0 Then
Begin
For i := 0 To aGrid.SelectedRows.Count - 1 Do
Begin
aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
For j := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[J].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[J].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[J].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
First := True;
End;
End
Else
//no selection
Begin
SavePlace := aGrid.DataSource.Dataset.GetBookmark;
aGrid.DataSource.Dataset.First;
Try
While Not aGrid.DataSource.Dataset.Eof Do
Begin
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[I].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[I].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
aGrid.DataSource.Dataset.Next;
First := True;
End;
aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
Finally
aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
End;
End;
Table.SaveToFile(FileName);
Finally
Table.Free;
End;
End; // ExportToCSV()
You could use a own tiny procedure wich could be adapted to your needs
Procedure Dataset2SeparatedFile(ads: TDataset; const fn: String; const Separator: String = ';');
var
sl: TStringList;
s: String;
i: Integer;
bm: TBookmark;
Procedure ClipIt;
begin
s := Copy(s, 1, Length(s) - Length(Separator));
sl.Add(s);
s := '';
end;
Function FixIt(const s: String): String;
begin
// maybe changed
Result := StringReplace(StringReplace(StringReplace(s, Separator, '', [rfReplaceAll]), #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
// additional changes could be Quoting Strings
end;
begin
sl := TStringList.Create;
try
s := '';
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayLabel) + Separator;
end;
ClipIt;
bm := ads.GetBookmark;
ads.DisableControls;
try
ads.First;
while not ads.Eof do
begin
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayText) + Separator;
end;
ClipIt;
ads.Next;
end;
ads.GotoBookmark(bm);
finally
ads.EnableControls;
ads.FreeBookmark(bm);
end;
sl.SaveToFile(fn);
finally
sl.Free;
end;
end;

'Malformed string' exception when inserting into Firebird (Delphi, UniDAC, UniSQL, INSERT, parameters)

Using Delphi 2010, UniDAC components, Firebird 2.5 SuperServer.
Database character set is ISO_8559_1 (my Windows default).
I am writing a data transfer application to transfer data from an Access database to a Firebird database that has identical table structure. I am using a ADOQuery component to select all rows from source table, and then looping through that recordset, and using UniSQL component with an INSERT statement with parameters, assigning parameter values from the corresponding source dataset field values.
When running the insert command, it throws a 'Malformed string' exception.
I am stuck and need help to resolve the issue.
Code follows:
function TDataTransfer.BeginTransfer(AProgressCallback: TProgressCallback): Boolean;
var
slSQLSelect, slSQLInsert: TStringList;
i, f, z: Integer;
cmdS, cmdI: String;
adods: TADODataSet;
fbcmd: TUniSQL;
fbscript: TUniscript;
q: String;
s : WideString;
begin
FProgressCallback := AProgressCallback;
fbscript := TUniscript.Create(nil);
try
fbscript.Connection := FirebirdConnection;
FirebirdConnection.StartTransaction;
try
fbscript.Delimiter := ';';
fbscript.ExecuteFile(ExtractFilePath(ParamStr(0)) + 'Firebird_Script_0.txt');
FirebirdConnection.CommitRetaining;
slSQLSelect := TStringList.Create;
slSQLInsert := TStringList.Create;
adods := TADODataSet.Create(nil);
fbcmd := TUniSQL.Create(nil);
try
adods.Connection := AccessConnection;
fbcmd.Connection := FirebirdConnection;
slSQLSelect.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'Access_Select.txt');
slSQLInsert.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'Firebird_Insert.txt');
z := slSQLSelect.Count - 1;
for i := 0 to z do begin
cmdS := slSQLSelect[i];
cmdI := slSQLInsert[i];
adods.CommandText := cmdS;
fbcmd.SQL.Text := cmdI;
adods.Open;
while not adods.Eof do begin
for f := 0 to adods.FieldCount - 1 do
try
if adods.FieldDefs[f].DataType = ftWideString then begin
s := adods.Fields[f].AsAnsiString ;
q := '"';
// if AnsiStrPos(PAnsiChar(#s), PAnsiChar(q)) <> nil then
// s := StringReplace(s, '"', '""', [rfReplaceAll]);
fbcmd.Params[f].Value := s;
end
else
if adods.FieldDefs[f].DataType = ftWideMemo then
fbcmd.Params[f].SetBlobData(adods.CreateBlobStream(adods.Fields[f], bmRead))
else
fbcmd.Params[f].Value := adods.Fields[f].Value;
except
raise;
end;
try
fbcmd.Execute;
// FirebirdConnection.CommitRetaining;
except
raise;
end;
adods.Next;
end;
adods.Close;
FProgressCallback((i + 1) * 100 div (z + 1), 10);
end;
finally
slSQLSelect.Free;
slSQLInsert.Free;
adods.Free;
fbcmd.Free;
end;
fbscript.ExecuteFile(ExtractFilePath(ParamStr(0)) + 'Firebird_Script_1.txt');
FirebirdConnection.Commit;
Result := True;
except
FirebirdConnection.Rollback;
Result := False;
end;
finally
fbscript.Free;
end;
end;
TIA,
SteveL
If you try to replace s := StringReplace(s, '"', '""', [rfReplaceAll]); with s := StringReplace(s, '''''', '''', [rfReplaceAll]); and uncomment the line;

Resources