I have:
Delphi 2007
Crystal 11
The Delphi 7 version of the Crystal VCL component (latest one I'm aware of, and it compiles fine in D2007)
A very simple test Crystal report, written in Crystal 11, which just dumps a table onto the screen (no selection criteria, no formulas, just straight data)
I tried
Created a new VCL forms app
Dropped the TCrpe component on the form
Set the "ReportName" property to my test report.
I dropped a button on the form, and behind it placed one line:
Crpe1.Execute
If the report has the "Save Data With Report" option turned on, then this works fine.
If I turn that option off, then I need to provide login credentials.
Using this code (which worked fine in Delphi 5 a million years ago):
procedure TForm1.BitBtn1Click(Sender: TObject);
var
logonItem: integer;
begin
Crpe1.LogOnServer.Clear;
logonItem := Crpe1.LogOnServer.Add('MYSERVER.MYDOMAIN.COM');
Crpe1.LogonServer[logonItem].UserID := 'USERNAME';
Crpe1.LogOnServer[logonItem].Password := 'PASSWORD';
Crpe1.LogOnServer[logonItem].DatabaseName := 'MYDATABASE';
Crpe1.Execute;
end;
I get this error:
---------------------------
Project2
---------------------------
Error:536 Error in File C:\REPORT.RPT:
Unable to connect: incorrect log on parameters.
Execute <PEStartPrintJob>.
---------------------------
OK
---------------------------
What am I doing wrong? How can I provide login credentials to the Crystal VCL component in Delphi? My current workaround is pretty ugly, and I have a lot of legacy code to convert. It would be really nice if I could use the VCL component in a straightforward way.
I use the VCL in Delphi 6, works great. But I don't use the LogOnServer property, I use the LogOnInfo.
This works for any report, and reports that contain subreports (as these need the credentials supplying as well):
With CRPE1 Do
Begin
With SubReports Do
Begin
Retrieve;
If (Count > 0) then
For i := 0 To (Count - 1) Do
Begin
ItemIndex := i;
LogOnInfo.Retrieve;
For j := 0 to LogOnInfo.Count - 1 Do
Begin
LogOnInfo[j];
With LogOnInfo Do
Begin
ServerName := MyDataSource;
DatabaseName := DatabasePath;
UserID := DBUser;
Password := sPwd;
End;
End; {For j}
Tables.Retrieve;
End; {For i}
ItemIndex := 0;
End; {With SubReports}
SubReports[0];
End; {With CRPE1}
Here is some old "legacy" code which uses the VCL component:
mCrpe.reportname:=mfilename;
mCrpe.Connect.UserID := CustomReportCurrentUser;
mCrpe.connect.ServerName:='servername';
mCrpe.connect.DataBaseName:='databasename';
mCrpe.connect.propagate:=True;
mCrpe.Connect.Password := CustomReportClientPass;
try
mConnected := mCrpe.Connect.Test;
except
on e: eDBEngineError do begin
showmessage(e.message);
end;
end;
mCrpe.windowbuttonbar.refreshbtn:=true;
mCrpe.discardsaveddata;
mCrpe.Show;
Some time ago I had read the Crystal VCL component was being "sunsetted". Since then I have migrated to the Active X RDC component. It will require the Active X to be installed on your target computers though.
Related
I've encountered a very strange DBComboBox problem in a master/detail app using Access via ADO. If you have a DBComboBox (.Style=csDropDown) containing a list of items and you enter some text that doesn't exist in the list, the value in the table's DBComboBox field won't appear when navigating back to that record. I've used the DBNavigator.OnClick code below to attempt to resolve this problem but it only works if the first record in the table contains a value not in the list. When you change the value of the DBComboBox in the first record to one that is in the list, no nonconforming items will appear in the DBComboBox text. Has anyone found a solution to this?
procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
var
SavePlace : TBookmark;
begin
if (DBComboBox1.Text='') then begin
SavePlace := TADODataSet(DBNavigator1.DataSource.DataSet).GetBookmark;
TADODataSet(DBNavigator1.DataSource.DataSet).Requery;
TADODataSet(DBNavigator1.DataSource.DataSet).GotoBookMark(SavePlace);
TADODataSet(DBNavigator1.DataSource.DataSet).FreeBookMark(SavePlace);
end;
end;
Unfortunately I don't have XE installed, but I have made a sample project which
reproduces your problem in D7 and Seattle. The code is shown below and I think
you will find that if you follow the exact steps below, it shows that there is something
rather strange going on. Update See the bottom of the answer for a possible work-around, which I think is preferable to the code you quote in your q.
As you'll see, except for Form1 itself, all the components are created at runtime
entirely in code. This is to remove any doubt whether the behaviour is caused
by some obscure property setting (it isn't) and in case you wish to submit it
to EMBA as a bug report. For a similar reason I've used a TClientDataSet so that
the app does not depend on any external data.
Steps (please follow steps 4-7 exactly the first time you try them)
Restart the IDE and create a new project and edit the .Pas file for the main form as shown below. The reason for restarting the IDE is that I discovered that if it has been running for a long time (two days in my case) the details of the misbehaviour
of the app change slightly).
Compile and run.
The app will start with the first from in the DBGrid selected.
Type anything (an 'X' will do) into the DBComboBox, then click the Save toolbutton
on the DBNavigator.
Click the Next (>) toolbutton on the DBNavigator once only. The DBComboBox now displays
'Two'.
Click the Prior (<) toolbutton on the DBNavigator once only. The DBComboBox is now empty.
Click the Prior (<) toolbutton on the DBNavigator once only. The DBComboBox now displays
what you typed in step 4.
Close the app. Most likely the IDE debugger will catch a fault and open the CPU window.
This fault occurs on the line
DestroyWindow(FHandle);
in TApplication.Destroy. I am no Windows internals expert but I think it's likely that this is because of some corruption being caused by whatever causes the blank result in step 6. The fact that
step 7 causes the DBComboBox to correctly display what you typed makes me suspect that cause is actually
in the way the DBComboBox interacts with its FieldDataLink which connects it to the dataset.
Btw, the fact that the fault does not occur if you call DBComboBox1.Free in TForm1's FormDestroy
seems to me to confirm that the fault is related to whatever is causing your problem.
All this, and the fact that it has apparently passed unnoticed in the 25 years of Delphi, seems very strange
to me. This demo app can show up another quirk that's been lurking in the DBGrid for a similar length ot time. To see
it:
Comment out all the references to the DBComboBox and reinstate dgMultiSelect amongst the grid options in the
line that sets them. Compile and run the app.
Click in the cell in the Name column for the first row, type something and save it.
Click the Next toolbutton once. The first row does not de-select itself as it should.
AFAICT (by displaying the DBGrid's count of Bookmarks on the form's caption) this is not
because it has saved a bookmark on the first row.
While I've been writing this, a possible work-around has occurred to me, which I'll updated
this to include if I can get it to work.
Code
type
TForm1 = class(TForm)
procedure FormCreate(Sender : TObject);
private
procedure SetUpDataSet;
procedure SetUpGUI;
protected
public
ClientDataSet1 : TClientDataSet;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
DBNavigator1: TDBNavigator;
DBComboBox1: TDBComboBox;
end;
[...]
procedure TForm1.SetUpGUI;
begin
ClientDataset1 := TClientDataSet.Create(Self);
DataSource1 := TDataSource.Create(Self);
DataSource1.DataSet := ClientDataSet1;
DBGrid1 := TDBGrid.Create(Self);
DBGrid1.Top := 8;
DBGrid1.Left := 8;
DBGrid1.Width := 425;
DBGrid1.Options := [dgEditing, dgTitles, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit{, dgMultiSelect}];
DBGrid1.DataSource := DataSource1;
DBGrid1.Parent := Self;
DBNavigator1 := TDBNavigator.Create(Self);
DBNavigator1.DataSource := DataSource1;
DBNavigator1.Top := 144;
DBNavigator1.Left := 16;
DBNavigator1.Parent := Self;
DBComboBox1 := TDBComboBox.Create(Self);
DBComboBox1.DataField := 'Name';
DBComboBox1.DataSource := DataSource1;
DBComboBox1.Top := 240;
DBComboBox1.Left := 16;
DBComboBox1.Parent := Self;
end;
procedure TForm1.SetUpDataSet;
var
Field : TField;
begin
// Create 2 fields in the CDS
Field := TIntegerField.Create(Self);
Field.FieldName := 'ID';
Field.FieldKind := fkData;
Field.DataSet := ClientDataSet1;
Field := TStringField.Create(Self);
Field.FieldName := 'Name';
Field.Size := 40;
Field.FieldKind := fkData;
Field.DataSet := ClientDataSet1;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetUpGUI;
SetUpDataSet;
// Set up DBComboBox
DBComboBox1.Style := csDropDown;
DBComboBox1.Items.Add('One');
DBComboBox1.Items.Add('Two');
DBComboBox1.Items.Add('Three');
// Next, set up the CDS
ClientDataSet1.CreateDataSet;
ClientDataSet1.InsertRecord([1, '']);
ClientDataSet1.InsertRecord([2, 'Two']);
ClientDataSet1.InsertRecord([3, '']);
ClientDataSet1.First;
end;
Possible work-around Add the following method to Form1:
procedure TForm1.ClientDataSet1AfterScroll(DataSet: TDataSet);
var
S : String;
begin
S := DataSet.FieldByName('Name').AsString;
if S <> DbComboBox1.Text then
DbComboBox1.Text := S;
Caption := IntToStr(DBGrid1.SelectedRows.Count);
end;
Then, in the SetUpGUI method, add the following immediately after the line where ClientDataSet1 is created:
ClientDataset1.AfterScroll := ClientDataSet1AfterScroll;
I have not tested this thoroughly, but it seems to work in the test conditions of the steps I've described above.
Need to an open an PowerPoint presentation. I am using the below statement.
Var
ppt: _Application;
pres: _Presentation;
try
ppt := GetActiveOleObject('PowerPoint.Application') as _Application;
except
ppt := CreateOleObject('PowerPoint.Application') as _Application ;
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open(FPOTX, msoFalse, msoTrue, msoTrue);
except
on e:exception do begin
printtofile('Error in call to ppt.Presentation.Open' + e.message);
end;
end;
It works fine whenever , CreateOleObject() in exception is called. (i.e., no presentation is already open).
But the above statement fails , if one presentation is already open. (i.e, ppt.Presentations.Open() is called after GetActiveOleObject() function).
Using Delphi XE2 , MS Office 2013 , Windows 8
This fails only in Windows 8 not in Windows 7.
Thanks in advance.
I don't know where the problem is, if it is your Delphi or Office or Windows version. But this code works no problem in Windows 8.1 x64, Delphi XE2 (32bit target), Office 2007. Unfortunately I do not have Office 2013 to test it.
I don't have any Type LIBs imported in my Delphi. So I tested it just using plain Variant types.
If PPT isn't opened, the code opens it. Otherwise it gets the OLE Object. Afterwards the desired Presentation is opened. Works as often as I tested it, no matter if PPT is closed or opened.
...
implementation
uses
ComObj, ActiveX;
const
msoFalse = TOleEnum(False);
msoTrue = TOleEnum(True);
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ppt, pres: Variant;
begin
ppt := Unassigned;
pres := Unassigned;
try
ppt := GetActiveOleObject('PowerPoint.Application');
except
ppt := CreateOleObject('PowerPoint.Application');
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open('C:\Temp\Test.pptx', msoFalse, msoTrue, msoTrue);
except
on E:Exception do
ShowMessage('OOPS');
end;
end;
EDIT
I also tested it with an imported PowerPoint Type Lib. And your code works 1:1 here:
...
implementation
uses
ComObj, ActiveX, PowerPoint_TLB;
const
msoFalse = TOleEnum(False);
msoTrue = TOleEnum(True);
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ppt: _Application;
pres: _Presentation;
begin
ppt := nil;
pres := nil;
try
ppt := GetActiveOleObject('PowerPoint.Application') as _Application;
except
ppt := CreateOleObject('PowerPoint.Application') as _Application;
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open('C:\Temp\Test.pptx', msoFalse, msoTrue, msoTrue);
except
on E:Exception do
ShowMessage('OOPS');
end;
end;
SOLUTION FOR OFFICE 2013
As you already found out: It seems to work with Office 2013 when changing the Title parameter of ppt.Presentations.Open to msoFalse ==> ppt.Presentations.Open(FPOTX, msoFalse, msoFalse, msoTrue)
This works for me absolutely without any issues. The problem faced was compatibility issue between the XXX.pot(office 1997-2003) and XXX.potx (office2014). Apart from that everthing was fine.
Does anybody know why the following code does not work with Excel 2010 (Home and Small Business Office Edition) ¿?
procedure TForm1.Button1Click(Sender: TObject);
var
rango : OleVariant;
ExcelObject : TExcelApplication;
ExcelWorksheet : TExcelWorksheet;
LCID : integer;
begin
try
ExcelObject := TExcelApplication.Create (self);
LCID := LOCALE_USER_DEFAULT;
ExcelObject.Workbooks.Add(EmptyParam, LCID);
ExcelWorksheet := TExcelWorksheet.Create(ExcelObject);
ExcelWorksheet.ConnectTo(ExcelObject.Worksheets.Item [1] as _Worksheet);
rango := ExcelWorksheet.Range['B2','B2'];
rango.Font.size := 16;
rango.Font.Bold := True;
rango.Value2 := 'test';
ExcelObject.Visible[lcid] := true;
except
on e: exception do
showmessage(e.message);
end;
end;
A "Not registered class" exception is shown while adding a workbook: "ExcelObject.Workbooks.Add(EmptyParam, LCID);"
This works fine with Office 2010 Proffesional Edition (and older Office Editions) but not with Home and Small Business Edition 2010 Edition.
I know this is really late but I've been fighting with this same error the last two days and finally figured it out (I think). The error is misleading due to crappy error handling in the component.
The clue I had was that it worked perfectly in a different test application on the same machine - so it wasn't a class registration issue.
I get the error (in several places) if I don't have things connected properly. In your case you probably need to do the CONNECT.
E.g.
ExcelObject := TExcelApplication.Create (self);
ExcelObject.Connect;
I've also seen this if you forget to do a ConnectTo call to connect the interfaces together.
E.g.
XlWorkBook.ConnectTo(XlApp.ActiveWorkbook);
Hope this helps someone else.
Delphi 6 on Vista service pack 2 seems that can't load imagelist from dfm and save back again in the IDE.
The project with the dfm corrupted can't be rebuilt.
the error when I run the project is:
EReadError Error reading imagelist1.Bitmap: Failed to read ImageList
data from stream
any suggestion?
thanks in advance
Have you done anything funny to your Delphi installation, such as adding a delphi32.exe.manifest file to Delphi's directory in an attempt to make the IDE have XP or Vista theming? Don't do that. If you have that file there, delete it, and you should be back to normal.
The image-list format changed with version 6 of the Common Controls library, and Delphi 6 is not capable of using it. The manifest tells the IDE to use version 6, so when it saves your DFM, it uses that format. Then, when loading, prior versions can't read it anymore.
The problem may be on ImageList_Write of the comctl32.dll
// delphi 6
procedure TCustomImageList.WriteData(Stream: TStream);
var
SA: TStreamAdapter;
begin
SA := TStreamAdapter.Create(Stream);
try
if not ImageList_Write(Handle, SA) then
raise EWriteError.CreateRes(#SImageWriteFail);
finally
SA.Free;
end;
end;
// delphi 2005
procedure TCustomImageList.WriteData(Stream: TStream);
var
SA: TStreamAdapter;
ComCtrlHandle: THandle;
const
ILP_DOWNLEVEL = 1;
begin
if CachedComCtrlVer = 0 then
begin
CachedComCtrlVer := GetFileVersion(comctl32);
if CachedComCtrlVer >= ComCtlVersionIE6 then
begin
ComCtrlHandle := GetModuleHandle(comctl32);
if ComCtrlHandle <> 0 then
ImageListWriteExProc := GetProcAddress(ComCtrlHandle, 'ImageList_WriteEx'); { Do not localize }
end;
end;
SA := TStreamAdapter.Create(Stream);
try
{ See if we should use the new API for writing image lists in the old
format. }
if Assigned(ImageListWriteExProc) then
begin
if ImageListWriteExProc(Handle, ILP_DOWNLEVEL, SA) <> S_OK then
raise EWriteError.CreateRes(#SImageWriteFail)
end
else if not ImageList_Write(Handle, SA) then
raise EWriteError.CreateRes(#SImageWriteFail);
finally
SA.Free;
end;
end;
I've heard that some custom component authors use an RTL routine that checks to see if Delphi is running in order to set up shareware restrictions. Does anyone know what this routine is? Checking obvious names like "DelphiRunning" or "IsDelphiRunning" doesn't turn up anything useful.
There are 2 different ideas here:
- Delphi is up and running
- The application is running under the debugger
The common way to test if Delphi is running is to check the presence of known IDE Windows which have a specific classname like TAppBuilder or TPropertyInspector.
Those 2 works in all version of Delphi IIRC.
If you want to know if your application is running under the debugger, i.e. launched normally from the IDE with "Run" (F9) or attached to the debugger while already running, you just have to test the DebugHook global variable.
Note that "Detach from program" does not remove the DebugHook value, but "Attach to process" sets it.
function IsDelphiRunning: Boolean;
begin
Result := (FindWindow('TAppBuilder', nil) > 0) and
(FindWindow('TPropertyInspector', 'Object Inspector') > 0);
end;
function IsOrWasUnderDebugger: Boolean;
begin
Result := DebugHook <> 0;
end;
If the goal is to restrict the use of a trial version of your component to when the application is being developped, both have flaws:
- Hidden windows with the proper Classname/Title can be included in the application
- DebugHook can be manually set in the code
You can use DebugHook <> 0 from your component code. DebugHook is a global variable (IIRC, it's in the Systems unit) that's set by the Delphi/RAD Studio IDE, and couldn't be set from anywhere else.
There are other techniques (FindWindow() for TAppBuilder, for instance), but DebugHook takes all of the work out of it.
This is a code snippet from www.delphitricks.com/source-code/misc/check_if_delphi_is_running.html.
function WindowExists(AppWindowName, AppClassName: string): Boolean;
var
hwd: LongWord;
begin
hwd := 0;
hwd := FindWindow(PChar(AppWindowName), PChar(AppClassName));
Result := False;
if not (Hwd = 0) then {window was found if not nil}
Result := True;
end;
function DelphiLoaded: Boolean;
begin
DelphiLoaded := False;
if WindowExists('TPropertyInspector', 'Object Inspector') then
if WindowExists('TMenuBuilder', 'Menu Designer') then
if WindowExists('TAppBuilder', '(AnyName)') then
if WindowExists('TApplication', 'Delphi') then
if WindowExists('TAlignPalette', 'Align') then
DelphiLoaded := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DelphiLoaded then
begin
ShowMessage('Delphi is running');
end;
end;
function DelphiIsRunning: Boolean;
begin
Result := DebugHook <> 0;
end;