Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Imprinted Promotions
Boat Donations
Corporate Awards
Computer Deals
KVM Switch over IP
GPS Devices
PDA Phones & Cases
Compare Prices
Calling Cards
Server Racks
Compare Prices
Disney World Tickets
GPS
Career Education




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Guide to Oracle 11g and Database Migration
Oracle Database 11g includes more features for self-management and automation, which makes it easier for customers to cost-effectively manage their data. Download this Internet.com eBook for an overview of some of the new features in 11g and for an overview of the issues you need to consider as you prepare for a database migration. »
Innovate Faster with Oracle Database 11g
Read this in-depth analysis of 56 customers, which shows significant differences between the value software vendors Oracle and SAP deliver to midsize companies. »
Oracle Business Intelligence Standard Edition One
Find out how Newport Beach, CA-based Mobilitie is shaking up the telecom industry by leveraging technology to provide an entirely different financial model for deploying, upgrading, and owning wireless and wireline network assets. »
Business Intelligence and Enterprise Performance Management: Trends for Emerging Businesses
Quickly implementing an ERP software solution can be of tremendous benefit; however, companies often struggle to balance the benefits of reducing implementation time and cost with the risks of an accelerated deployment. Read this white paper to learn about easy-to-follow best practices for achieving a successful accelerated implementation. »
Making the Case for Oracle Database on Windows
Users benefit as vendors reduce enterprise complexity and deliver integration. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MS Access
March 21, 2008
Access TreeView-ListView Basics
By Danny Lesandrini

Ok, this truly is a 'basics' article. I've never been fond of the TreeView & ListView because the library file that exposes these controls, MSComCtl.ocx, always seemed to behave a little quirky. It was as if I could never be sure which version of the controls any given user would have on their computer and while the controls worked fine for me, users constantly experienced issues. So, I gave up on them ... but maybe too soon.

Recently I've been working with the TreeView & ListView controls on some utilities we use in-house, and the problems I remember from my Access 97 days have not manifest themselves. I'm sure I'll get email from developers who never encountered problems with these controls in Access 97 applications, but alas, that was my experience. But all that's water under the bridge, and I am finally, after all these years, embracing this family of controls.

However, as a newcomer to the TreeView & ListView control, I have no depth of wisdom to impart. There are resources a plenty on the web that describe advanced techniques. This article will focus on getting started. Click here to download the sample code.

Reference The Control

The first step is critical. You need to inform your Access application that you plan to use these controls, and point it to the Microsoft Library wherein they are contained. On all the Access installs where I develop, that ends up being in the Windows\System32 directory:

 C:\Windows\System32\MSComCtl.ocx 

To add the reference, you need to open any VBA code module (the keystroke Ctl+G will get you there), and select References from the Tools menu. With most libraries, you simply need to scan the list for the one you want. You're looking for the Microsoft Windows Common Controls library, but if your install is like mine, you won't find it in the list. You're going to have to browse to it, thus the need for the path given above. The process is illustrated in the screen shot below. Find the file, click Open and then click OK on the References dialog, and we're ready to code.

Build the Form

The first step, and not a trivial one, is to get the control(s) on your form. There are two ways to do this. You can click on the "More Controls" button on the ToolBox toolbar, which will pop up a scrollable list of available ActiveX controls. Scan the list for Microsoft TreeView Control or Microsoft ListView Control and select it.

Alternatively, you can choose ActiveX Control from the Insert menu and work with a smaller, more manageable pick list which gives you feedback about the control you are about to plunk on your form. Either way, you end up with a special object that has, as we shall see, special properties.

This new control (TreeView shown in the screen shot) exposes a special properties dialog box, in addition to the standard properties that Access exposes for all controls, such as Height & Width, Top & Left, etc. To expose these special properties, right-click the control and choose TreeCtl Object >> Properties from the menu. This opens the special properties dialog shown in the screen shot below.

Setting these properties changes the way the control appears and behaves. The ListView control exposes additional tabs to control things like the widths for column headers, sorting and the like. I had a hard time finding help, as it did not seem to be installed. A search of the newsgroups revealed the files I needed, and where to get them, but even then, it didn't seem to run on my machine. Here's a summary of what I found. The version 5 files are included in the download, but here's a site where you can download the latest files, if you can get them to work on your computer:

    http://latium.dpi.ufv.br/downloads/C++IntroductoryEdition/VCB600ENU1/COMMON/HELP/?C=N;O=D
      Ver 6 ... CMCTL198.CHI
      Ver 6 ... CMCTL198.CHM
      Ver 5 ... comctl1.hlp
      Ver 5 ... comctl1.cnt

There's so much more that could be written here ... and so much more has. Remember, this article is just to get you started. There are many examples on the web, and MSDN even has some helpful articles. For now, having placed the controls on a form, let's look at one example of using these controls.

The Code

Below is the entire code listing for the demo application. The only trick to it is the adding of what are called 'Nodes'. Node keys must be unique, and from my experience, must be perceived by the control as being non-numeric. Accordingly, I've attached the bar character (|) to the ID numbers to ensure they appear to be text values.

The other thing to note in the code is how string values are used for comparison to determine when it's time to create a new node. As we begin looping through the recordset, the previous value for strPName (publisher name) is compared with the current recordset value. If it's different, a new node is created and the variables are updated. The same process is followed for the Store Node and the Author Node. After that, it's just looping.

You won't find any special events for the control, but they do exist. Accordingly, you simply have to code for them, as with this event procedure:

 Private Sub tvwMyTreeView_NodeClick(ByVal Node As Object) 

The same is true for the ItemCheck event of the ListView control. However, the ListView control differs from the TreeView in that there are no nodes ... just items, which are added through the ListItems.Add method. The best way to learn about how this works is to download the demo app, and step through the code. That's pretty much what I had to do my first time.

Private Sub LoadMyTreeView()
    On Error GoTo Err_Handler
    Dim strSQL As String
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    
    Dim nod As Node
    Dim iNode As Integer
    Dim strPName As String
    Dim strPKey As String
    Dim strSName As String
    Dim strSKey As String
    Dim strAName As String
    Dim strAKey As String
    
    
    ' Clear the treeview nodes
    tvwMyTreeView.Nodes.Clear
    Set dbs = CurrentDb
    strSQL = "SELECT pub_name, stor_name, au_name, pub_id, stor_id, au_id " & _
             "FROM qryPublisherStoreAuthor ORDER BY pub_name, stor_name;"
    Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
    
    If Not rst.BOF Then rst.MoveFirst
    Do Until rst.EOF
         ' Add Publisher node
        If strPName <> rst!pub_name Then
            strPKey = rst!pub_id & "|"
            strPName = rst!pub_name
            Set nod = tvwMyTreeView.Nodes.Add(, , strPKey, strPName, "publisher_open", "publisher_closed")
            
            strSName = ""
        End If
        
        ' Add Store node
        If strSName <> rst!stor_name Then
            strSKey = strPKey & rst!stor_id & "|"
            strSName = rst!stor_name
            Set nod = tvwMyTreeView.Nodes.Add(strPKey, tvwChild, strSKey, strSName, "store", "store")
            nod.Tag = rst!stor_id
            
            strAName = ""
        End If
        
        ' Add Author node
        If strAName <> rst!au_name Then
            strAKey = strSKey & rst!au_id & "|"
            strAName = rst!au_name
            Set nod = tvwMyTreeView.Nodes.Add(strSKey, tvwChild, strAKey, strAName, "author", "author")
            
            nod.Tag = rst!au_id
        End If
        
        rst.MoveNext
    Loop
    
    ' Initialize ListView to empty recordset.
    LoadMyListView ("")
    
Exit_Here:
    Set dbs = Nothing
    Set rst = Nothing
    Exit Sub
Err_Handler:
    MsgBox Err.Description, vbCritical, "ERROR"
    Resume Next
End Sub
Private Sub tvwMyTreeView_NodeClick(ByVal Node As Object)
    On Error GoTo Err_Handler
        
    Dim lngStor_id As Long
    Dim strAu_Id As String
    
    ' Grab the stor_id from the node's tag property
    ' If null, set to zero.
    strAu_Id = Nz(Node.Tag, 0)
    
    ' Given we have a non-zero ID, load the listview control
    If strAu_Id <> "" Then
        Call LoadMyListView(strAu_Id)
    End If
    
Exit_Here:
    Exit Sub
Err_Handler:
    MsgBox Err.Description, vbCritical, "ERROR"
    Resume Next
End Sub
Private Sub LoadMyListView(ByVal au_id As String)
    On Error GoTo Err_Handler
    Dim strSQL As String
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim lstItem As ListItem
    Dim strItem As String
    Dim strKey As String
    Dim intCount As Integer
    
    
    ' Remove all existing items
    While Me!lvwMyListView.ListItems.Count > 0
        Me!lvwMyListView.ListItems.Remove (1)
    Wend
    
    'open recordset of filtered Title/Authors
    Set dbs = CurrentDb
    strSQL = "SELECT title, title_id FROM qryTitleAuthor WHERE [au_id]='" & au_id & "'"
    Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
    
    'add each entry in the rs to the listview
    If Not rst.BOF Then rst.MoveFirst
    Do Until rst.EOF
        strItem = rst!title
        strKey = "ID=" & rst!title_id
        
        Set lstItem = Me!lvwMyListView.ListItems.Add(, strKey, strItem)
        
        intCount = intCount + 1
        rst.MoveNext
    Loop
    
    If intCount > 0 Then Me!lvwMyListView.ListItems(1).Selected = True
    
Exit_Here:
    Set dbs = Nothing
    Set rst = Nothing
    Exit Sub
Err_Handler:
    MsgBox Err.Description, vbCritical, "ERROR"
    Resume Next
End Sub
Private Sub lvwMyListView_ItemCheck(ByVal Item As Object)
    On Error GoTo Err_Handler
    Dim strSQL As String
    Dim iCurr As Integer
    Dim strKey As String
    Dim lngKeyID As Long
    For iCurr = 1 To Me!lvwMyListView.ListItems.Count
        If Me!lvwMyListView.ListItems(iCurr) = Item Then
            strKey = Me!lvwMyListView.ListItems(iCurr).Key
            MsgBox strKey, vbInformation, "Note"
        End If
    Next
Exit_Here:
    Exit Sub
Err_Handler:
    MsgBox Err.Description, vbCritical, "ERROR"
    Resume Next
End Sub

Barely scratched the Surface

There's a lot to these controls, way more than what's been described here. Below are a couple of MSDN references, and there are more to be had. The newsgroups were a great source for additional code samples and for suggestions on how to overcome errors in describing unique Node Keys, which seems to be the most common pitfall of the TreeView control.

Fill a TreeView Recursively
TreeView Drag and Drop


No, I'll never be a Common Control guru, but such developers exist. I work with one such expert, and he has shown me an app that programmatically changes icons, column headers, sort orders and much, much more. I'm in awe of what can be accomplished by the TreeView and ListView controls. It may be just the tool you need to finish your application, but be advised, the learning curve is anything but trivial.

» See All Articles by Columnist Danny J. Lesandrini

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS Access Archives

Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers. Sponsored by HP, Citrix, and Intel.
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation


Latest Forum Threads
MS Access Forum
Topic By Replies Updated
Export a report into excel Irina_5220 2 May 9th, 08:48 AM
Table Property Question barlowr70 0 May 6th, 10:51 AM
Compile MS Access Database samson 1 May 1st, 03:28 AM
How to connect MS-Access with c++ rockys111 0 April 30th, 01:36 AM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES