Results 1 to 7 of 7

Thread: Access 2000 AddItem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    21

    Access 2000 AddItem

    Hi,

    I developed a form in Access 2003 which has a combo box in it. The combo box displays all the years since 1900 until the current year. I created a function called PopulateCboYear and called the function when the form is loaded:

    Private Sub PopulateCboYear()

    Dim iYear As Integer

    For iYear = 1900 To Year(Now())
    cboYear.AddItem (iYear)

    Next

    End Sub

    The function worked perfectly in Access 2002 and 2003, but not in 2000. I then realized that Access 2000 does not have the AddItem function and, therefore, added a module called AddItem according to some suggestions I found on the internet:

    Sub ListAddItem(LB As ListBox, ByVal strItemToAdd As String)

    LB.RowSource = IIf(LB.RowSource = "", strItemToAdd, LB.RowSource & ";" & strItemToAdd)

    End Sub


    I was hoping that by adding this module, the PopulateCboYear function would work, but it doesn't, and I can use some help from smart people like you!!

    Thank you,
    egghi

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Access 2000 AddItem

    Without looking any farther, the first error is that you're trying to populate a combobox, but declaring it in your module as a listbox.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    21

    Re: Access 2000 AddItem

    Hello Salvelinus,

    Thank you for pointing that out! I changed the module to:

    Sub CboAddItem(CB As ComboBox, ByVal strItemToAdd As String)

    CB.RowSource = IIf(CB.RowSource = "", strItemToAdd, CB.RowSource & ";" & strItemToAdd)

    End Sub


    However, I am not sure what I need to do to edit the actual combo box, cboYear, and its property in order to populate the list of years correctly in the combo box... Any advice?

    Thanks,
    egghi

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Access 2000 AddItem

    Typically, I might populate a combobox by setting Row Source Type to Table/Query and then setting Row Source to a saved query.
    But, to do it your way, try something like this:
    VB Code:
    1. Private Sub Form1_Load()
    2.    Dim s As String
    3.    Dim i As Integer
    4.    me.cboYear.RowSourceType = "Value List"
    5.    For i = 1900 to Year(Now())
    6.       If i = Year(Now()) Then   'this will prevent an empty record being added
    7.          s = s & Cstr(i)
    8.       Else
    9.          s = s & Cstr(i) & ";"
    10.       End If
    11.    Next i
    12.    me.cbo.RowSource = s
    13. End Sub
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    21

    Re: Access 2000 AddItem

    Hi Savelinus,

    Your code rocks!! Thank youuuuuu Have a very happy Friday!

    egghi

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Access 2000 AddItem

    You're welcome. Not sure it'll work in Access 2003 since I've never used it.
    Tengo mas preguntas que contestas

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    21

    Re: Access 2000 AddItem

    It works in Access 2003 as well Thank you for your help!!

Posting Permissions

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



Click Here to Expand Forum to Full Width