Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

TreeView ImageCombo Drop-Down Access Menu

Introduction.

In this Session of TreeView Control Tutorial we will learn programming the ImageComboBox Control.  We will create an MS-Access Project Drop-down Menu, with ImageCombo Control.  There is a second ImagecomboBox Control for displaying the Images and their Key Values from the ImageList Control.  For both of these ImageComboBox Controls the list of Images are taken from a common ImageList Control.  The Images were manually uploaded into the ImageList Control from the Computer, in an earlier session of this Tutorial Series.

Following are the Tree View Control Tutorial Sessions we have covered so far:

The TreeView Control Tutorial Session Links.

  1. Microsoft TreeView Control Tutorial
  2. Creating Access Menu with TreeView Control
  3. Assigning Images to TreeView Control
  4. Assigning Images to TreeView Control-2
  5. TreeView Control Check-Mark Add Delete Nodes

The Demo View of Form with Both ComboBox Expanded.

The completed MS-Access Project Drop-Down Menu Image is given below:

The Design View Image of the above Form is given below:

The Drop-Down ImageComboBox Source Data.

The new MS-Access Project Drop-Down Menu Source Data is taken from our earlier Access Menu Project.

Download the Demo database now, if you have not done earlier, from the 4th item Link given above.  If you do, then you have all the required data Objects to continue with the current Session.

There are three Tables: Categories, Products and the Menu table.  There are two forms to display the Categories and Products data and a data filter parameter Form for Report.

We have two more Forms: frmMenu and frmMenu2 we have used for our earlier Tutorial Sessions.

You will find two Reports to Preview the Categories and Products Data Items.

Two Macros for displaying some simple Messages.  The macros can be used for sequencing Action-Queries for data Processing for complicated Reports.  These actions are performed by selecting Options from TreeView Control Project Menu in earlier Tutorial Session.

We need all these objects here also because we are going to create a new Drop-Down Menu using the ImageComboBox control. We must be able to open all these objects by selecting the options in the New Drop-Down Control Menu, exactly the same way as we did on frmMenu2 Form using the TreeView Control, in this same Database

The Menu table image is given below for your reference.

Preparing for the Design of the Drop-Down Menu Form.

Check the Design View of the Menu Form above, in there we have two Menu related Controls.   One ImageList Control and one ImageComboBox Control.  One more ImageComboBox Control is placed to the right side of the Form to display the ImageList’s Images.

  1. Make a Copy of the ImageList Control from frmMenu2 Form and Paste it on a New Form, say frmMenu3Combo

    This Imagelist Control have Images we have uploaded mannually from the Computer in our earlier Tutorial Session.  You may open it’s Property Sheet and check the Images and their Key Names. Right-Click on the ImageList Control, highlight ImageListCtl Object and select Properties Option.

  2. The ImageComboBox Control placed at the left side on the Form's Design View, is for our Project Drop-Down Menu, with the name: imgCombo1.  Find Microsoft ImageComboBox Control from the ActiveX Controls group and place it on the Form.  Change the Name of the control to imgCombo1.

  3. Create another ImageComboBox  Control to the right side, with the name imgCombo2. The second ImagecomboBox we will use it for displaying the Images and their Key Names, from the ImageList0 Control, as a drop-down List.

  4. Add a Label control above the second ImageComboBox control and change it’s Caption to Image List.

The Images Listing in Image-Combo-Box Control.

First, we will work with the second imgCombo2 Control and display the List of Images from the ImageList Control.  Once you are familiar with the Code you will understand the Drop-down Menu Creation procedure very easily.

We have divided the frmMenu3Combo Form Module VBA Code into two parts.  Let us take the first part and see what we have in there.

Briefly, in the Global Declaration Area  the main Object variables are declared. The Form_Load() Event Procedure initializes the ImageList Control on the Form to it’s object Variable objimgList and calls the cboImageList() Subroutine to Add Images from the ImageList Control to the second ImageComboBox Control.  Let us take a closer look at the Code.

The first-part VBA Code, with the Form_Load() and cboImageList() Subroutines are given below:

Dim imgcombo1 As MSComctlLib.ImageCombo
Dim imgCombo2 As MSComctlLib.ImageCombo
Dim objimgList As MSComctlLib.ImageList
Const KeyPrfx As String = "X"

Private Sub Form_Load()

Set objimgList = Me.ImageList0.Object

cboImageList 'load imagelist-combo

'CreateMenu 'Create Drop-Down Menu
 
End Sub

Private Sub cboImageList()
Dim j As Integer
Dim strText As String

Set imgCombo2 = Me.ImageCombo2.Object
imgCombo2.ImageList = objimgList

For j = 1 To objimgList.ListImages.Count
    strText = objimgList.ListImages(j).Key
    imgCombo2.ComboItems.Add , , strText,j,,j
Next
    imgCombo2.ComboItems(1).Selected = True
End Sub

VBA Code Review.

On the Global Declaration area we have declared two ImageComboBox Controls, imgCombo1 for the Project Menu and imgCombo2 for displaying images from the ImageList Control.  The objimgList Variable declared for the ImageList Control on the Form.  The Keyprfx variable with the character X declared as a Constant.

Within the Form_Load() Event Procedure the objimgList is initialized with the ImageList Control on the Form, with the statement: Set objimgList = Me.ImageList0.Object.  Now, all the preloaded images in the ImageList Control are available to access through the objimgList Object.

The next statement calls the Sub-routine cboImageList() to add all the Images to the ImgCombo1 Control.

The CreateMenu() Subroutine call is commented out for the time being.

In the cboImageList()  Subroutine two Variables are declared. 

Next,  the statement Set imgCombo2 = Me.ImageCombo2.Object  assigns the second ImagecomboBox on the Form to the object variable imgCombo2. 

Like the TreeView Control the imgCombo2 has an ImageList Property, to pass the ImageList Control’s Reference to the ImageComboBox control, in order to access the ImageList’s Properties. The next statement: imgCombo2.ImageList = objimgList does that.

Next, the For . . . Next Loop runs for the number of Images in the ImageList Control. 

The first ImageList item’s Key Value ('form_close') is saved in strText Variable.  Here, we have taken the Key value of Image List control as Text or as Description of ImageCombo Image, because that is the only Text available.  The Tag Property is empty and we have other uses with this property when we work with the drop-down menu.

The next statement is the imporant one that we have to look closely, the Add method of ImageComboBox control.  The Syntax of the statement is as given below:

imgCombo2.ComboItems.Add [Index],[Key],[Text],[Image],[SelImage],[Indentation]

All the Parameters of Add() Method are Optional.  For our first Test Run of this control we will use values for [Text], [Image] and [Indentation] onlyAfter viewing the result of the first test run of image list view, we will not use the [Indentation] parameter value on this Image Combo control.

Note: But, keep in mind that we will need the Indentation Property value for the Drop-Down Menu to make the menu items to look like Root-Node and Child-Node on the TreeView Control.  We will be using the [Key] Parameter also  (for both Key and Text parameters) to access a specific menu item’s Tag Property value.  

Image List with incrementing Indentation Param Setting.

The first Test Run image list in ImageCombo2 will look like the image given below, after applying incremental values for indentation.:

The effect of the Indentation is clear from the above trial run image.  Each item is moved to the right, one space greater than the previous one.  We can make use of this feature to position our Project Menu Items to look like Root-Node and Child-Node.

After the strText Value (‘form_close’) the first Variable j refers to the ImageList’s Index Number, the [SelImage] parameter we have skipped and the next Value in j is used for Indentation of each list item when placed in the combobox list.  After the first test run and after viewing the result you may remove all parameters after the Image Index value.

The next statement: imgCombo2.ComboItems(1).Selected = True selects the first item in the ComboBox.  When you select an Item from the ImageCombo control through Code the Change() Event fires, but it is not when you select an Item directly on the Form. The Update() Event ignores in both manual and through Code.

Save the Form frmMenu3Combo and Open it in Normal View.  Expand the second Image List ComboBox control and view the result.  Remove the commas and the variable j at the end, after the first variable j, retained for the Image List Index number.

The VBA Code of Project Drop-down Menu.

Now, we will proceed with the second part of the Form Module VBA Code to learn as how to create the Access Drop-Down Menu and see how to open Access Forms, Reports and Macros by selection of ComboBox Control Items.

The second part of the VBA Code, consist of the CreateMenu() Subroutine and ImageCombo1_Click() Event Procedure, is given below:

Private Sub CreateMenu()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strKey As String
Dim strText As String
Dim typ As Integer

Set imgcombo1 = Me.ImageCombo1.Object
imgcombo1.ImageList = objimgList

strSQL = "SELECT ID, Desc, PID, Type,Macro,Form,Report FROM Menu;"

Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

Do While Not rst.EOF And Not rst.BOF
    If Len(Trim(Nz(rst!PID, ""))) = 0 Then
        strKey = KeyPrfx & CStr(rst!ID)
        strText = rst!Desc
        imgcombo1.ComboItems.Add , strKey, strText, 1, 2, 1 ' image index 1,2([image],[selectedimage])
        'imgcombo1.ComboItems.Add , strKey, strText, "folder_close", "folder_open", 1
    Else
        strKey = KeyPrfx & CStr(rst!ID)
        strText = rst!Desc
        imgcombo1.ComboItems.Add , strKey, strText, 4, 5, 4 'last param is spacing
        'imgcombo1.ComboItems.Add , strKey, strText, "left_arrow", "right_arrow", 4
     
        'Check for the presense of Type Code
        If Nz(rst!Type, 0) > 0 Then
                typ = rst!Type
                With imgcombo1.ComboItems
            Select Case typ
                'save type Code & Form/Report/Macro Name in Tag Property
                Case 1
                    .Item(strKey).Tag = typ & rst!Form
                Case 2
                    .Item(strKey).Tag = typ & rst!Report
                Case 3
                    .Item(strKey).Tag = typ & rst!Macro
            End Select
                End With
        End If
        
    End If
    rst.MoveNext
Loop
rst.Close
imgcombo1.ComboItems.Item(1).Selected = True
End Sub

Private Sub ImageCombo1_Click()
Dim strObject As String
Dim strTag As String
Dim typ As Integer

strTag = ImageCombo1.SelectedItem.Tag
typ = Val(strTag)
strObject = Mid(strTag, 2)

Select Case typ
    Case 1
        DoCmd.OpenForm strObject, acNormal
    Case 2
        DoCmd.OpenReport strObject, acViewPreview
    Case 3
        DoCmd.RunMacro strObject
End Select

End Sub

I think, before starting with the VBA Code you should take a look at the Table Image  (third Image from the top of this page), if you have not done in the earlier Session of Access Project Menu creation. 

The ID field have Unique ID Value and is an AutoNumber Field. 

Second Field Desc have Object Type Group Names (Forms, Reports and Macros) and actual Object Names of Form, Report and Macro.

The PID (Parent ID) field value is empty for Object Group Names: Forms, Reports and Macros.  This empty PID value items we will position at the left position in the ImageComboBox Drop-Down Menu, with one character indentation space and other items will be moved forward with four character space indentation.  With these positioning of items they will look like the Root-Level and Child-Level Nodes in a TreeView Control, but we will miss the connecting Tree Lines.

Check the image in the ImageComboBox selected item control, the image is positioned at the left-most position.  The Group Item in the next line is positioned after one character space at the left side and other Group items are also appearing in the same position.  The Child member items under each Group  are positioned after four character spaces.

The PID field value is checked and if found empty then we assume that it is a group Name otherwise it is an Access Object Name that we need to open, when the User Clicks on it, and position them as Child member of the Group. The actual Key value in the PID field is not important here.  But in either case we need it here.  But, we can use the Type field value for this purpose instead.

Next, the Type field contains the object type Code: 1 – Form Name, 2 – Report Name and 3 – Macro Name.  Next three fields: Form, Report and Macro have the actual Object Names based on their respective Codes in the Type field.  I have used three different fields for clarity, they all can be placed in one column as well.

The Type Code and Object Name Pair (say 2rptCategories) will be saved in the ImageComboBox’s Tag Property.

The CreateMenu() Subroutine.

Now, on to the VBA Code of CreateMenu() Subroutine.

The Database and other Variables are declared at the beginning.

The imgcombo1 Object Variable is initialized with Me.ImageCombo1.Object on the Form.

Next, the imgCombo1.ImageList Property is loaded with the ImageList objimgList Reference, so that we will be able to access the ImageList’s Index Number and Key values directly.

Next, the Menu Table recordset is open with the SQL String. 

The PID field value is checked, if it is empty then it is Object Group Name, the ID value is prefixed with the Constant X and saved in strKey Variable.  The field Desc Value is saved in strText Variable.

Next statement Calls the Add() Method of ImageComboBox Control and the first item is added to the Drop-Down Menu.

imgcombo1.ComboItems.Add , strKey, strText, 1, 2, 1

The first parameter value Index number is omited, but it will be created automatically.  The strKey Variable consists of ID Field value 1 with prefix constant X (X1) as Key parameter.  The strText contains the Desc Field Value.  The next Value 1 is the ImageList’s first Image’s (Key value folder_close) Index Value.  If you prefer the 'folder_close' Key value within quotes you may do so. Next Value 2 is the second Image’s (‘folder_open’) Index Value or you may use 'folder_open' in quotes and the last parameter 1 is for indentation.

Note: To confirm the Index order of Images in the ImageList Control check the Image Listing Order in the ImageCombo2 Display we have created earlier.  We could have prefixed the Index value to the Key value with one space to add that number also to the Key value to look like [image]  1 form_close. [image] 2 form_open and so on . . . This I will leave to you as an exercise for yourself.

Next, if the PID field value is non-zero then the actual Menu Option to be Added under the Else ClauseHere, we have added another ImageCombo item as we did earlier.  For [Image] and [SelImage] params we have taken the ImageList Item Index value 4 and 5.  Indentation param value is 4 character spaces.

In the ImageCombo Item’s Add() Method, under the Else Clause, we need to save the Access Object Name (frmData Entry) along with the Type Code 1 in the Tag (ImageCombo1.ComboItems.Item(strKey).Tag) Property.  When the User selects this item from the Drop-Down ComboBox Menu, the Click() Event fires, the Tag Property  value is extracted, the Type code is checked, if the type code is 1 (Form) then the form-name in Tag Property (frmData Entry) is open with DoCmd.OpenForm Command.

This way all the Menu Table records are added to the Image ComboBox Control.

The statement imgcombo1.ComboItems.Item(1).Selected = True selects the first item in the Image ComboBox Control.  On this Code execution time the Change() Event fires, but not when an item selected by clicking on it on the Form View.

NB: Before opening the form for the Drop-down Menu Trial Run, please remove the Comment Symbol from the ‘CreateMenu call line in Form_Load() Event Procedure.  We have commented out this line temporarily, during trial runs of Images display from ImageList Control in ImageCombo2 Control.

The ImageCombo1_Click() Event fires when the User selects an item from the Image ComboBox Control.  The selected item’s Tag Property Value is checked for the Type Code & Object Name and opens it with Docmd.Objecttype ObjectName.

The Demo Database ProjectMenuV221.accdb in .zip format is attached for Downloading.

DICTIONARY OBJECT

  1. Dictionary Objects Basics
  2. Dictionary Object Basics-2
  3. Sorting Dictionary Object Keys and Items
  4. Display Records from Dictionary
  5. Add Class Objects as Dictionary Items
  6. Update Class Object Dictionary Item
Share:

No comments:

Post a Comment

Comments subject to moderation before publishing.

MS-Access Tips on your Finger-Tip

  • Download Android App 'MSA Guru' Version of LEARN MS-ACCESS TIPS AND TRICKS from Google Play Store.

Want to Post Free Ads on the Web


Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object Graph Charts Query VBA msaccessQuery Calculation Event List Boxes Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference TreeView Control ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button ImageList Control Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join ListView Control Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code