.AddItem

C

Caligo

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.
 
W

Wayne Morgan

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.
 
C

Caligo

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.
 
W

Wayne Morgan

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."
 
J

Jon Lewis

Caligo said:
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.
 
C

Caligo

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.
 
W

Wayne Morgan

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.
 
C

Caligo

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.
 
W

Wayne Morgan

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

Caligo

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.
 
B

Brendan Reynolds

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

Wayne Morgan

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

Brendan Reynolds

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.
 
C

Caligo

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top