Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa tarde,
Eu estou aqui tentando fazer os exercicios do livro, mas cheguei num ponto aqui que deu um erro e não sei qual é!
O programa conecta, mostras informações sobre o banco, porem ele perde a conexao, tudo indica que seja no PageControl1Change...
Ah! Meu banco de dados é em access.
Se alguem puder olhar o código para min e dar uma ajuda eu agradeço!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, ComCtrls, Shellapi, StdCtrls, Buttons, DB, ADODB,
ImgList, Grids, DBGrids;
const
{ Para o System Tray }
WM_ICONTRAY = WM_USER + 1;
type
TForm_Banco_Dados = class(TForm)
PageControl1: TPageControl;
TabSheet_Conectar: TTabSheet;
TabSheet_Visualizar: TTabSheet;
TabSheet_CriarDB: TTabSheet;
TabSheet_CriarTabela: TTabSheet;
TabSheet_GerarDados: TTabSheet;
TabSheet_ImportarDados: TTabSheet;
TabSheet_Manutencao: TTabSheet;
TabSheet_EditarBD: TTabSheet;
TabSheet_Relatorio: TTabSheet;
Image1: TImage;
Label_Fecha_Aplicativo: TLabel;
Label_System_Tray: TLabel;
Label_Minimiza_Aplicativo: TLabel;
Label_Muda_Posicao_Aplicativo: TLabel;
Edit_Conectar_BD_Conexao: TEdit;
Memo_Conectar_StringConexao: TMemo;
Label_BD: TLabel;
Label_String_Conexao: TLabel;
Label_StoredProcedures: TLabel;
Label_Propiedades: TLabel;
SpeedButton_Conectar_BD: TSpeedButton;
Memo_Conectar_StoredProcedures: TMemo;
Memo_Conectar_Propriedades: TMemo;
Shape_Conectar: TShape;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
Label_Banco_Dados_Inicial: TLabel;
TreeView_Visualizar: TTreeView;
DBGrid_Visualizar: TDBGrid;
GroupBox_Info_Banco_Dados: TGroupBox;
Label_Total_Campos: TLabel;
Label_Total_Tabelas: TLabel;
Label_Tabela_Selecionada: TLabel;
Label_Campo: TLabel;
Label_Tipo_Campo: TLabel;
Label_Num_Tabela_Tree: TLabel;
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Label_Muda_Posicao_AplicativoMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Label_Muda_Posicao_AplicativoMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Label_Muda_Posicao_AplicativoMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure Label_Fecha_AplicativoClick(Sender: TObject);
procedure Label_Minimiza_AplicativoClick(Sender: TObject);
procedure Label_System_TrayClick(Sender: TObject);
procedure SpeedButton_Conectar_BDClick(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure TreeView_VisualizarDblClick(Sender: TObject);
procedure DBGrid_VisualizarDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
private
{ Private declarations }
TrayIconData: TNotifyIconData;
public
{ Public declarations }
procedure TrayMouseClick(var Msg: TMessage); message WM_ICONTRAY;
end;
var
Form_Banco_Dados: TForm_Banco_Dados;
TreeNode1, TreeNode2, TreeNode3: TTreeNode;
TreeNode1MT, TreeNode2MT, TreeNode3MT: TTreeNode;
Linha_Selecionada: Integer;
Flag_TreeView2MT_NoPai: Boolean;
MainForm: TForm_Banco_Dados;
Mouse_Down: Boolean;
implementation
{$R *.dfm}
procedure TForm_Banco_Dados.FormCreate(Sender: TObject);
begin
with TrayIconData do
begin
Application.Icon.LoadFromFile('Icone_Aplicativo.ico');
cbSize := SizeOf(TrayIconData);
Wnd := Handle;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_ICONTRAY;
hIcon := Application.Icon.Handle;
//Tooltip mostrado quando o mouse passa sobre o icone do system tray
StrPCopy(szTip, Form_Banco_Dados.Caption);
end;
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;
procedure TForm_Banco_Dados.FormDestroy(Sender: TObject); Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;
procedure TForm_Banco_Dados.Label_Muda_Posicao_AplicativoMouseDown(
Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Mouse_Down := True;
end;
procedure TForm_Banco_Dados.Label_Muda_Posicao_AplicativoMouseUp(
Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Mouse_Down := False;
end;
procedure TForm_Banco_Dados.Label_Muda_Posicao_AplicativoMouseMove(
Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if (Mouse_Down = True) then
begin
Form_Banco_Dados.Left := Form_Banco_Dados.Left + (X-12);
Form_Banco_Dados.Top := Form_Banco_Dados.Top + (Y-12);
end;
end;
procedure TForm_Banco_Dados.Label_Fecha_AplicativoClick(Sender: TObject); Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
Application.Terminate;
end;
procedure TForm_Banco_Dados.Label_Minimiza_AplicativoClick(Sender: TObject); Application.Minimize;
end;
procedure TForm_Banco_Dados.Label_System_TrayClick(Sender: TObject);end;
procedure TForm_Banco_Dados.TrayMouseClick(var Msg: TMessage); Form_Banco_Dados.Show;
end;
WM_RBUTTONDOWN:
begin
with Application do
begin
NormalizeTopMosts;
if (MessageBox('Deseja encerrar o aplicativo?',
'Atenção!',MB_YESNO) = IDYES) then
begin
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
Application.Terminate;
RestoreTopMosts;
end;
end;
end;
end;
end;
procedure TForm_Banco_Dados.SpeedButton_Conectar_BDClick(Sender: TObject); Contador: Integer;
Auxiliar: String; Shape_Conectar.Brush.Color := clGray;
Label_Banco_Dados_Inicial.Caption := 'Não conectado';
if (ADOConnection1.Connected = False) then
ADOConnection1.Open;
ADOConnection1.ConnectionObject.DefaultDatabase :=
Edit_Conectar_BD_Conexao.Text;
if (StrPos(
PChar(ADOConnection1.ConnectionObject.DefaultDatabase),
PChar(Edit_Conectar_BD_Conexao.Text)) = nil) then
begin
Shape_Conectar.Brush.Color := clSkyBlue;
Memo_Conectar_StringConexao.Text := ADOConnection1.ConnectionString;
Label_Banco_Dados_Inicial.Caption :=
'Banco de dados: ' + ADOConnection1.DefaultDatabase;
//Verifica e mostra a lista de stored procedures
ADOConnection1.GetProcedureNames(Memo_Conectar_StoredProcedures.Lines);
//Verifica e mostra a relação de propriedades da conexão
Memo_Conectar_Propriedades.Lines.Clear;
for Contador := 0 to ADOConnection1.Properties.Count-1 do
begin
Auxiliar := ADOConnection1.Properties.Item[Contador].Value;
Memo_Conectar_Propriedades.Lines.Add(
IntToStr(Contador) + ' ' +
ADOConnection1.Properties.Item[Contador].Name +
' = ' + Auxiliar);
end;
end
else
begin
Shape_Conectar.Brush.Color := clGray;
end;
end;
procedure TForm_Banco_Dados.PageControl1Change(Sender: TObject);
var
Tabelas: TStrings;
Index: Integer;
Contador: Integer;
Lista_de_Campos: String;
Campo: String;
begin
Tabelas := TStringList.Create;
TreeView_Visualizar.Items.Clear;
DBGrid_Visualizar.DataSource.Enabled := False;
try
ADOConnection1.GetTableNames(Tabelas, False);
with TreeView_Visualizar.Items do
begin
//Adiciona um nó pai com o nome do banco de dados
TreeNode1 := Add(nil, ADOConnection1.DefaultDatabase);
TreeNode1.ImageIndex := 1;
end;
for Index := 0 to (Tabelas.Count - 1) do
begin
ADODataset1.Close;
if (StrPos(Pchar(Tabelas[Index]), ' ') <> nil) then
ADODataset1.CommandText := 'SELECT * FROM [' + Tabelas[Index] + ']'
else
ADODataset1.CommandText := 'SELECT * FROM ' + Tabelas[Index];
ADODataset1.Open;
Lista_de_Campos := '';
Lista_de_Campos := ADODataset1.FieldList.CommaText;
with TreeView_Visualizar.Items do
begin
//Adiciona um nó pai com o nome da tabela
TreeNode2 := AddChild(TreeNode1, Tabelas[Index]);
TreeNode2.ImageIndex := 2;
end;
for Contador := 1 to StrLen(PChar (String(Lista_de_Campos))) do
begin
if (Lista_de_Campos [Contador] <> ',') then
begin
Campo := Campo + Lista_de_Campos [Contador];
end
else
begin
with TreeView_Visualizar.Items do
begin
//Adiciona um nó filho com o nome do campo
TreeNode3 := AddChild(TreeNode2, Campo);
TreeNode3.ImageIndex := 3;
end;
Campo := '';
end;
end;
with TreeView_Visualizar.Items do
begin
//Adiciona um nó filho com o nome do campo
TreeNode3 := AddChild(TreeNode2, Campo);
TreeNode3.ImageIndex := 3;
end;
Campo := '';
DBGrid_Visualizar.DataSource.Enabled := True;
end;
finally
Tabelas.Free;
end;
TreeView_Visualizar.FullExpand;
end;
procedure TForm_Banco_Dados.TreeView_VisualizarDblClick(Sender: TObject);
var
Tipo: String;
Nome_Tabela: String;
Classe: TFieldClass;
begin
Label_Total_Tabelas.Caption := '';
Label_Tabela_Selecionada.Caption := '';
Label_Campo.Caption := '';
Label_Tipo_Campo.Caption := '';
Label_Num_Tabela_Tree.Caption := '';
Label_Total_Campos.Caption := '';
if (TreeView_Visualizar.Selected.Level = 0) then
begin
Label_Total_Tabelas.Caption := 'Total de tabelas: ' +
IntToStr (TreeView_Visualizar.Selected.Count);
DBGrid_Visualizar.DataSource.Enabled := False;
end
else
DBGrid_Visualizar.DataSource.Enabled := True;
if (TreeView_Visualizar.Selected.Level = 1) then
begin
Label_Num_Tabela_Tree.Caption := 'Tabela nº: ' +
IntToStr (TreeView_Visualizar.Selected.Index + 1);
Label_Total_Campos.Caption := 'Total de campos: ' +
IntToStr (TreeView_Visualizar.Selected.Count);
ADODataset1.Close;
Nome_Tabela := TreeView_Visualizar.Selected.Text;
if (StrPos(PChar(TreeView_Visualizar.Selected.Text), '') <> nil) then
ADODataset1.CommandText := ' SELECT * FROM [' + Nome_Tabela + ']'
else
ADODataset1.CommandText := ' SELECT * FROM ' + Nome_Tabela;
ADODataset1.Open;
Label_Tabela_Selecionada.Caption := 'Tabela selecionada: ' + Nome_Tabela;
end;
if (TreeView_Visualizar.Selected.Level = 2) then
begin
Label_Tabela_Selecionada.Caption :=
'Tabela selecionada: ' +
TreeView_Visualizar.Items.Item[
TreeView_Visualizar.Selected.AbsoluteIndex -
TreeView_Visualizar.Selected.Index-1].Text;
Label_Campo.Caption := 'Campo: '+
TreeView_Visualizar.Selected.Text;
DBGrid_Visualizar.Refresh;
ADODataset1.Close;
Nome_Tabela := TreeView_Visualizar.Items.Item[
TreeView_Visualizar.Selected.AbsoluteIndex -
TreeView_Visualizar.Selected.Index-1].Text;
if (StrPos(PChar(Nome_Tabela), '') <> nil) then
ADODataset1.CommandText := ' SELECT * FROM [' + Nome_Tabela + ']'
else
ADODataset1.CommandText := ' SELECT * FROM ' + Nome_Tabela;
ADODataset1.Open;
Classe :=
ADODataset1.FieldDefs.Items[TreeView_Visualizar.Selected.Index].FieldClass;
if (Classe = TStringField) then
Tipo := 'TStringField';
if (Classe = TWideStringField) then
Tipo := 'TWideStringField';
if (Classe = TSmallIntField) then
Tipo := 'TSmallIntField';
if (Classe = TIntegerField) then
Tipo := 'TIntegerField';
if (Classe = TBooleanField) then
Tipo := 'TBooleanField';
if (Classe = TFloatField) then
Tipo := 'TFloatField';
if (Classe = TCurrencyField) then
Tipo := 'TCurrencyField';
if (Classe = TDateField) then
Tipo := 'TDateField';
if (Classe = TTimeField) then
Tipo := 'TTimeField';
if (Classe = TDateTimeField) then
Tipo := 'TDateTimeField';
if (Classe = TVarBytesField) then
Tipo := 'TVarBytesField';
if (Classe = TBlobField) then
Tipo := 'TBlobField';
if (Classe = TWordField) then
Tipo := 'TWordField';
if (Classe = TBCDField) then
Tipo := 'TBCDField';
if (Classe = TBytesField) then
Tipo := 'TBytesField';
if (Classe = TNumericField) then
Tipo := 'TNumericField';
Label_Tipo_Campo.Caption := 'Tipo de Campo: ' + Tipo;
end;
end;
procedure TForm_Banco_Dados.DBGrid_VisualizarDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Column.Field.Index = TreeView_Visualizar.Selected.Level) then
begin
if (TreeView_Visualizar.Selected.Level = 2) then
begin
DBGrid_Visualizar.Canvas.Font.Color := clNavy;
DBGrid_Visualizar.Canvas.Brush.Color := clYellow;
end;
end
else
begin
DBGrid_Visualizar.Canvas.Font.Color := clBlack;
DBGrid_Visualizar.Canvas.Brush.Color := clWhite;
end;
DBGrid_Visualizar.DefaultDrawDataCell(
Rect, DBGrid_Visualizar.Columns[datacol].Field, state);
end;
end.
O erro que da é quando eu mudo de "aba", ai aparece isso.
/applications/core/interface/imageproxy/imageproxy.php?img=http://img79.imageshack.us/img79/3007/99673318ip0.th.png&key=a0a4850deba9f62c95f67ea19028bbf5313b7bc98bd1578df5691f0f8ae570cd" alt="Imagem Postada" />/applications/core/interface/imageproxy/imageproxy.php?img=http://img79.imageshack.us/images/thpix.gif&key=b28eba59e4db0569cc3dadbc3e1f965cce782bf180133bf3175e8c1c4437a51d" alt="Imagem Postada" />/applications/core/interface/imageproxy/imageproxy.php?img=http://img227.imageshack.us/img227/4449/34605023xx5.th.png&key=277fb613eeb554914d388d6181c974eb66273e1c9477209481aa9d5e27541a07" alt="Imagem Postada" />/applications/core/interface/imageproxy/imageproxy.php?img=http://img227.imageshack.us/images/thpix.gif&key=16b343513d73444f39ae9f2cb12679086b094ebaaa0cb326383245ea16e73d76" alt="Imagem Postada" />/applications/core/interface/imageproxy/imageproxy.php?img=http://img263.imageshack.us/img263/6992/65234459ju0.th.png&key=3a6b1b464641db9fefd074bd949660d2cab309c7fb65f6eb708a120b886db15a" alt="Imagem Postada" />/applications/core/interface/imageproxy/imageproxy.php?img=http://img263.imageshack.us/images/thpix.gif&key=4deba5e53bcfad3ad8f3c6819237aee8b515d347ef17a17e9f53f01ba1004522" alt="Imagem Postada" />
Abraço
Carregando comentários...