#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Question Recursive procedures


    Hi All,

    I am seeking advice with Delphi and Recursive Procedures. Basically I want a procedure to call itself BUT some of the member variables are TADOConnection and TADORecordset. When calling itself, and attempting to create these members, the code bombs with an exception.

    Are the variables not recognized as completely new members? In C++ the variables would be NULL in each call of the procedure until I explicitly create them... Is this not the case in delphi?

    Thanks in advance,

    Chris
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Update...


    Just an update..

    I removed the TADOConnection and TADOQuery variables to be global throughout the form... but still I have a problem with accessing variables in the recursive calls... any ideas? The code is below:

    Code:
    procedure TfrmPromotions.PopulateTreeNode(nodeItem : TMyNode; nID : Integer);
    Var
      strSQL: String;
      Item: TMyNode;
    begin
    // Populate the tree here...
      strSQL := 'SELECT PROMOTION_ID, [DATETIME], SHORT_DESC, LONG_DESC, LENGTH_OF_EFFECT, PARENT_PROMO_ID FROM PROMOTION WHERE PARENT_PROMO_ID = ';
      strSQL := strSQL + IntToStr(nID);
      strSQL := strSQL + ' ORDER BY PARENT_PROMO_ID;';
    
      rs.SQL.Text := strSQL;
      rs.Open();
    
      while rs.Eof <> TRUE do
      begin
           Item := TMyNode(m_treeview.Items.Add(nodeItem, rs.Fields[2].AsString));
           Item.m_nID :=  Integer(rs.Fields[0].Value);
           PopulateTreeNode(Item, Item.m_nID);   // recursive call...
           rs.Next();
      end;
      rs.Close();
    end;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Can Anyone Help Pls? Tq..


    Can anyone help with this?

    Thank you,

    Chris
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Question


    Hi,

    Thanks for replying. Actually the rs used to be defined within the procedure itself. I was just simply trying somthing out by removing it and making it a public variable.

    PARENT_PROMO_ID tells the tree which node is it's parent item. So I call the code initially getting all of the top level items (PARENT_PROMO_ID = -1) and then for each item, I wish to call it again to find all the children etc...

    Problem is, it doesnt seem to like re-using the rs. In C++ the object will get created everytime you call the procedure when it is defined locally but Delphi just seems to bomb out even when rs is defined locally OR as a member of the form.

    The first call is always fine in both instances, but the second iterative call to the procedure always always bombs out.

    Thank you for your help.

    Chris
  8. #5
  9. Code Cruncher
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Tasmania, Australia
    Posts
    121
    Rep Power
    14
    I see a problem here.
    I assume "rs" is a dataset defined outside this procedure, you are opening it and closing it with the recursive call in the middle, close your dataset before doing the recursive call.

    Why do you think you need recursion for this code?
    What are you trying to do here with this tree?

    The procedure you have written will fill the tree for you if you just take the recursive call out alltogether.

    Recursion is usually used to traverse the tree itself, not to populate it from the database in this way.
    Beware of a programmer with a screwdriver!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Talking


    Oops, clicked the wrong reply button... please see the thread for my reply.

    Thanks,
    Chris

    Originally Posted by tim snl
    I see a problem here.
    I assume "rs" is a dataset defined outside this procedure, you are opening it and closing it with the recursive call in the middle, close your dataset before doing the recursive call.

    Why do you think you need recursion for this code?
    What are you trying to do here with this tree?

    The procedure you have written will fill the tree for you if you just take the recursive call out alltogether.

    Recursion is usually used to traverse the tree itself, not to populate it from the database in this way.
  12. #7
  13. Code Cruncher
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Tasmania, Australia
    Posts
    121
    Rep Power
    14
    The first procedure you had sounds like it should work.
    If you are still having a problem with this post that code and I will have a look at it for you.
    Beware of a programmer with a screwdriver!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Thumbs down


    Hi,

    I put the rs back into the local function and still get the error. An access violation is generated when the PopulateTreeNode procedure calls itself and when it hits the line that reads rs := TADOQuery.Create(nil);

    This is very simple in VC++ so I guess I am missing something very simple in Delphi. Any help you can offer will be greatly appreciated.

    Code is:

    Code:
    procedure TfrmPromotions.FormCreate(Sender: TObject);
    begin
      ADOMasterConnection := TADOConnection.Create(nil);
      ADOMasterConnection.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=MS Access Database;DBQ=HPMonMaster.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;"';
      PopulateTreeNode(nil, -1);
    end;
    
    procedure TfrmPromotions.PopulateTreeNode(nodeItem : TMyNode; nID : Integer);
    Var
      strSQL: String;
      Item: TMyNode;
      rs : TADOQuery;
    begin
    // Populate the tree here...
      rs := TADOQuery.Create(nil);
      rs.Connection := ADOMasterConnection;
      
      strSQL := 'SELECT PROMOTION_ID, [DATETIME], SHORT_DESC, LONG_DESC, LENGTH_OF_EFFECT, PARENT_PROMO_ID FROM PROMOTION WHERE PARENT_PROMO_ID = ';
      strSQL := strSQL + IntToStr(nID);
      strSQL := strSQL + ' ORDER BY PARENT_PROMO_ID;';
    
      rs.SQL.Text := strSQL;
      rs.Open();
    
      while rs.Eof <> TRUE do
      begin
           Item := TMyNode(m_treeview.Items.Add(nodeItem, rs.Fields[2].AsString));
           Item.m_nID :=  Integer(rs.Fields[0].Value);
           PopulateTreeNode(Item, Item.m_nID);   // recursive call...
           rs.Next();
      end;
      rs.Close();
    end;
    Thank you,
    Chris

IMN logo majestic logo threadwatch logo seochat tools logo