home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 February / Chip_2003-02_cd1.bin / zkuste / delphi / kompon / d34567 / KADAO77.ZIP / ODBCDialogUnit.pas < prev    next >
Pascal/Delphi Source File  |  2002-02-14  |  4KB  |  169 lines

  1. unit ODBCDialogUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TODBCDialog = class(TForm)
  11.     RadioGroup1: TRadioGroup;
  12.     ComboBox1: TComboBox;
  13.     ComboBox2: TComboBox;
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     Button3: TButton;
  17.     procedure RadioGroup1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button3Click(Sender: TObject);
  21.     procedure ComboBox1Change(Sender: TObject);
  22.     procedure ComboBox2Change(Sender: TObject);
  23.   private
  24.     { Private declarations }
  25.      NewDSN : String;
  26.   public
  27.     { Public declarations }
  28.     Function Execute(SysDsn, UsrDsn:TStrings; Var DsnString:String; UseODBCDialog:Boolean):Boolean;
  29.   end;
  30.  
  31. var
  32.   ODBCDialog  : TODBCDialog;
  33.  
  34. implementation
  35. Uses
  36.   KDaoDatabase;
  37. {$R *.DFM}
  38.  
  39. Var
  40.  Resultat : String;
  41.  
  42. procedure TODBCDialog.RadioGroup1Click(Sender: TObject);
  43. begin
  44.   NewDSN := '';
  45.   if RadioGroup1.ItemIndex=0 Then
  46.      Begin
  47.        ComboBox1.Enabled:=True;
  48.        ComboBox2.Enabled:=False;
  49.      End
  50.   Else
  51.      Begin
  52.        ComboBox1.Enabled:=False;
  53.        ComboBox2.Enabled:=True;
  54.      End;
  55. end;
  56.  
  57. procedure TODBCDialog.Button2Click(Sender: TObject);
  58. begin
  59.   ModalResult:=mrCancel;
  60. end;
  61.  
  62. procedure TODBCDialog.Button1Click(Sender: TObject);
  63. begin
  64.   ModalResult:=mrOk;
  65.   if RadioGroup1.ItemIndex=0 Then
  66.      Begin
  67.        Resultat:=ComboBox1.Items.Strings[ComboBox1.ItemIndex];
  68.      End
  69.   Else
  70.      Begin
  71.        Resultat:=ComboBox2.Items.Strings[ComboBox2.ItemIndex];
  72.      End;
  73. end;
  74.  
  75. Function TODBCDialog.Execute(SysDsn, UsrDsn:TStrings; Var DsnString:String; UseODBCDialog:Boolean):Boolean;
  76. begin
  77.   NewDSN := '';
  78.   ComboBox1.Clear;
  79.   ComboBox2.Clear;
  80.   ComboBox1.Items.SetText(SysDsn.GetText);
  81.   ComboBox2.Items.SetText(UsrDsn.GetText);
  82.   ComboBox1.ItemIndex:=0;
  83.   ComboBox2.ItemIndex:=0;
  84.   if ComboBox1.Items.IndexOf(DsnString) > -1 Then
  85.      Begin
  86.        RadioGroup1.ItemIndex:=0;
  87.        ComboBox1.Enabled:=True;
  88.        ComboBox2.Enabled:=False;
  89.        ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(DsnString);
  90.      End;
  91.   if ComboBox2.Items.IndexOf(DsnString) > -1 Then
  92.      Begin
  93.        RadioGroup1.ItemIndex:=1;
  94.        ComboBox1.Enabled:=False;
  95.        ComboBox2.Enabled:=True;
  96.        ComboBox2.ItemIndex:=ComboBox2.Items.IndexOf(DsnString);
  97.      End;
  98.   if UseODBCDialog Then
  99.      Begin
  100.        Button3.Click;
  101.        Result := False;
  102.        if NewDSN <> '' Then
  103.           Begin
  104.            DsnString := NewDSN;
  105.            Result    := True;
  106.           End;
  107.      End
  108.   Else
  109.      Begin
  110.       if ShowModal=mrOK Then
  111.          Begin
  112.           Result := True;
  113.           if NewDSN = '' Then DsnString := Resultat Else DsnString := NewDSN;
  114.          End
  115.       Else
  116.          Begin
  117.           Result := False;
  118.          End;
  119.      End;
  120. end;
  121.  
  122. procedure TODBCDialog.Button3Click(Sender: TObject);
  123. Var
  124.  DB     : TKADaoDatabase;
  125.  Error  : TDaoErrRec;
  126. Begin
  127.  NewDSN := '';
  128.  //************************************************************** ODBC Based
  129.  DB := TKADaoDatabase.Create(Nil);
  130.  DB.GoOffline;
  131.  DB.PrivateEngine        := True;
  132.  DB.DatabaseType         := 'ODBC';
  133.  DB.Database             := 'DATABASE=;DSN=';
  134.  DB.GoOnline;
  135.  if DB.Database='' Then
  136.     Begin
  137.      DB.Free;
  138.      Exit;
  139.     End;
  140.  Try
  141.    DB.Open;
  142.  Except
  143.    Error := DB.GetLastDaoError;
  144.    DB.Free;
  145.    ShowMessage(Error.Description);
  146.    Exit;
  147.  End;
  148.  if NOT DB.Connected Then
  149.     Begin
  150.       DB.Free;
  151.       Exit;
  152.     End;
  153.  NewDSN := DB.CoreDatabase.Connect;
  154.  DB.Free;
  155.  Button1.Click;
  156. End;
  157.  
  158. procedure TODBCDialog.ComboBox1Change(Sender: TObject);
  159. begin
  160.  NewDSN := '';
  161. end;
  162.  
  163. procedure TODBCDialog.ComboBox2Change(Sender: TObject);
  164. begin
  165.  NewDSN := '';
  166. end;
  167.                               
  168. end.
  169.