I had a lot of help here to get this sorted out with Parent and child nodes.
I am now after loading GrandChildNodes to the tree. These will be the actual files, when clicked on, that open in a RTB
It would be straight forward, but some of the Parent Nodes have Child Nodes, so to add a GrandChild Node, I could probably work out.
But Some of the Parent Nodes don't have Child Nodes.
I have attached a Pic of my DB Layout and also the treeview in my App.
How do I code to add a GrandChild node to all parent nodes?
As you will see in the TreeView pic, VBA has two child nodes, where as APi has no child node.
When you look in the DB_Fields.gif in Post#1, you can see the category API, Modules have no entries in the tvwChildNode field.
So, when loading the Nodes, when I get to the part that checks to see if there are any entries in the tvwChildNodes field, would I then have to check if there was anything in the tvwGrandChildNode field and then add it before continuing with the loop to load the rest of the nodes.
OR, will I have to save it differently? If so, how do I do this without creating an extra unnecessary level of nodes?
Hope this makes some sort of sense?
Why is nothing sraight forward in programming
If somebody helps you, take time to RATE the post. I do.
"FAILURE IS NOT AN OPTION. It comes bundled with the software."
Below are some of the threads that have helped me along the way:
I have resolved this, although not 100% the way I would have liked to.
As I know in advance what the categories are going to be, I have added a 'Select Case' statement to filter out the categories without the child node, then loaded the nodes this way.
If somebody helps you, take time to RATE the post. I do.
"FAILURE IS NOT AN OPTION. It comes bundled with the software."
Below are some of the threads that have helped me along the way:
It doesn't really make sense to check the GrandChild if there is no Child, so the code could go between lines 67 and 68 of your original post.
This does make it a bit more awkward in terms of adding Nodes, but to be honest I think you should deal with the actual Items (what you currently have in the GrandChild field) slightly differently. We should think about this tho, based on your current table design.
What I think you should have in this table is just the Nodes, and the actual items (including their titles) stored in a separate table. An extra Number field would then be added to that table (called NodeID?), which specifies which 'folder' the item goes in - it would link to the AutoNumber field in this table, so "RemoveAmp" and "Change Text Case" would have a NodeID value of 14.
If you do that, the ID's for the nodes would change from "P" & CStr(lngNodeParent) and "C" & CStr(lngNodeChild) to "N" & rs.Fields("AutoNumber").Value , and you would use a separate loop to add the items to the existing nodes.
Doing this would be a better design, as it has less repeating data - but it could be confusing for you!
The alternative is to keep what you've got, and after your existing new bit (between 67 and 68) add an Else clause (at the same level as the End If on line 68) with basically the same bit of code again - but this time adding to the Parent node instead of the Child.
edit: your new idea isn't a great one.. if there is ever any change in the data (such as a change of spelling of a node), you will need to modify (and re-compile) the program, instead of just changing data in the database.
Why is nothing straight forward in programming
It is if you look at a small enough level.. the bigger picture gets easily complicated if you don't plan it in advance. Unfortunately I haven't helped there, I seem to have made things a bit worse!
It is if you look at a small enough level.. the bigger picture gets easily complicated if you don't plan it in advance. Unfortunately I haven't helped there, I seem to have made things a bit worse!
Far from it si, I am here to learn. My problem is I am just a bit too impatient.
I have been doing programming in my own time, and learning with the help of this forum mainly, on and off for ages, and as of yet, I have yet to compile a program I have written.
This one is the closest I have come and that is what is making me impatient.
As for the DB, I can redesign this anyway needed.
The code I have saved in it, is saved as text files, which is what I did originally.
It's just as long as you guys are patient enough for me to catch up with what you are thinking (which everybody has been so far)
Originally Posted by si_the_geek
What I think you should have in this table is just the Nodes, and the actual items (including their titles) stored in a separate table. An extra Number field would then be added to that table (called NodeID?), which specifies which 'folder' the item goes in - it would link to the AutoNumber field in this table, so "RemoveAmp" and "Change Text Case" would have a NodeID value of 14.
If you do that, the ID's for the nodes would change from "P" & CStr(lngNodeParent) and "C" & CStr(lngNodeChild) to "N" & rs.Fields("AutoNumber").Value , and you would use a separate loop to add the items to the existing nodes.
Doing this would be a better design, as it has less repeating data - but it could be confusing for you!
This just shows how much I have to learn.
I didn't think that the design of the DB would be so important, and also that it should be sorted out first!
Originally Posted by si_the_geek
edit: your new idea isn't a great one.. if there is ever any change in the data (such as a change of spelling of a node), you will need to modify (and re-compile) the program, instead of just changing data in the database.
I sort of new this
I think I will go with the re-designing of the DB and go from their.
I will get a couple of pics of the two tables I have so far and post them, and if you wouldn't mind, get some advice on how to change them for the better.
If somebody helps you, take time to RATE the post. I do.
"FAILURE IS NOT AN OPTION. It comes bundled with the software."
Below are some of the threads that have helped me along the way:
OK, I have attached the two images of the two tables I have at present in my DB
Obviously, tbl_Nodes stores the names of the nodes, the grandchild nodes being the actual files to open (Well for most of them anyway )
tbl_Code is where the actual file is stored.
After the comments in another thread, I may wall use this re-design to not use the name Code for the field, maybe fldCode or similar.
vb Code:
This is the code I ended up using, when I hard coded it :o :
The field name Code is actually valid, it's just one of those that looks like a reserved word, but it isn't.
In tbl_Code I would have these fields:
Code_ID - the AutoNumber field (I don't see a reason for your current Code_ID data)
Code_Description - as it is
Notes - as it is
Code_Text - same as Code (just to avoid confusion over the field name!)
Node_ID - link to the Nodes table
In tbl_Nodes I would have these fields:
Node_ID - the AutoNumber field
NodeName - same as tvwNodes
ChildNodeName - same as tvwChildNodes
[GrandChildNodeName - if needed, an extra level of 'folders']
In theory (but only in theory!) you would not have a "Child" or "GrandChild" field, but instead have a "ParentID" field link which links back to the same table.. this would mean less data is stored (eg: "VB Code" would only be stored once), but it makes the code (and the database itself) much more complex, so isn't worth the hassle!
For the amount of data you have, there isn't much point in having indexes.
If chosen carefully they make searching a 'big' table faster, but are very unlikely to provide any benefit here, as there aren't enough rows of data to make a difference.
By setting it to "No Duplicates" it would mean that you couldn't have two items with the same name.. in tbl_Code this could be a problem, as it is perfectly valid to have two items with the same name, as long as they are under different nodes.
I am getting to work on changing the code to load the nodes now.
Just realised how much code else where I am probably going to have to change, in the save code and open code.
Originally Posted by si_the_geek
If you do that, the ID's for the nodes would change from "P" & CStr(lngNodeParent) and "C" & CStr(lngNodeChild) to "N" & rs.Fields("AutoNumber").Value ,
I've got this, I hope it is just that simple.
Originally Posted by si_the_geek
What I think you should have in this table is just the Nodes
you would use a separate loop to add the items to the existing nodes.
Not quite sure how to do this!
When I add the new data to the DB, it would be something like this?
Nodes_ID in the tbl_Nodes would be as follows:
API = 1
Modules = 2
VB Code Functions = 3
VB Code Subs = 4
So when I add a new piece of code to the DB:
tbl_Code - Node_ID would be the value of which ever node it is stored under, such as:
mRegistry (Module) would have '2' stored in the Node_ID field.
Then loop through the tbl_Code Node_ID field and would you load each entry as you go, or loop looking for any '1' and then any '2' etc?
ALSO
Would you have Modules as one of the Child nodes of VB?
I have also decided to leave the 'Notes' node off of the treeview. It is accessed else where now.
Sorry if it sounds obvious, but it ain't to me
Last edited by aikidokid; Apr 2nd, 2007 at 12:37 PM.
Reason: Added a question
If somebody helps you, take time to RATE the post. I do.
"FAILURE IS NOT AN OPTION. It comes bundled with the software."
Below are some of the threads that have helped me along the way:
When I add the new data to the DB, it would be something like this?
...
Exactly.
Then loop through the tbl_Code Node_ID field and would you load each entry as you go, or loop looking for any '1' and then any '2' etc?
You would do similar to the current setup you have for adding nodes, except reading from the tbl_Code table, and simply adding every item directly to the node - using .Nodes.Add "N" & rs.Fields("NodeID").Value, tvwChild, ...
Would you have Modules as one of the Child nodes of VB?
It's completely up to you.. it's easy enough to change it later if you want (you just need to edit the Nodes table).
Is the AutoNumber field also the Primary Key as well?
Yep.
The NodeID field in tbl_Code is a Foreign Key (links to a Primary Key of another table).
First of all I'd recommend changing the field names from AutoNumber (to NodeID etc), as it will make things easier to read.
For the Parent it should be added as you have it in the first post (but with the N change).
You don't actually need the lngNodeParent/lngNodeChild variables any more, but should instead be storing the N value (including the field value) to a variable instead. Note that if you don't have grandchild nodes, you wont need to store it for the child nodes.
You would then use that variable as the first parameter when adding the child nodes.
First of all I'd recommend changing the field names from AutoNumber (to NodeID etc), as it will make things easier to read.
Done.
Originally Posted by si_the_geek
For the Parent it should be added as you have it in the first post (but with the N change).
Done, with the "N" change.
Originally Posted by si_the_geek_
You don't actually need the lngNodeParent/lngNodeChild variables any more, but should instead be storing the N value (including the field value) to a variable instead. Note that if you don't have grandchild nodes, you wont need to store it for the child nodes.
So what I have done here is to save into the new variable the following:
API=N22
VB Code=N23
etc
Is this what you meant?
I put the '=' in so, if needed, string manipulation would be easier, regardless of how many items added to the array.
Originally Posted by si_the_geek
You would then use that variable as the first parameter when adding the child nodes.
Wouldn't what I almost already had do this:
vb Code:
.Nodes.Add "N" & rs.Fields("Node_ID").Value, tvwChild, "N" & rs.Fields("Node_ID").Value, strNodeChild, "Folder Closed"
Originally Posted by si_the_geek
Note that if you don't have grandchild nodes, you wont need to store it for the child nodes
Would the actual file names (mFormatVB) be classed as GrandChildNodes?
Don't know why it started at 22. When I redid the tables I deleted the old DB and created a new one.
Last edited by aikidokid; Apr 3rd, 2007 at 10:55 AM.
If somebody helps you, take time to RATE the post. I do.
"FAILURE IS NOT AN OPTION. It comes bundled with the software."
Below are some of the threads that have helped me along the way:
When I get to the line of code to add:
24 - Databases - Excel
I am getting the error: Element not found.
On this line of code:
vb Code:
.Nodes.Add "N" & rs.Fields("Node_ID").Value, tvwChild, strNodeChild, "Folder Closed"
"N" & rs.Fields("Node_ID").Value = 24
strNodeChild = Excel
I can't work this out as it loops through as (I) would expect it to, and shows the values I would expect.
I have the string array NodesKey(keyctr) that holds the Nodes key number and field value. I have added a string called OldStrNodeParent to use to loop through and load the Child Nodes.
So what I have done here is to save into the new variable the following:
API=N22
VB Code=N23
etc
Is this what you meant?
Nope.. what I meant was just store the last value to a single variable (so as soon as "VB Code" loads it would contain "N23").
This is so you can use it as the first parameter to .Add, which specifies the parent node (as you are getting the first parameter from the recordset, your current code basically specifies itself - before it is added!).
Would the actual file names (mFormatVB) be classed as GrandChildNodes?
Basically yes in most cases (in some they are just Child nodes), but technically they are Leaf nodes - they have a parent (and possibly grandparent etc), but no children of their own.
Don't know why it started at 22. When I redid the tables I deleted the old DB and created a new one.
That sounds a bit odd, but isn't something to worry about - as long as the other table uses the same numbers it will work fine.
I have the string array NodesKey(keyctr) that holds the Nodes key number and field value. I have added a string called OldStrNodeParent to use to loop through and load the Child Nodes.
You don't actually need these, or any of the other arrays.
By ordering the data in the recordset (I think we added an Order By?) you can be sure that all items with the same NodeName will be grouped together - and as strNodeParent etc are only set within the If statement, they will always contain the appropriate parent.
You just need strNodeParent (so that you can tell when a new parent should be added) and a string variable (perhaps strNodeParentID ?) to contain "Nxx", which allows the child nodes to be added to it.
Basically yes in most cases (in some they are just Child nodes), but technically they are Leaf nodes - they have a parent (and possibly grandparent etc), but no children of their own.
So will I be adding nodes for these when I get to loading them?
Originally Posted by si_the_geek
That sounds a bit odd, but isn't something to worry about - as long as the other table uses the same numbers it will work fine.
I have re-done the DB again, and it is starting from 1. The original DB ended at 21, so for some reason it carried on from there.
Originally Posted by si_the_geek
You don't actually need these, or any of the other arrays.
So you wouldn't store the nodes names, Parent & Child, in the array to populate other combo boxes in the app, you would just make a call to the DB each time they were needed?
So far I have the following code, which adds all of the Parent Nodes, but is having a problem loading the Child nodes.
The error is on the line where the node is added in the second If statement.
I know what is causing the error, I just don't know how to change it to to get it to work.
It's the KEY parameter of the .Node.Add line.
I am not sure what this is or where I am getting it from.
So will I be adding nodes for these when I get to loading them?
Yep.
So you wouldn't store the nodes names, Parent & Child, in the array to populate other combo boxes in the app, you would just make a call to the DB each time they were needed?
Correct.. as the chances are you don't want exactly the same data - and the database is far better at finding/sorting data than VB code can ever be (actually for an Access database on the local computer that isn't completely true, but it will be less work!).
So far I have the following code, which adds all of the Parent Nodes, but is having a problem loading the Child nodes.
The error is on the line where the node is added in the second If statement.
I know what is causing the error, I just don't know how to change it to to get it to work.
It's the KEY parameter of the .Node.Add line.
I am not sure what this is or where I am getting it from.
You don't need it - strNodeParentID should just be a string (containing the latest parent), not an array of strings.
This also removes the need for KeyCtr, and ReDim Preserve (which is so slow it would lose a race to a dead snail!).
By the way, that inner Do Loop shouldn't be there.