By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,504 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,504 IT Pros & Developers. It's quick & easy.

How Do I set '.Rowsource' property on a combo box, using a variable generated at runt

P: 1
Here's my code ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim i As Integer
  3. ' loop through from current year, and add each preceding year for 80 yrs to Combo Box
  4. For i = 0 To 79
  5.     Me.cboYear.RowSource = Year(Date) - i
  6. Next i
  7. End Sub
I hoped this would output the current year at top of list ( i.e. 2012 ) and then the previous 80 years in descending order. It outputs "2012" only ! Any help would be wonderful.
Apr 29 '12 #1
Share this Question
Share on Google+
2 Replies


NeoPa
Expert Mod 15k+
P: 31,186
Your code is repeatedly changing the Record Source of the control. What you need to do is to set the Record Source to a single string which contains all the values you need.
Apr 29 '12 #2

NeoPa
Expert Mod 15k+
P: 31,186
This code might be the sort of thing you're after. Notice what it does and how it goes about doing it :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim intDiff As Integer
  3.     Dim strRecSrc As String
  4.  
  5.     ' loop through from current year, and add each preceding year for 80 yrs to Combo Box
  6.     For intDiff = 0 To 79
  7.         strRecSrc = strRecSrc & ";" & Year(Date) - intDiff
  8.     Next intDiff
  9.     Me.cboYear.RowSource = Mid(strRecSrc, 2)
  10. End Sub
Apr 29 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.