I want information from TADOQuery
to be loaded into a TTreeView
. For example, I want it to be loaded as Field1->Add
in Table1
and as Field2->AddChild
with buttonClick
. But when I run the code, I am getting an error:
Access violation at adress 0043616B in module "TRV2.exe"
I'm making a mistake or something is missing. Can you guide me?
procedure TForm1.AddButtonClick(Sender: TObject);
var
t: Integer;
MyNode, Node : TTreeNode;
begin
MyNode := Node;
t := Node.AbsoluteIndex;
TreeView1.Items.Add(MyNode, ADOQuery1.FieldByName('CODE_NAME').AsString);
end;
procedure TForm1.AddChildButtonClick(Sender: TObject);
var
t: Integer;
MyNode, Node: TTreeNode;
begin
MyNode := Node;
t := Node.AbsoluteIndex;
TreeView1.Items.Add(MyNode, ADOQuery1.FieldByName('CODE_CHILD').AsString);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
t: Integer;
MyNode, Node: TTreeNode;
begin
MyNode := Node;
t := Node.AbsoluteIndex;
ADOQuery1.Open;
end;
UPDATE: I want to get the whole table and update the TTreeView
when I add new Add and Child to the database. With these codes (AddButtonClick
and AddChildButtonClick
) I can only import the first values into the TTreeView
. I wonder if a loop is needed?
CodePudding user response:
MyNode
and Node
are both local variables that you are not initializing to anything. Your AV is because you are trying to access an object that doesn't exist.
Try using a class member instead, where you initialize it with one button click, and then use it with the other button click, eg:
private
MyNode: TTreeNode;
...
procedure TForm1.AddButtonClick(Sender: TObject);
begin
MyNode := TreeView1.Items.Add(nil, ADOQuery1.FieldByName('CODE_NAME').AsString);
end;
procedure TForm1.AddChildButtonClick(Sender: TObject);
begin
if MyNode <> nil then
TreeView1.Items.AddChild(MyNode, ADOQuery1.FieldByName('CODE_CHILD').AsString);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ADOQuery1.Open;
end;
UPDATE: to iterate through multiple records in the query result, you need to call TADOQuery.Next()
in a loop until TADOQuery.Eof
is true.
CodePudding user response:
The database was taken into treview with the following codes. There is something missing. Because Field1=Add and Field2=Child. Same Fields repeating.
procedure TForm1.AddButtonClick(Sender: TObject);
var
CurrentDeptID, RecordDeptID: Integer; RootNode, DeptNode: TTreeNode;
begin
CurrentDeptID := 0;
TreeView1.Items.Clear;
RootNode := TreeView1.Items.Add(DeptNode, 'CODE_NAME');
DeptNode := nil;
ADOQuery1.SQL.Text := 'Select * from Tablo1 where CODE_NAME= CODE_NAME';
ADOQuery1.Open;
try
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
RecordDeptID := ADOQuery1.FieldByName('ID').AsInteger;
if ( DeptNode = nil) or (RecordDeptID <> CurrentDeptID) then
begin
DeptNode := TreeView1.Items.AddChild(RootNode,
ADOQuery1.FieldByName('CODE_NAME').AsString); //
CurrentDeptID := RecordDeptID;
end;
TreeView1.Items.AddChild(DeptNode,
ADOQuery1.FieldByName('CODE_CHILD').AsString);
ADOQuery1.Next;
end;
finally
ADOQuery1.close;
end;
end;
[https://i.stack.imgur.com/kNojV.jpg]
Blockquote