Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding (text) items to listboxes and getting/setting text in textboxes

Forms

Adding (text) items to listboxes and getting/setting text in textboxes

by  bujin  Posted    (Edited  )
I don't know whether it's just that I've never found it, but I was surprised to see that listboxes and comboboxes don't have the .AddItem method in Access that they have in Excel and Word.

Here's the function I use to add an item to an Access Listbox or Combobox. I had written a DelListItem sub too, but it was fundamentally flawed, so I haven't included it here.

---

Sub AddListItem(lst As Control, S As String)
With lst
If (.ControlType = acComboBox) Or (.ControlType = acListBox) Then
If .RowSourceType = "Value List" Then
If .ListCount > 0 Then
.RowSource = .RowSource & "; " & S
Else
.RowSource = .RowSource & S
End If
End If
End If
End With
End Sub

---

Call this function using:

AddListItem lstName, "text string"

---

The next one is due to the annoying problem of having to set focus to textboxes or comboboxes to read or set the information in them. The first is used to get the text from a textbox, and is simply used by calling:

GetText ctrName

The second sets the text in a textbox. Use:

SetText ctrName, "text string"

---

Function GetText(ctr As Control) As String
With ctr
If (.ControlType = acTextBox) Or (.ControlType = acComboBox) Then
.SetFocus
GetText = .Text
End If
End With
End Function

---

Sub SetText(ctr As Control, txt As String)
With ctr
If (.ControlType = acTextBox) Or (.ControlType = acComboBox) Then
.SetFocus
ctr.Text = txt
End If
End With
End Sub
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top