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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview on a form displaying editable fields when child node clicked

Status
Not open for further replies.

DonnaB

Programmer
Apr 23, 2001
34
0
0
AU
Hi,

I have created a treeview on a form, along with some fields from the database. The treeview is working as required (allowing nodes that are read from db to be opened and closed) however, I would like to be able to click on the lowest level node and display extra data (which is read from db) in the fields on the form.

When the form is started data is already populating the fields on the form, which is not what I want, as they should be blank until one of the lowest children nodes is clicked on.

I've discovered I can populate a new text box when a node is clicked on, but i'm not sure how to limit this so data is only displayed when lowest level node is clicked.

Also, I want to be able to edit the data shown and write the changes to the db.

Hmmm, any suggestions?

Donna.
 
Hi Donna,

First, when you have finished populating the tree, move the record pointer to EOF in your table. This will ensure that you do not initially see any values in the textboxes. Use the following code to move to EOF:

GO BOTTOM
SKIP

Next, you need to write code in the tree's NodeClick event. This event takes, as a parameter, an object reference to the node which has been clicked on.

You can use this object reference to find out if the node is at the lowest level of the tree. For example, you can use the Children property, which says if the node itself has any child nodes:

IF NOT node.Children
* this node is at lowest level.
ENDIF

Another possibility is to store an indication of the level number within the Key property of the node. For example, if you have three levels, say countries, provinces, towns, you might construct the keys of the nodes so that countries begin with C_, provinces with P_ and towns with T_. Then, in the NodeClick, you can do something like this:

IF LEFT(node.Index) = "_T"
* we are at the Town level
ENDIF

I hope this makes sense. If anything needs clarifying, get back to me.

Mike
Mike Lewis
Edinburgh, Scotland
 
Thanks Mike. :-D

I couldn't work out how to use GO BOTTOM and SKIP, so I have just hidden each field and I make them visible only when a child node is clicked.

I now have a treeview that, when the lowest level child is clicked on, displays some editable fields. However, I have defined each of these fields as having a Control Source, which is a column from the database. For this reason, only the data from the first record is displayed, but it can be edited and automatically saved in the database.

What is the best way for me to make these fields show the data associated with the node clicked on, and allow changes made to be saved in the database?

Thanks again,
Donna
 
Hi Donna,

The way I usually do this is to store the primary key (such as the customer ID) in the node's Key property.

For example, if the node represent a customer, the node's key might be something like C_ALFKI, where ALFKI is the customer ID of the one of my customers.

Then, in the NodeClick, I do something like this:

IF LEFT(node.Key,2) = "C_"
* this is a customer
lcID = SUBSTR(node.Key,3) && extract the ID
SELECT Customer
SEEK lcID
THISFORM.Refersh
ENDIF

As you have the textboxes' ControlSource set to the fields in the underlying table, the above code is all you need to display the data.

(By the way, I made a mistake in my earlier post; I referred to node.Index rather than node.Key. I expect you realised that.)

Mike
Mike Lewis
Edinburgh, Scotland
 
Hi Mike,

Thanks for all your help, but I think we're working on different times (I'm in Australia)! The current status of my database is that I've used the method you recommended (i.e. testing the left chars of the node.key) to test which node is clicked and the corresponding data fields appear on the form. :-D

If left(Node.Key, 3) = "CH_" Then
Me.RecordSource = "SELECT * FROM Report " _
& "WHERE Name = '" _
& Right(Node.Key, Len(Node.Key) - 3) & "'"
End If

My last final touch is to include code so a popup menu appears when a node is right-clicked on (which I have successfully tested for). I hope this to be similar to the popup menu in Explorer. I'm just not sure how to popup a menu that just reads "New...", do I use MsgBox?

Thanks again, [2thumbsup]
Donna.
 
Donna,

Not sure how your being in Australia would affect the code <g>, but I'm glad you've got it working.

To create a popup menu, you need to go to the menu designer. You will be prompted to create either a menu or a shortcut menu. Choose the shortcut option. Create the menu and generate it.

Then, in your right-click event, execute:
DO MyMenu.MPR

The MPR extension is important, otherwise it would try to execute a PRG.

Give it a try.

Mike
Mike Lewis
Edinburgh, Scotland
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top