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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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; |