INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Forms

Adding (text) items to listboxes and getting/setting text in textboxes by bujin
Posted: 18 May 01

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

Back to Microsoft: Access Other topics FAQ Index
Back to Microsoft: Access Other topics Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close