Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    28

    Unanswered: listindex and setfocus problem with combobox

    Hey guys, I have a form where a form has a combobox whose rowsource is a field from a table. After pressing a button, this rowsource is changed, along with some other things, and then i use setfocus and then try to use the listindex property to set the value to the first on the list. This calls the error of "using the listindex property incorrectly". Usually this is from the control not having the focus.

    The wierd part is if i debug, change nothing, and continue, the code works.

    Any ideas on what could be causing this? Should I put a delay or something?

  2. #2
    Join Date
    Jun 2008
    Posts
    28
    btw, I placed a break at the GotFocus event for the combobox and it is calling the event before writing the listindex property.... so the listindex property should be writable... is there any other property for that control that could limit the writing capability of listindex that would somehow change once i went into debug?

    this is very annoying...

  3. #3
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Help files are great!

    Copied and pasted from the help files:

    ListIndex Property
    See AlsoApplies ToExampleSpecificsYou can use the ListIndex property to determine which item is selected in a list box or combo box. Read-only Long.
    Just set the combo box to the first item directly... ie:

    Forms("Formname")!ComboName = Item1
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  4. #4
    Join Date
    Jun 2008
    Posts
    28
    well the problem is that i wont know what the first item is in the list. second, i am almost positive the listindex property is writable if the focus is set, even though the helpfile may not specify that... I think i have thought of a workaround, but it will require me to change my forms a bit

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Can you post the actual code? I have given it a quick try here using A2003 and it works fine
    Code:
    Private Sub Command0_Click()
    
        Me.List1.RowSource = "SELECT firstname FROM people"
        
        Me.List1.Requery
        
        Me.List1.SetFocus
        Me.List1.ListIndex = 0
    
    End Sub
    EDIT: Note that I used the click event of the button you described earlier: I'm missing a trick, aren't I?
    George
    Home | Blog

  6. #6
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Can't you just use the "TOP 1" of the SQL from the combo box to find the first item?

    Personally I'd dodge trying to set the ListIndex property if the help says it's read-only.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  7. #7
    Join Date
    Jun 2008
    Posts
    28
    to StarTrekker: Yeah, I hadn't thought of that... however, now I have just added an invisible textbox bound to the field that i wanted and then i set the combobox's value directly... this works pretty well because i am guaranteed it is the current record.

    I'll post the code... I am almost certain there is nothing wrong with it (especially since it works after i press continue in the debug and changing nothing)... I think it might be some bug im hitting in VBA, because i'm getting the impression that VBA has a couple...


    Code:
    Sub InputChange()
    Select Case Form_Analysis.Tab.Value
    Case 0
    With Form_tslotForm
    Select Case Form_Analysis.tshFrame.Value
    Case 1 'existing part
    .partno.SetFocus
    .calcbtn.Visible = False
    .RecordSource = "tshInfo"
    .AllowAdditions = False
    .DataEntry = False
    .partno.RowSource = "SELECT [tslot].[partno] FROM tslot;"
    .partno.RowSourceType = "Table/Query"
    .partno.ControlSource = ""
    .tshQtyCombo.Locked = False
    .Label62.ForeColor = 9605778
    .fgroup.ForeColor = 9605778
    .partno.SetFocus
    .partno.ListIndex = 0
    tsheditnow = False


    That is pretty much it;... thats the only case it is going to and the 2nd and 3rd to last lines are what is in question...

  8. #8
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    have a go with
    me.mylist = me.mylist.column(0,X)
    where X is the .boundcolumn of the list (0=1st, 1=2nd etc)

    izy
    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •