setting comvbo box itemselected

jonnydexter

Registered User.
Local time
Today, 22:25
Joined
Aug 31, 2012
Messages
21
hi
after spending almost an hour trawling and testing various methods, can someone please explain how to initilaise the combo box value at runtime.

i have a combo box with 5 items in it and i need it to point to item 5 when the form displays.

should i be using .listindex or not ?

tia
 
i need it to point to item 5 when the form displays
So I assume that you know what the fith item in the list will be. So in the On Open event of the form use:
Me.NameOfComboBox = "Text in list"
or
Me.NameOfComboBox = 44

If the bound column of the combo box is numeric use the second option.
 
i wont know the value at runtime, it will be calculated as the form is opened so i need to read the index and change it to point to the last entry in the list. The list could contain 1, 2, 5 or 20 entries. so i need to populate the combobox in code and then make it show the last item that was added to the list.

i could look at using
me.combobox = me.combobox.listcount - 1

maybe ?

will this show the values i have put in the list rather than the count - 1 though ?

tia

edit - just tried this and it shows the count -1 in the combo not the matching value at that position i nthe list
 
Using:
me.combobox = me.combobox.listcount - 1

will select the last item in the combo box if the "Column Heads" property is set to "Yes". If this property is set to "No" thenyou will need:
me.combobox = me.combobox.listcount
 
still doesnt give me the values i have added but is giving the index in the combobox

this is what i am using where first year is static and set to 2008

Code:
Public Function Populate_cboyear()

For i = year(first_year) To year(Date)
    cboyear.AddItem CStr(i)
Next i

cboyear.SetFocus
Me.cboyear = Me.cboyear.ListCount - 1

End Function
after stepping through the combo is showing the number 4 not the ast date added
 
still doesnt give me the values i have added but is giving the index in the combobox

this is what i am using where first year is static and set to 2008

Code:
Public Function Populate_cboyear()

For i = year(first_year) To year(Date)
    cboyear.AddItem CStr(i)
Next i

cboyear.SetFocus
Me.cboyear = Me.cboyear.ListCount - 1

End Function
after stepping through the combo is showing the number 4 not the ast date added

If you are just trying to make it be the current year as being selected, just use

Me.cboYear = Year(Date)
 
that will achieve the result i need in this case Bob but if the list contained something else that was not covered by a function in VBA how would i go about telling the combo to display the value?

It seems that whatever i have tried to do with this combo, it will not allow me to tell it to show a value from the list i have set,. e.g. how could i get it show
an entry based on another value that could be coming from another routine ?

thanks
 
Try this
Code:
Public Function Populate_cboyear()
Dim i As Integer
For i = Year(first_year) To Year(Date)
    Me.cboyear.AddItem CStr(i)
Next i
cboyear.SetFocus
Me.cboyear = i - 1
End Function
 
ok, i have traced part of this issue down to the bound column setting of the combo box.
but i have differnet results on 2 different combo boxs.
One has been set to display the months of the year which i have populated a design level using row source and row source type.

if i set bound column to 1 and use

Code:
me.cbomonth - month(date)
it displays the numeric month value. i have to switch it to bound column zero to get the month name textually.

however on my cboyear combo if i change it to 0 and use my original code

Code:
Public Function Populate_cboyear()

For i = year(first_year) To year(Date)
    cboyear.AddItem CStr(i)
Next i

Me.cboyear = year(Date)  ' this is the same as for cbomonth but for year so why a different result ?

End Function
it displays nothing until returned to a bound column setting of 1

is there something i am missing in the way the lists are stored here ?

sorry for this confusing thread
 

Users who are viewing this thread

Back
Top Bottom