Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sesame - 02 Dec 2004 01:41 GMT
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
Helmut Weber - 02 Dec 2004 09:48 GMT
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
Sesame - 02 Dec 2004 23:03 GMT
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,

> Hi,
> seems you are looking for "split", like
[quoted text clipped - 19 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.