• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    Welcome to the HEXUS.community discussion forums forums.

    You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 26-10-2009, 05:10 PM   #1 (permalink)
    Registered+
     
    Join Date: Feb 2009
    Posts: 52
    Thanks: 1
    Thanked 2 Times in 2 Posts
    Leggie's system
    Getting values from a VBA Listbox

    Hi guys,

    In an Access 2007 database, I have a form with a ListBox that grabs the values from one of my tables. I want, when I click on a ListBox item, the value I have selected in the list to appear in a TextBox.

    I can't seem to find the right code to do. I've tried many combinations, such as something like the following:

    Code:
    txtS.Value = lstS.ItemData(lstS.ItemsSelected.Item(0))
    
    I've a feeling it must be easy and I can't see the wood for the trees. Should be a simple query, but I've waded through the VBA API and after working in Java it all seems much more vague and hazy.

    Thanks for your help,

    Leggie
    Leggie is offline   Reply With Quote
    Old 26-10-2009, 07:48 PM   #2 (permalink)
    Member
     
    Join Date: May 2009
    Location: Norfolk
    Posts: 144
    Thanks: 2
    Thanked 8 Times in 8 Posts
    pipTheGeek's system
    Re: Getting values from a VBA Listbox

    Sorry, just tried finding the answer for you. Then I realised/remembered how much I HATE Access 2007. I'm going out instead. I had ended up with something similar to what you had posted, but my event handler appeared to be the wrong one becuase I was getting an error becuase the list had no items selected! If you are still stuck tomorrow then I'll sort the code out to do it in Access 2 instead.

    (Access 2 is the version of Access that I used during the Access developer stage of my programming career.)

    pipTheGeek is offline   Reply With Quote
    Old 27-10-2009, 01:46 PM   #3 (permalink)
    Registered+
     
    Join Date: Feb 2009
    Posts: 52
    Thanks: 1
    Thanked 2 Times in 2 Posts
    Leggie's system
    Re: Getting values from a VBA Listbox

    I've had no more luck, I'm afraid, so any help would be gratefully received, thanks.

    I'm also somewhat glad to see that it isn't just me that has trouble with Access!

    Thanks,

    Leggie
    Leggie is offline   Reply With Quote
    Old 27-10-2009, 08:56 PM   #4 (permalink)
    Large Member
     
    Join Date: Apr 2004
    Posts: 3,368
    Thanks: 25
    Thanked 27 Times in 22 Posts
    Re: Getting values from a VBA Listbox

    I've no idea, but an educated guess would be:

    Code:
    txtS.Value = lstS.ItemsSelected.Item(0).Value
    

    To err is human. To really foul things up ... you need a computer.
    yamangman is offline   Reply With Quote
    Old 28-10-2009, 09:17 PM   #5 (permalink)
    Three As fanboy...
     
    Join Date: Jan 2009
    Location: Manchester
    Posts: 1,600
    Thanks: 60
    Thanked 119 Times in 117 Posts
    scaryjim's system
    Re: Getting values from a VBA Listbox

    Good lord the Access 2007 help is appalling, isn't it. I've never been able to find anything in office help since 2002...

    Anyway, the code you are looking for is
    Code:
    txtS.Value = lstS.Value
    
    the value of the listbox is the value of the bound column in the currently selected row (at the risk of guessing, if you have a multiselect list with more than 1 item selected you probably get a comma-separated list of the selected items). If you have a multi-column listbox and want to use a value other than the bound column (although why you'd do that is beyond me ) you'd need
    Code:
    txtS.Value = lstS.List(lstS.ListIndex, columnindex)
    
    where columnindex is the zero-based index of the column you're interested in. That example only works for non-multiselect lists though: for multiselects you'd need something like
    Code:
    Dim result as String, i as Integer
    
    For i = 0 to lstS.ListCount - 1
        If lstS.Selected(i) result = result & lstS.List(i, columnindex) & ", "
    Next i
    
    txtS.Value = result
    
    But that's probably enough showing off from me for tonight

    //
    // TODO: Add witty signature here
    //
    scaryjim is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Tags
    vba


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On


    Similar Threads
    Thread Thread Starter Forum Replies Last Post
    CS21A550 default deflection values needed. tusharfrompune Help - technical & advisory 0 23-07-2009 05:19 PM
    Any good VBA books? stroberaver Software and web development 8 21-07-2009 08:35 AM
    Ram CAS latency values help please! mathewdbarlow HEXUS.hardware 2 26-01-2007 01:25 PM
    British Core Values? BEANFro Elite General discussion 48 17-05-2006 11:35 AM
    multi boot options robbiehall Operating systems & applications 4 27-10-2003 07:55 PM



    All times are GMT. The time now is 12:20 AM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned.
    HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    For more information please read HEXUS Limited's terms, conditions and privacy policy.

    Hosted Exchange

    Powered by vBulletin® Version 3.8.4
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.3.2
    © Copyright 2009 HEXUS® Limited. All rights reserved. Unauthorised reproduction strictly prohibited.