Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview inquiry 1

Status
Not open for further replies.

irishjoe

Programmer
Aug 5, 2002
76
AU
Does anyone here know all the ins and outs of the access treeview?

First I would like to know are the contents of a treeview saved when the user clicks save or when they are stored?

This is because an error is cropping up when the number of items in the tree is modified. When I output the number of nodes in the onload of the form containing the tree, it comes up with a number say 100 and if I do the same output at the end of the onload, it comes out with say 150. Now if the user hits the save button, what number should the treeview save?
On my version, it reverts to the original 100 instead of saving the most up to date number of nodes.

Code:
Debug.Print "Onload of Mainpage: " & axTreeView.Nodes.Count

Also, is there any other way of removing all nodes apart from the following loop?
Code:
For ii = axTreeView.Nodes.Count To 1 Step -1
axTreeView.Nodes.Remove (ii)
Next ii

Thanks to anyone who can assist me.

 
Does anyone here know all the ins and outs of the access treeview?

1)Yes I do. I have posted numerous code on this site working with treeviews.

First I would like to know are the contents of a treeview saved when the user clicks save or when they are stored?

2) I have no idea what you are asking. The question does not make sense to me.


3)If you are working with a database normally the treeview is created dynamically. I have never tried to add nodes and then save it. I would assume you would need to pop the form into design view,add the nodes, save the form, pop it open in form view.

For ii = axTreeView.Nodes.Count To 1 Step -1
axTreeView.Nodes.Remove (ii)
Next ii
4)I think there is a clear method that removes all.

Do some googling, there is a lot of treeview code out there.
 
Thanks for the reply.
Ok, to clarify the second bit, when you take off all coding references to filling the treeview with data and then load the form, there are nodes in there when the form loads. The previous programmer had code to remove all nodes then fill it up which is in the onload part of the form.
I am confused as to how there can be nodes in there without any code filling it up.
Do you know of any alternate methods of filing a tree without code? I have searched the code for references to the tree and not found anything that isn’t called in the onload part.

There is code to clear, axTreeView.nodes.clear but it doesn’t always work. Something to do with, if a node is being referenced, it won’t remove it.
 
Although I always load the tv dynamically I would assume that you can fill the nodes in design view which will "hardwire" persistent data. The easiest would be to delete the current treeview and drop a new one on the form with the same name.
 
I do not have vba in front of me but I am now less certain that you can save the nodes collection in persistent memory. I would then think that maybe something else is coded somewhere, I will take a look at that later today. On the clear method can you set the focus away from the tv so that no node is selected? That may solve your error message.
 
I have been trying to fix the problem since posting yesterday and think I am making headway. I looked at other databases that use the treeview and found that if you go into the design view and look at the treeview, it doesn’t show actual data, it looks more like this.
- * Sample Node
- * Sample Node
- * Sample Node
- * Sample Node

(The stars are meant to be icons)
But when I go the treeview in this database, it shows the actual data rather than “Sample Node”.
So I have been looking into ways to clear this. I found that if you click once then click again (not double click) you can get in to select the nodes. If I select them all then press the delete button, it deletes the whole treeview but if I click undo, the tree re-appears but without the nodes. It just comes up blank. And when I run the form, the onload part says there are 0 nodes in the tree.

Unfortunately, while this makes the database work on my computer, when I tested it on another computer, its started coming up with other errors that I am about to start looking into.

The more I try and fix this database, the more I think its just fcuked beyond help!

I have tried deleting the treeview and putting a new one in but this only fixes the problem for maybe a day or 2 then it somehow remembers what nodes are in it.


 
Think I may be going crazy, this seems completely illogical to me…
Got rid of all the nodes as explained above but when I run on it on another computer it comes up with an “Imagelist must be initialised before it can be used” error. Fair enough, but here is the confusing part…
If I open the form straight away, it comes back with this error. If I go into design view first then open the form, it doesn’t throw up the error and loads correctly! WTF???

I could swear there are ghosts in this database that are determined not to play ball!
 
In which event do you initialize your imagelist and which events do you load the images into your treeview? Could you post the code for the imagelist initialization and the code where the error is thrown?
 
The treeview is loaded in the onload part of the form that it’s on. The ImageList is not done in code, it’s done on the form. There is a little square activeX control on the form that holds all the icons for the treeview. I have looked through the code and can’t find any reference to the imagelist in the code behind the form or anywhere else.

The code stops when it is trying to add a node to the tree
Code:
axTreeView.Nodes.Add , , node & "_" & strNodeidx, strNodeval , node & ".img"
“ImageList must be initialized before it can be used.”

But if this was working before, why would it stop because I deleted and re-added the treeview?

The only other thing that could be interfering is the splash screen. There is code in the onOpen part of the form that opens a page for 2 seconds then closes itself. Could that affect it?

Thanks for your help, its greatly appreciated!
 
Some code has to be missing. The treeview.nodes.add method looks like:

TreeView.Nodes.Add relative, relationship, key, text, image, selectedimage

where selectedimage is the index of an image in the imagelist. So in your code

axTreeView.Nodes.Add , , node & "_" & strNodeidx, strNodeval , node & ".img"

you must have some image saved in the imagelist with the index "nodename.img".

However, you have to tell the treeview where to get the images from. Something like:

Private Sub Form_Load()

' Tell the TreeView where it's images are.
TreeView1.ImageList = ImageList1

So if you do not have a reference to the imagelist your are going to get the above error. Was some code deleted.
 
Are you sure you have to have code for the treeview to use the imagelist? A have made a database up (a long time ago) that had a treeview with an imagelist and I didn’t put in any code.
For that database, to add a node to the top level, I had
filterTree.Nodes.Add , 4, "HH1", "1- Piping", "0.img"
And I have double checked that I didn’t put in any code to be able to access the “0.img” icon. I added the activeX icon to the main page and added the picture in there and called it “0.img”.
Never had any problems with that database, its just this current one isn’t playing ball.

I have searched for “axImageList” and “ImageListCtrl” thru all the code and it doesn’t exist anywhere. Are you sure it isn’t just as simple as double clicking on the ImageListCtrl in the form, adding the images to the images tab, giving them the correct names, then in the properties of the treeview, setting the ImageList to the name that you called the activeX control?

 
Sorry, I assumed you were doing it dynamically. You are correct you can associate the imagelist in design view or in code. Working with db's I always load it dynamically so that the user can add new icons for different records. Here is why I always load dynamically:
Important You should populate the ImageList control with images before you associate it with another control. Once you have associated an ImageList with a control, and assigned any image to a property of the control, the ImageList control will not allow you to add any more images.

As said you can do it either way
To associate the ImageList control with the TreeView, TabStrip, or Toolbar control at design time

Right-click on the control using images from the ImageList control and click Properties to display the Property Pages dialog box.


On the General tab, select the name of the ImageList control from the ImageList box.
To associate the ImageList control at run time, you might use the following code:

' Associate an ImageList named "imlImages" with a
' TreeView control named "tvwDB."
Set tvwDB.ImageList = imlImages

Once you have associated an ImageList control with another control, you can set properties for various objects using either the Key or Index property of an image in the ImageList control. For example, the following code sets the Image property of a TreeView control's Node object to an ImageList image with the Key property "leaf."

Private Sub Form_Load()
' The TreeView is named "tvwData."
' Add a node and set its Image property.
' The Key value of the image is "leaf."
tvwData.Nodes.Add , ,"1 node","Top","leaf"
End Sub

But I think I understand what is going on and it deals with order of operations. You stated
I added the activeX icon to the main page and added the picture in there and called it “0.img”.

which hints to me that the treeview is on a subform. In that is the case, a subform loads prior to the main form loading. Therefore the on load event of the subform runs prior to the main form loading and thus prior to the imagelist being instantiated. This would then explain why if you open in design view first and then in form view it will work.

Simple test:
prior to where your code breaks put a msgbox and try to return the imagelist's name.

But now I am curious. If you have an image list instantiated on another form than the control using it can it reference that imagelist? I would have assumed that it needed to be on the local form, but I will now have to try.

So a possible solution if this is the case is to populate the tree from the main forms on load event.
 
Sorry for not replying, for some reason Outlook started putting the email notifications in a spam folder instead of my inbox.
I was trying to load the imagelist your way instead of thru the design interface but having problems. Do I need to add references to the code to be able to reference the trees functions?
I was trying your debug idea of
Code:
Debug.Print axTreeView.ImageList
at the start but it won’t run. “Object variable or with variable not set.” The intellisense comes up with options when I type in the name of the treeview then dot but ImageList is not in there.

Sorry to mislead you but the treeview is on the main form and the ImageList is there too. I tried moving the ImageList to one of the sub forms on the right but bad things started happening so I gave up. (Somehow all the alignments went out and the tabs moved all the way to the left)

The code for loading the tree is in the OnLoad of the main form.
 
I may or not have fixed the problem. This was my chain of thought….
I have deleted the treeview and added it again and it can’t connect to the ImageList correctly. But when I go into design mode then into form mode, it works but if you go straight in, it can’t find the ImageList. So, that means it must be something to do with the order that access is loading them into memory.
I made a copy of the database, opened the main form and copied the ImageList. I went into the original database and deleted the ImageList. I then pasted the new ImageList in and everything started working.

I have no idea how this is different for access but its started working. I have tested it on 3 computers now with no problems.

(Apart from when it auto-compacts itself after closing, it can’t delete itself and rename the db1.mdb to the original filename. But that’s a problem for another day!)


PS. I can’t believe I have spent so many days on this problem for it to be fixed like this!!!
Thanks for your help mate! If you’re ever in Perth, Australia, I’ll buy you a pint!
 
Not sure how I helped, does not sound like anything I suggested worked for you. Maybe you just needed someone to vent to. Those are some very strange problems which I have never seen. The treeview control is temperamental. If I remember correctly there is a really weird issue with the key. The key has to be a string and the string has to start with non-numeric values. I remember wasting a couple days chasing that one down. This would not work: "1234", "1234A", but this would work "A1234". So the natural key would be some record ID. I now have the habit of making my key the table name & the record ID.
 
Yeah, I had that problem too. You have to start the key with a letter or it trys to use it as a number or something. Don't really know why, just know to start each one with a letter from now on.

Think you helped by making me put the problem down on paper and pointed me in the right direction. Cheers again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top