How To…Create RAP Globals in Code

Question

“How do I create a RAP Global Variable and assign it in the RAP OnGlobalCreate event in Delph code?”

Solution

Use the raGetCodeModule() routine to access the RAP code module in code and manually add a TraVarProgram and TraProgram object to create the variable and OnGlobalCreate event.

Download: CreateGlobalVar.zip

Download: CreateGlobalProc.zip

Sample Delphi code:

procedure TForm1.CreateGlobalVar;
var
  lCodeModule: TraCodeModule;
  lGlobalVarProgram: TraVarProgram;
begin

  lCodeModule := raGetCodeModule(ppReport1);

  lGlobalVarProgram := TraVarProgram(lCodeModule.GlobalVarProgram);

  if (lGlobalVarProgram = nil) then
    begin
      lGlobalVarProgram := TraVarProgram.Create;
      lGlobalVarProgram.ChildType := ctVarProgram;
      lGlobalVarProgram.ProgramName := 'Variables';
      lCodeModule.GlobalVarProgram := lGlobalVarProgram;
    end;

  lGlobalVarProgram.SourceLines.Clear;
  lGlobalVarProgram.SourceLines.Add('procedure Variables; ' + #13#10);
  lGlobalVarProgram.SourceLines.Add('var' + #13#10);
  lGlobalVarProgram.SourceLines.Add('  gNewData: Double;' + #13#10);
  lGlobalVarProgram.SourceLines.Add('  begin' + #13#10 + #13#10);
  lGlobalVarProgram.SourceLines.Add('  end;');

  {compile the code}
  lCodeModule.BuildAll(True);


end;


procedure TForm1.CreateGlobalOnCreate;
var
  lCodeModule: TraCodeModule;
  lGlobalCreateProgram: TraProgram;
begin

  lCodeModule := raGetCodeModule(ppReport1);

  lGlobalCreateProgram := TraProgram(lCodeModule.GlobalCreateProgram);

  if (lGlobalCreateProgram = nil) then
    begin
      lGlobalCreateProgram := TraProgram.Create;
      lGlobalCreateProgram.ChildType := ctCreateProgram;
      lGlobalCreateProgram.ProgramName := 'GlobalOnCreate';
      lGlobalCreateProgram.ProgramType := ttProcedure;
      lCodeModule.GlobalCreateProgram := lGlobalCreateProgram;
    end;

  lGlobalCreateProgram.SourceLines.Clear;
  lGlobalCreateProgram.SourceLines.Add('procedure GlobalOnCreate; ');
  lGlobalCreateProgram.SourceLines.Add('begin' + #13#10);
  lGlobalCreateProgram.SourceLines.Add('  gNewData := 40.00;' + #13#10);
  lGlobalCreateProgram.SourceLines.Add('end;');

  {compile the code}
  lCodeModule.BuildAll(True);


end;