Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

.AddItem

Discussion in 'Information Technology' started by Caligo, Jul 28, 2006.

Tags:
  1. Caligo

    Caligo
    Expand Collapse
    Guest

    I am new to Microsoft Access and VB so I am going through the internet
    looking for tutorials. I stumbled across one that has the .AddItem
    method, but it seems that Access does not use that code. This is the
    little bit of code that I have:

    Private Sub cmdAdd_Click()
    lstTextList.AddItem (txtAddText.Text)

    If (cmdDelete.Enabled = False) Then
    cmdDelete.Enabled = True
    End If
    End Sub

    cmdAdd being a button
    lstTextList a list box
    txtAddText a text box
    cmdDelete another button

    I want to add to the list box when I click on the cmdAdd button, but
    when I do it doesn't work. If someone could explain it to me I would be
    very grateful.
     
  2. Wayne Morgan

    Wayne Morgan
    Expand Collapse
    Guest

    There are a few problems here.

    1) You are referring to the Text property of a textbox (txtAddText.Text)
    when the textbox doesn't have the focus. As soon as the textbox loses the
    focus, it updates and the Text will become the Value of the textbox. Since
    you're doing this in the Click routine of the button, as soon as you click
    on the button, it has the focus, not the textbox.

    2) You don't specify the Row Source Type of the listbox. To use AddItem, the
    Row Source Type must be set to Value List.

    3) Unless the form is in Design View when you do this, any items you add to
    the list will be lost when the form is closed.

    If you are using a table or query as the Row Source of the listbox, you need
    to add the item to the underlying table then Requery the listbox to force it
    to retrieve the updated Row Source data.

    --
    Wayne Morgan
    MS Access MVP


    "Caligo" <aterp413@aol.com> wrote in message
    news:1152300268.215253.245270@b28g2000cwb.googlegroups.com...
    >I am new to Microsoft Access and VB so I am going through the internet
    > looking for tutorials. I stumbled across one that has the .AddItem
    > method, but it seems that Access does not use that code. This is the
    > little bit of code that I have:
    >
    > Private Sub cmdAdd_Click()
    > lstTextList.AddItem (txtAddText.Text)
    >
    > If (cmdDelete.Enabled = False) Then
    > cmdDelete.Enabled = True
    > End If
    > End Sub
    >
    > cmdAdd being a button
    > lstTextList a list box
    > txtAddText a text box
    > cmdDelete another button
    >
    > I want to add to the list box when I click on the cmdAdd button, but
    > when I do it doesn't work. If someone could explain it to me I would be
    > very grateful.
    >
     
  3. Caligo

    Caligo
    Expand Collapse
    Guest

    I apologize for not understanding, but I don't see why the textbox has
    to have the focus in order to add it to the listbox. Some more
    explaining on that would be helpful.

    As for the Row Source Type, I do have it set to Value List in the
    properties of the listbox, just not in the code. Do I have to have it
    in the code also?

    I am doing all of this in design view, yes.

    The only place I am getting any information from to put in the listbox
    is from the textbox, not from a table or query.
     
  4. Wayne Morgan

    Wayne Morgan
    Expand Collapse
    Guest

    The .Text and .Value properties are two different properties of the textbox.
    The reason you can't use the Text value if the textbox doesn't have the
    focus is because the Text value is the value of the text in the textbox
    BEFORE the textbox updates. The textbox will Update its Value to what you've
    typed in the Text as soon as the textbox loses the focus (this is when the
    BeforeUpdate and AfterUpdate events of the textbox will run). What used to
    be the Text value is now the Value value. If you try to access the Text
    value when the textbox doesn't have the focus, you'll get the error "You
    can't reference a property or method for a control unless the control has
    the focus."

    If the Row Source Type is ValueList then no, you don't need to set it again
    via code.

    You say you are doing all of this in Design View of the form. If so, then
    the textbox shouldn't have a Text or Value value. These are there during run
    time. If you try to get the Text or Value value from a textbox in design
    view you'll get the error "This property isn't available in Design view."

    --
    Wayne Morgan
    MS Access MVP


    "Caligo" <aterp413@aol.com> wrote in message
    news:1152534073.905661.152480@h48g2000cwc.googlegroups.com...
    >I apologize for not understanding, but I don't see why the textbox has
    > to have the focus in order to add it to the listbox. Some more
    > explaining on that would be helpful.
    >
    > As for the Row Source Type, I do have it set to Value List in the
    > properties of the listbox, just not in the code. Do I have to have it
    > in the code also?
    >
    > I am doing all of this in design view, yes.
    >
    > The only place I am getting any information from to put in the listbox
    > is from the textbox, not from a table or query.
    >
     
  5. Jon Lewis

    Jon Lewis
    Expand Collapse
    Guest

    "Caligo" <aterp413@aol.com> wrote in message
    news:1152534073.905661.152480@h48g2000cwc.googlegroups.com...
    >I apologize for not understanding, but I don't see why the textbox has
    > to have the focus in order to add it to the listbox. Some more
    > explaining on that would be helpful.


    The 'Text' property of an editable control can only be read when the control
    has focus. If you're running code on for example the click event of a
    command button, the command button has focus (unless you've shifted focus
    elsewhere from within the On Click code). So try using txtAddText.Value
    instead of txtAddText.Text


    > As for the Row Source Type, I do have it set to Value List in the
    > properties of the listbox, just not in the code. Do I have to have it
    > in the code also?


    Not as far as I know

    >
    > I am doing all of this in design view, yes.


    Wayne means that you have to actually run the cmdAdd_Click() code whilst
    your container form of the listbox is in design view in order that the
    additions to its valuelist be permanently stored. You probably aren't doing
    this so you would need a look up table as the list's Row Source and have
    your Add Item routine add the new item to the table and then requery the
    listbox.

    >
    > The only place I am getting any information from to put in the listbox
    > is from the textbox, not from a table or query.
    >
     
  6. Caligo

    Caligo
    Expand Collapse
    Guest

    I am still quite confused, I now understand the reasoning behind the
    focus on the textbox, but I don't know how to fix it. Think the code
    for changing the focus to the textbox when I click the button would be:

    Private Sub cmdAdd_Click()
    txtAddText.SetFocus

    I am not sure if that is 100% correct though.

    Also, when I click on the 'Add' button in form view it goes to my code
    saying

    "Compile Error: method or data member not found."

    and highlights the .addItem in the script. That has been my problem all
    along, but I don't know how to fix it.

    Here is my entire code for the form.

    Private Sub Form_Load()
    If (lstTextList.ListCount = 0) Then
    cmdDelete.Enabled = False
    End If
    End Sub
    ______________________________________________

    Private Sub cmdAdd_Click()
    txtAddText.SetFocus
    lstTextList.AddItem (txtAddText.Text)

    If (cmdDelete.Enabled = False) Then
    cmdDelete.Enabled = True
    End If
    End Sub
    ______________________________________________

    Private Sub cmdDelete_Click()

    If (Not (lstTextList.ListCount = 0)) Then
    lstTextList.RemoveItem (lstTextList.ListCount - 1)
    If (lstTextList.ListCount = 0) Then
    cmdDelete.Enabled = False
    End If
    End If
    End Sub
    ______________________________________________

    Please just correct anything I have wrong and explain it to me also. I
    am a visual learner and if I see the correct code it is easier for me
    to understand. Sorry for not being able to understand, but thank you
    for your help.
     
  7. Wayne Morgan

    Wayne Morgan
    Expand Collapse
    Guest

    You usually don't need to move the focus back to the textbox. As I stated
    before, if the focus has been moved from the textbox, what was the Text
    value is now the Value value. Just use that instead (txtAddText.Value). Yes,
    you are correct on the command to move the focus to the textbox if you want
    to do so.

    I don't see anything wrong with the AddItem code provided that the Row
    Source Type of the listbox is set to Value List. If it wasn't, you would get
    an error stating such, not the error you're currently getting.

    Where is the listbox located? Is it on the same form as the button? Is it on
    a subform or main form of the form that the button is on? What happens if
    you change the line to

    Me.lstTextList.AddItem txtAddText.Value

    Also, as stated previously, if you're doing this in run mode, the changes to
    the list won't be saved once the form is closed and if you're doing this in
    design view of the form, then the textbox doesn't have a Text or Value
    property while in design view.

    --
    Wayne Morgan
    MS Access MVP


    "Caligo" <aterp413@aol.com> wrote in message
    news:1152540126.122887.98360@p79g2000cwp.googlegroups.com...
    >I am still quite confused, I now understand the reasoning behind the
    > focus on the textbox, but I don't know how to fix it. Think the code
    > for changing the focus to the textbox when I click the button would be:
    >
    > Private Sub cmdAdd_Click()
    > txtAddText.SetFocus
    >
    > I am not sure if that is 100% correct though.
    >
    > Also, when I click on the 'Add' button in form view it goes to my code
    > saying
    >
    > "Compile Error: method or data member not found."
    >
    > and highlights the .addItem in the script. That has been my problem all
    > along, but I don't know how to fix it.
    >
    > Here is my entire code for the form.
    >
    > Private Sub Form_Load()
    > If (lstTextList.ListCount = 0) Then
    > cmdDelete.Enabled = False
    > End If
    > End Sub
    > ______________________________________________
    >
    > Private Sub cmdAdd_Click()
    > txtAddText.SetFocus
    > lstTextList.AddItem (txtAddText.Text)
    >
    > If (cmdDelete.Enabled = False) Then
    > cmdDelete.Enabled = True
    > End If
    > End Sub
    > ______________________________________________
    >
    > Private Sub cmdDelete_Click()
    >
    > If (Not (lstTextList.ListCount = 0)) Then
    > lstTextList.RemoveItem (lstTextList.ListCount - 1)
    > If (lstTextList.ListCount = 0) Then
    > cmdDelete.Enabled = False
    > End If
    > End If
    > End Sub
    > ______________________________________________
    >
    > Please just correct anything I have wrong and explain it to me also. I
    > am a visual learner and if I see the correct code it is easier for me
    > to understand. Sorry for not being able to understand, but thank you
    > for your help.
    >
     
  8. Caligo

    Caligo
    Expand Collapse
    Guest

    The listbox is on the same form as the buttons and the textbox.

    I tried

    Me.lstTextList.AddItem txtAddText.Value

    and it still came up with the same error message.
     
  9. Wayne Morgan

    Wayne Morgan
    Expand Collapse
    Guest

    Then I would suspect that something is misspelled or the listbox is
    corrupted.

    --
    Wayne Morgan
    MS Access MVP


    "Caligo" <aterp413@aol.com> wrote in message
    news:1152547793.156301.212950@p79g2000cwp.googlegroups.com...
    > The listbox is on the same form as the buttons and the textbox.
    >
    > I tried
    >
    > Me.lstTextList.AddItem txtAddText.Value
    >
    > and it still came up with the same error message.
    >
     
  10. Caligo

    Caligo
    Expand Collapse
    Guest

    I checked everything multiple times and nothing that I can see is
    misspelled. I changed the listbox and even made an entire new project
    and it still doesn't work. Thanks for the help anyways though.
     
  11. Brendan Reynolds

    Brendan Reynolds
    Expand Collapse
    Guest

    Which version of Access? If I remember correctly, I believe the AddItem
    method was new in Access 2002.

    --
    Brendan Reynolds
    Access MVP

    "Caligo" <aterp413@aol.com> wrote in message
    news:1152622933.202498.156370@b28g2000cwb.googlegroups.com...
    >I checked everything multiple times and nothing that I can see is
    > misspelled. I changed the listbox and even made an entire new project
    > and it still doesn't work. Thanks for the help anyways though.
    >
     
  12. Caligo

    Caligo
    Expand Collapse
    Guest

    Oh, well, I have 2000, so that would be why it doesn't work then.
     
  13. Wayne Morgan

    Wayne Morgan
    Expand Collapse
    Guest

    Thanks Brendon. I checked the 2000 help file and there is no mention of
    AddItem except for adding to a toolbar.

    --
    Wayne Morgan
    MS Access MVP


    "Caligo" <aterp413@aol.com> wrote in message
    news:1152706737.226971.24940@h48g2000cwc.googlegroups.com...
    > Oh, well, I have 2000, so that would be why it doesn't work then.
    >
     
  14. Brendan Reynolds

    Brendan Reynolds
    Expand Collapse
    Guest

    Yep. In 2000, you can add items to a combo box with a RowSourceType of Value
    List by appending to the semi-colon delimited string.

    <untested air code>
    strValueList = Me!SomeCombo.RowSource
    If Right$(strValueList, 1) <> ";" Then
    strValueList = strValueList & ";"
    End If
    strValueList = strValueList & MyNewValue
    Me!SomeCombo.RowSource = strValueList
    </untested air code>

    This change will not be permanent, though - it will last only as long as the
    form is open, and will be disgarded when it is closed. If you want to add
    items to the list permanently, it might be better to use a table for the row
    source.

    --
    Brendan Reynolds
    Access MVP

    "Caligo" <aterp413@aol.com> wrote in message
    news:1152706737.226971.24940@h48g2000cwc.googlegroups.com...
    > Oh, well, I have 2000, so that would be why it doesn't work then.
    >
     
  15. Caligo

    Caligo
    Expand Collapse
    Guest

    Alright, thanks for the info, it definitely helps explain why it
    wouldn't work with the list box.
     

Share This Page