Is there a way to bound 2 items in a textbox to a listbox?

S

Sesame

Hi
I currently have a Template with a textbox (tbName), a listbox (Listbox2)
and an "Add" button (cmdAddName). So user can type a name in the textbox,
click the Add command button, the name will appear in the listbox. This can
be done repeatitively so that eventually there's a list of Names. And this
list of names will be inserted as bookmarks in the document. But now, I need
it to store 2 Names at once. Ultimately, the user could enter 2 Names,
separated by a comma or a semi colon (1st is long name, 2nd is short name),
for e.g "Samantha, Sam" or "Samantha; Sam". Again, store them as list of
names (but bound with 2 items instead of 1), then insert the list as
bookmarks again, so that it looks like this in the document.

SAMANTHA ("SAM")

Any help would be very much appreciated. I'll paste the codes I have now.
Thank you!!

Private Sub cmdAddName_Click()
thisProcName = "Name List"

If Trim(tbName) <> "" Then
With ListBox2
.ListIndex = -1
On Error Resume Next
.Text = tbName.Text
On Error GoTo 0
If Not (.ListIndex >= 0) Then
ListBox2.AddItem tbName.Text
tbName.Text = ""
tbName.SetFocus
cmdAddName.Enabled = False
.TopIndex = .ListCount
Else
MsgBox "This name is already in the list.", vbOKOnly,
ModProcName
End If
End With
End If
End Sub

Private Sub ListBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Dim thisItem As Integer

With ListBox2
thisItem = .ListIndex
If KeyCode = vbKeyDelete And thisItem <> -1 Then
.RemoveItem thisItem
If thisItem < .ListCount Then
'select the new item
If thisItem < .ListCount Then .Selected(thisItem) = True
End If
End If
End With
End Sub

Private Sub tbName_Change()
cmdAddName.Enabled = (Trim(tbName) <> "")
End Sub
 
H

Helmut Weber

Hi,
seems you are looking for "split", like
---
Textbox1.text = "Daniel, Danny"
---
Private Sub CommandButton1_Click()
Dim ArNames() As String
Dim i As Integer
ArNames = Split(TextBox1.Text, ",")
For i = 0 To UBound(ArNames)
MsgBox "[" & ArNames(i) & "]"
' add to list etc.
Next
End Sub

"[" "]" are used to indicate remaining spaces,
which could be removed by "Trim". Of course, you
may use ", " as split parameter.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
S

Sesame

Hi Helmut

Thanks so much for your reply. I realised I have to use Split function too.
Will have a play around with that. I think my main problem now is the way I
want the document to display the long and short names. If I have any problem,
I would post under this subject again.

Many Thanks,
 

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