Next Tip    Using The Multi Selection Option In Access List Boxes

The List Boxes in Access 97 have property that allows you to select multiple items in the list box.  You will find this property under the Others tag and it is called Multi Select.  But if you want to use it, you will now have to write vb that loops through a collection rather than simple pointing to the list box itself.  The Access 97 help examples are shown 

The following example prints the value of the bound column for each selected row in a Names list box on a Contacts form. To try this example, create the list box and set its BoundColumn property as desired and its MultiSelect property to Simple or Extended. Switch to Form view, select several rows in the listbox, and run the following code:

Sub BoundData()
    Dim frm As Form, ctl As Control
    Dim varItm As Variant

    Set frm = Forms!Contacts
    Set ctl = frm!Names
    For Each varItm In ctl.ItemsSelected
        Debug.Print ctl.ItemData(varItm)
    Next varItm
End Sub

The next example uses the same list box control, but prints the values of each column for each selected row in the list box, instead of only the values in the bound column.

Sub AllSelectedData()
    Dim frm As Form, ctl As Control
    Dim varItm As Variant, intI As Integer

    Set frm = Forms!Contacts
    Set ctl = frm!Names
    For Each varItm In ctl.ItemsSelected
        For intI = 0 To ctl.ColumnCount - 1
            Debug.Print ctl.Column(intI, varItm)
        Next intI
        Debug.Print
    Next varItm
End Sub

The code that I wrote for a wizard that I am building in Graf-FX is as follows.   This example uses information from 2 columns in a list box.

On Error GoTo hide_unhide_exit
Dim frm As Form, ctl As Control
Dim varItm As Variant

  DoCmd.SetWarnings False

  Set frm = Me
  Set ctl = frm!cboTables
  For Each varItm In ctl.ItemsSelected
    DoCmd.RunSQL "Delete from z_systables where tabname = '" & ctl.ItemData(varItm) & "'"
    DoCmd.RunSQL "insert into z_systables (tabName, tabType, tabIgnore ) SELECT '" &   ctl.ItemData(varItm) & "','" & ctl.Column(1, varItm) & "' ," & hideFlag & ";"
  Next varItm

hide_unhide_exit:
  DoCmd.SetWarnings True

Try out our popular Access shareware Graf-FX  @  http://www.vb123.com/explore

 

Our Tools and Resources

  • RSS & Newsletter Here
    Join our newsfeed or sign up for our informative newsletter on Office Automation, Access and VB topics

  • Smart Access is online 
    The best magazine written about Microsoft Access is now being transferred to the web. There are 400 articles written by a 100 authors in the collection. 

    See the Smart Access 2010 specials here

  • The Workbench
    Find out who has your database open, start the correct version of Access, easy compacting and backups, change startup options, creation versions,  shutdown database

  • Read about the Toolbox
    Sample downloads, library resource kit and searchable help file comprising most of the information at vb123.com.au plus hidden downloads etc.

  • Convert Access to SQL Server  
    Upsize to SQL Server 2005 or 2008, easily repeated conversions, highly accurate SQL query  translation and web form conversion.

 

 

vb123 Professionals


Get Good Help Here

If you need help with a database or Office programming, our Professionals could be the answer because we have worked on many similar solutions



Frontpage Conversions
We have converted vb123.com to Expression Web, contact us if we can help you move to the latest Microsoft web tool.


About The Editor ~ Contact Us
Garry Robinson writes for a number of popular computer magazines, is now a book author and has worked on 100+ Access databases. He is based in Sydney, Australia

Access 2003 Security

MS Access Security

Read More here