Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use query name to identify TabSheet?

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a PageControl with 10 tabsheets on a form. Each of the tabsheets has a name almost identical to the query that populates it. For instance, the TQuery qryCaseNotes is displayed on the TabSheet named CaseNotes, the FileLocation tabsheet is populated by qryFileLocation. For each of these pages I need to pass the same parameters to the query (CasePrefix and CaseNumber). Instead of writing almost identical code for each of the 10 pages, I wanted to write a procedure that I pass the query to, fill in the parameters, run it and if something is returned change the Visible property to true. Something like this:
Code:
procedure GetInfo(AQuery : Tquery);
var
ATabSheet : TTabSheet;
begin
  ATabSheet := rightstr(AQuery.Name,length(AQuery.Name - 3)); //remove the qry from the beginning of QueryName
  with AQuery do
  begin
    ParamByName('CASPRE').AsString := Form_Main.strCasPre;
    ParamByName('CASNUM').AsInteger := Form_Main.intCasNum;
    Active := True;
    If not IsEmpty then
      Form_CaseInformation.ATabSheet.TabVisible := True;
  end;
end;

Except this isn't working! The main problem is in the assignment of the Tabsheet name. Any suggestions?

Thanks!

Leslie
 
When you say it is not working what is the problem? A compilation error or a run time error?

As the code is presented, the first line will not compile as you are trying to assign a string to a TabSheet when you are probably wanting to assign a string to the caption of the TabSheet. I guess your code should look something like:
Code:
  ATabSheet.Caption := Copy(AQuery.Name,4,length(AQuery.Name);
or maybe I'm missing the point of the question.

Andrew
 
I think it's almost there. What I want to do is:

Run qryNotes
if qryNotes is not empty then
Notes.TabVisible := True

then run qryFileLocation
if qryFileLocation is not empty then
FileLocation.TabVisible := True;

Leslie
 
Code:
procedure GetInfo(AQuery : Tquery);
var
ATabSheet : TTabSheet;
begin
  ATabSheet.Name := Copy(AQuery.Name,4,length(AQuery.Name));
  with AQuery do
  begin
    ParamByName('CASPRE').AsString := Form_Main.strCasPre;
    ParamByName('CASNUM').AsInteger := StrToInt(Form_Main.StrCasNum);
    Active := True;
    If not IsEmpty then
      Form_CaseInformation.ATabSheet.TabVisible := True;
  end;
end;

on the line

Form_CaseInformation.ATabSheet.TabVisible := True

I get an error:

Undeclared identifier: 'ATabSheet'
 
When you declare ATabSheet as a variable in your procedure it is just a pointer which is not pointing anywhere. So when you try to assign something to ATabSheet.Name it will fail at run time.

You got a compiler error because ATabSheet is not part of your form - it is a local variable.

What I think you want is for ATabSheet to point at the TabControl with a similar name to AQuery.Name and then make it visible (or invisible). In which case something like this should do:
Code:
procedure GetInfo(AQuery: TQuery);
var
  ATabSheet: TTabSheet;
  TabSheetName: string;
begin
  TabSheetName := Copy(AQuery.Name,4,length(AQuery.Name));
  ATabSheet := FindComponent(TabSheetName) as TTabSheet;
  ... 
  ... your AQuery SQL code here
  ...
  ATabSheet.TabVisible := not AQuery.IsEmpty;
end;

I've got to go out in a minute so I haven't had time to test it but it should put you on the right lines.

Andrew

 
i get error:

Undeclared identifier: 'FindComponent'
Operator not applicable to this operand type

on line:

ATabSheet := FindComponent(TabSheetName) as TTabSheet;

but we're getting closer, thanks for your help!

Leslie
 
I've done a quick test and the code works for me. Can you cut and paste the procedure containing the FindComponent function call?

Andrew
 
You know which tabsheet you want to display so you should be able to manage this with something like this :-
Code:
var
  S : String;
begin
  S := rightstr(AQuery.Name,length(AQuery.Name - 3));
  with AQuery do
  begin
    ParamByName('CASPRE').AsString := Form_Main.strCasPre;
    ParamByName('CASNUM').AsInteger := Form_Main.intCasNum;
    Active := True;
    If not IsEmpty then
    begin
      if S = 'CaseNotes' then Form_CaseInformation.Pages[0].TabVisible := True;
      if S = 'FileLocation' then Form_CaseInformation.Pages[1].TabVisible := True;
    end;
  end;
end;

All you would need to do is match up the tabsheet index numbers and captions in the "if not isempty" statement.

Arte Et Labore [rockband]
 
Ok, thanks for your time and help! Here's the code I've got so far:

Code:
unit Unit_Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ComCtrls, dbtables, Grids, DBGrids, ExtCtrls, Menus,
  IDGlobal;

type
  TDefendantRec = record
    CASPRE : string;
    CASNUM : string;
    DefendantName : string;
    DOB : string;
    SSN : string;
    LASTEVENT : string;
    HERNGDAT : string;
    HERTIM : string;
    JUDCOD : integer;
    JUDNAME : string;
    CaseStatus : string;
  end;
  
type
  TForm_Main = class(TForm)
    Edit_CaseNumber: TEdit;
    Label1: TLabel;
    ComboBox_CasePrefix: TComboBox;
    Edit_Name: TEdit;
    Label2: TLabel;
    Edit_DOB: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Edit_SSN: TEdit;
    Btn_Close: TBitBtn;
    StatusBar1: TStatusBar;
    Lbl_DateTime: TLabel;
    Timer1: TTimer;
    MainMenu1: TMainMenu;
    Menu_Help: TMenuItem;
    Menu_Exit: TMenuItem;
    Menu_About: TMenuItem;
    Label5: TLabel;
    Label6: TLabel;
    Edit_Booking: TEdit;
    Label7: TLabel;
    panSearchGrid: TPanel;
    sgSearchGrid: TStringGrid;
    procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Menu_PrintClick(Sender: TObject);
    procedure Menu_ExitClick(Sender: TObject);
    procedure Menu_AboutClick(Sender: TObject);
    procedure Edit_NameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure sgSearchGridDblClick(Sender: TObject);

  private
    { Private declarations }
    strCASPRE : string;
    intCASNUM : integer;
    procedure Process;
    procedure ProcessByCase(strProcessCasePre : string; intProcessCaseNum : integer);
    procedure ProcessSearch;
  public
    { Public declarations }
    Save_Cursor:TCursor;

  end;

var
  Form_Main: TForm_Main;


implementation

uses Unit_DataMod, Unit_NameSearch, Unit_CaseInformation, Subs;

{$R *.DFM}
procedure TForm_Main.FormActivate(Sender: TObject);
begin
  ComboBox_CasePrefix.ItemIndex:= 0;
  Form_Main.panSearchGrid.Visible := False;
  with Form_Main.sgSearchGrid do
  begin
    ColWidths[0] := 26;
    ColWidths[1] := 77;
    ColWidths[2] := 200;
    ColWidths[3] := 89;
    ColWidths[4] := 99;
    ColWidths[5] := 125;
    ColWidths[6] := 72;
    ColWidths[7] := 65;
    ColWidths[8] := 30;
    ColWidths[9] := 103;
    ColWidths[10] := 114;
    Cells[1,0] := 'Case #';
    Cells[2,0] := 'Defendant Name';
    Cells[3,0] := 'DOB';
    Cells[4,0] := 'SSN';
    Cells[5,0] := 'Last Event';
    Cells[6,0] := 'Date';
    Cells[7,0] := 'Time';
    Cells[8,0] := 'Div';
    Cells[9,0] := 'Judge';
    Cells[10,0] := 'Status';
  end;
  Form_Main.Edit_CaseNumber.SetFocus;
end;

procedure GetInfo(AQuery: TQuery);
var
  ATabSheet: TObject;
  TabSheetName: string;
begin
  TabSheetName := Copy(AQuery.Name,4,length(AQuery.Name));
  ATabSheet := FindComponent(TabSheetName) As TTabSheet;
  with AQuery do
  begin
    ParamByName('CASPRE').AsString := Form_Main.strCASPRE;
    ParamByName('CASNUM').AsInteger := Form_Main.intCASNUM;
    Active := True;
  end;
end;

procedure TForm_Main.Process;
begin
  // Case number processing
  If Form_Main.panSearchGrid.Visible then
    Form_Main.panSearchGrid.Visible := False;
  //Process By Case number
  If (Edit_CaseNumber.text <> '') then
    ProcessByCase(Form_Main.ComboBox_CasePrefix.Text, strToInt(Form_Main.Edit_CaseNumber.text))
  else
  //Process by search criteria
    ProcessSearch;
  ClearForm(Form_Main);
end;

procedure TForm_Main.Timer1Timer(Sender: TObject);
begin
  Lbl_DateTime.Caption:= DateTimeToStr(Now);
  Form_CaseInformation.Lbl_DateTime.Caption:= DateTimeToStr(Now);
end;

procedure TForm_Main.Edit_NameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  //if user selects return key then process
  if (Key=VK_RETURN) then
  Process;
end;

procedure TForm_Main.Menu_PrintClick(Sender: TObject);
begin
  MessageDlg('Printing screen now', mtInformation, [mbOk], 0);
  Form_Main.Print;
end;

procedure TForm_Main.ProcessByCase(strProcessCasePre : string; intProcessCaseNum : integer);
var
i : integer;
begin
  Save_Cursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    For i := 0 to Form_CaseInformation.ComponentCount - 1 do
    begin
      If Form_CaseInformation.Components[i] is TTabSheet Then
      (Form_CaseInformation.Components[i] as TTabSheet).TabVisible := False;
    end;
    DataMod.qryMain.ParamByName('CASPRE').AsString := strProcessCasePre;
    If IsNumeric(Edit_CaseNumber.Text) then
      DataMod.qryMain.ParamByName('CASNUM').AsInteger := intProcessCaseNum
    else begin
      ShowMessage('Please enter a numeric case number.');
      Exit;
    end;
    DataMod.qryMain.Active := True;
    GetInfo(DataMod.qryCaseNotes);
Code:
    //below is what I've current got working, but I don't like it
    {with DataMod.qryCaseNotes do
    begin
      ParamByName('CASPRE').AsString := strCasPre;
      ParamByName('CASNUM').AsInteger := intCasNum;
      Active := True;
      If not IsEmpty then
      begin
        While not EOF do
        begin
          Form_CaseInformation.memoCaseNotes.Lines.Add(FieldByName('COMNTS').AsString);
          Next;
        end;
        Form_CaseInformation.CaseNotes.TabVisible := true;
      end;
    end;
    With DataMod.qryTapeLogs do
    begin
      ParamByName('CASPRE').AsString := strCasPre;
      ParamByName('CASNUM').AsInteger := intCasNum;
      Active := True;
      If not IsEmpty then
        Form_CaseInformation.TapeLogs.TabVisible := True;
    end;
    With DataMod.qryFileLocation do
    begin
      ParamByName('CASPRE').AsString := strCasPre;
      ParamByName('CASNUM').AsInteger := intCasNum;
      Active := True;
      If not IsEmpty then
        Form_CaseInformation.FileLocation.TabVisible := True;
    end;}
Code:
    Form_CaseInformation.ShowModal;
    DataMod.qryCaseNotes.Active := False;
    DataMod.qryTapeLogs.Active := False;
    DataMod.qryFileLocation.Active := False;
  finally
    Screen.Cursor := Save_Cursor;
  end;
end;

procedure TForm_Main.ProcessSearch;
var
DefendantList : array of TDefendantRec;
DefendantRecord : TDefendantRec;
i,j : integer;
strSearchCasNum : string;
begin
  Save_Cursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    DataMod.qryMainName.SQL.Clear;
    DataMod.qryMainName.SQL.Add('SELECT CASPRE, CASNUM, DEFNAM, DEFDTOB, ' +
     'DEFSSN, HERTYP, HERNGDAT, HERTIM, JUDCOD FROM CMPDEFMF');
    //search by Name
    If Form_Main.Edit_Name.Text <> '' then
    begin
      DataMod.qryMainName.SQL.Add('WHERE DEFNAM LIKE ' +
       QuotedStr(Form_Main.Edit_Name.Text + '%'));
      //Add DOB info if present
      If Form_Main.Edit_DOB.text <> '' then
      begin
        If not IsNumeric(Form_Main.Edit_DOB.Text) then
        begin
          ShowMessage('Please enter a numeric date of birth.');
          exit;
        end
        else
          DataMod.qryMainName.SQL.Add(' AND DEFDTOB = ' + convertdateMMDDYYYY(Form_Main.Edit_DOB.Text));
      end;
    end
    //search by SSN
    else if Form_Main.Edit_SSN.Text <> '' then
    begin
      If not IsNumeric(Form_Main.Edit_SSN.Text) then
        begin
          ShowMessage('Please enter a numeric Social Security Number.');
          exit;
        end
        else
          DataMod.qryMainName.SQL.Add(' WHERE DEFSSN = ' + Form_Main.Edit_SSN.Text)
    end
    //search by Booking Number
    else if Form_Main.Edit_Booking.text <> '' then
    begin
      If not IsNumeric(Form_Main.Edit_Booking.text) then
      begin
        ShowMessage('Please enter a numeric Booking number.');
        exit
      end
      else
        DataMod.qryMainName.SQL.Add(' WHERE &quot;BOOKING#&quot; = ' + Form_Main.Edit_Booking.Text);
    end;
    DataMod.qryMainName.SQL.Add(' ORDER BY DEFNAM');
    DataMod.qryMainName.Active := True;
    j := 0;
    If not DataMod.qryMainName.IsEmpty then
    begin
      While not DataMod.qryMainName.Eof do
      begin
        inc(j);
        DataMod.qryMainName.Next;
      end;
      SetLength(DefendantList, j );
      DataMod.qryMainName.First;
      for i := 0 to j - 1 do
      begin
        strCASPRE := DataMod.qryMainName.FieldByName('CASPRE').AsString;
        intCASNUM := DataMod.qryMainName.FieldByName('CASNUM').AsInteger;
        //convert CaseNumber to seven character string for CMR5340
        strSearchCasNum := IntToStr(intCASNUM);
        strSearchCasNum := StringofChar('0', (7 - length(strSearchCasNum))) + strSearchCasNum;
        //get last hearing info for all cases
        DataMod.qryHearingInfo.ParamByName('CASPRE').AsString := strCASPRE;
        DataMod.qryHearingInfo.ParamByName('CASNUM').AsInteger := intCASNUM;
        DataMod.qryHearingInfo.Active := True;
        //get judge info for last hearing
        If not DataMod.qryHearingInfo.IsEmpty then
        begin
          DataMod.qryJudgeService.ParamByName('JUDCOD').AsInteger :=
           DataMod.qryHearingInfo.FieldByName('JUDCOD').AsInteger;
          DataMod.qryJudgeService.ParamByName('COURTDATE1').AsInteger :=
           DataMod.qryHearingInfo.FieldByName('HERNGDAT').AsInteger;
          DataMod.qryJudgeService.ParamByName('COURTDATE2').AsInteger :=
           DataMod.qryHearingInfo.FieldByName('HERNGDAT').AsInteger;
          DataMod.qryJudgeService.Active := True;
        end;
        //CMR5340 returns the case type and case status:
        //value 2: case type; value 3 case status1; value 4 case status2
        //if value 4 has a value then display that value not value 2
        with DataMod.CMR5340 do
        begin
          value[0] := strCASPRE;
          value[1] := strSearchCasNum;
          value[2] := EmptyStr;
          value[3] := EmptyStr;
          value[4] := EmptyStr;
          Execute;
          //add each defendant query information to Defendant Record
          With DefendantRecord do
          begin
            CASPRE := DataMod.qryMainName.FieldByName('CASPRE').AsString;
            CASNUM := DataMod.qryMainName.FieldByName('CASNUM').AsString;
            DefendantName := DataMod.qryMainName.FieldByName('DEFNAM').AsString;
            DOB := DateFormat(DataMod.qryMainName.FieldByName('DEFDTOB').AsString);
            SSN := SSNFormat(DataMod.qryMainName.FieldByName('DEFSSN').AsString);
            If value[4] <> '' then
              CaseStatus := value[4]
            else
              CaseStatus := value[3];
            If not DataMod.qryHearingInfo.IsEmpty then
            begin
              LASTEVENT := DataMod.qryHearingInfo.FieldByName('HERDSC').AsString;
              HERNGDAT := dateformat(DataMod.qryHearingInfo.FieldByName('HERNGDAT').AsString);
              HERTIM := FormatDateTime('h:mm AM/PM',(IntToTime(DataMod.qryHearingInfo.FieldByName('HERTIM').AsInteger)));
              JUDCOD := DataMod.qryHearingInfo.FieldByName('JUDCOD').AsInteger;
              if DataMod.qryJudgeService.Active then
                JUDNAME := DataMod.qryJudgeService.FieldByName('JUDSNM').AsString;
            end
            else
            begin
              LASTEVENT := '';
              HERNGDAT := '';
              HERTIM := '';
              JUDCOD := 0;
              JUDNAME := '';
            end;
            DefendantList[i] := DefendantRecord;
            DataMod.qryMainName.Next;
            DataMod.qryHearingInfo.Active := False;
            DataMod.qryJudgeService.Active := False;
          end;
        end;
      end;
      //for all defendants in Defendant Record array, add to TStringGrid
      for i := 0 to j - 1 do
      begin
        with sgSearchGrid do
        begin
          Cells[0, i + 1] := DefendantList[i].CASPRE;
          Cells[1, i + 1] := DefendantList[i].CASNUM;
          Cells[2, i + 1] := DefendantList[i].DefendantName;
          Cells[3, i + 1] := DefendantList[i].DOB;
          Cells[4, i + 1] := DefendantList[i].SSN;
          Cells[5, i + 1] := DefendantList[i].LASTEVENT;
          Cells[6, i + 1] := DefendantList[i].HERNGDAT;
          Cells[7, i + 1] := DefendantList[i].HERTIM;
          Cells[8, i + 1] := IntToStr(DefendantList[i].JUDCOD);
          Cells[9, i + 1] := DefendantList[i].JUDNAME;
          Cells[10,i + 1] := DefendantList[i].CaseStatus;
        end;
      end;
      Form_Main.sgSearchGrid.RowCount := i + 1;
      Form_Main.panSearchGrid.Visible := True;
    end;
  finally
    Screen.Cursor := Save_Cursor;
  end;
end;

procedure TForm_Main.Menu_ExitClick(Sender: TObject);
begin
  Form_Main.close;
end;

procedure TForm_Main.Menu_AboutClick(Sender: TObject);
begin
  MessageDlg('Last Revision 05/20/03', mtInformation, [mbOk], 0);
end;

procedure TForm_Main.sgSearchGridDblClick(Sender: TObject);
begin
  //when user doubleclicks, pass Caspre and CasNum to ProcessByCase
  With sgSearchGrid do
  begin
    Form_Main.strCASPRE := Cells[0,Selection.Top];
    Form_Main.intCASNUM := StrToINt(Cells[1,Selection.Top]);
    ProcessByCase(Form_Main.strCASPRE, Form_Main.intCASNUM);
  end;
end;

end.

As you can see, I'm currently doing something similar to Eric suggestion, by setting each TabSheet.TabVisible property, but I want something more dynamic so I don't have to do each one.

Thanks!

Leslie
 
I have provided you with the code that works dynamically and asked for the procedure where you couldn't get it to compile. Could you supply the procedure which uses my code please?

Andrew
 
Here is the procedure:

Code:
procedure GetInfo(AQuery: TQuery);
var
  ATabSheet: TObject;
  TabSheetName: string;
begin
  TabSheetName := Copy(AQuery.Name,4,length(AQuery.Name));
  ATabSheet := FindComponent(TabSheetName) As TTabSheet;
  with AQuery do
  begin
    ParamByName('CASPRE').AsString := Form_Main.strCASPRE;
    ParamByName('CASNUM').AsInteger := Form_Main.intCASNUM;
    Active := True;
  end;
end;

and here is where I'm calling it:
Code:
procedure TForm_Main.ProcessByCase(strProcessCasePre : string; intProcessCaseNum : integer);
var
i : integer;
begin
  Save_Cursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    For i := 0 to Form_CaseInformation.ComponentCount - 1 do
    begin
      If Form_CaseInformation.Components[i] is TTabSheet Then
      (Form_CaseInformation.Components[i] as TTabSheet).TabVisible := False;
    end;
    DataMod.qryMain.ParamByName('CASPRE').AsString := strProcessCasePre;
    If IsNumeric(Edit_CaseNumber.Text) then
      DataMod.qryMain.ParamByName('CASNUM').AsInteger := intProcessCaseNum
    else begin
      ShowMessage('Please enter a numeric case number.');
      Exit;
    end;
    DataMod.qryMain.Active := True;
Code:
GetInfo(DataMod.qryCaseNotes);
Code:
    Form_CaseInformation.ShowModal;
    DataMod.qryCaseNotes.Active := False;
    DataMod.qryTapeLogs.Active := False;
    DataMod.qryFileLocation.Active := False;
    DataMod.qryCashBond.Active := False;
    DataMod.qryCashTran.Active := False;
    DataMod.qrySuretyBond.Active := False;
    DataMod.qrySuretyTran.Active := False;
  finally
    Screen.Cursor := Save_Cursor;
  end;
end;

When I try to run it I get the errors:

Undeclared identifier: 'FindComponent'
Operator not applicable to this operand type

I am calling this from Unit Unit_Main (Form_Main) {all code for Unit_Main is in above posting}, these queries are for use in popualating Form_CaseInformation. Once I have all the queries run and know what needs to be shown on the Form_CaseInformation then I show it modally.

Thanks for your help!
Leslie



 
You need to make GetInfo a method of TForm_Main

OR

you could prefix the FindComponent call with Form_Main i.e.
Code:
  ATabSheet := Form_Main.FindComponent(TabSheetName) As TTabSheet;

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top