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 / July 2007

Tip: Looking for answers? Try searching our database.

Multiple listbox problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amy - 26 Jul 2007 17:40 GMT
I want to create multiple list boxes in a user form, and then have the
selections appear in corresponding bookmarks.

I found this bit of code, but I don't understand why selections for both
Activity1 and Activity2 appear in the Activity2 bookmark. How can I make sure
that only Activity2 selections appear in Activity2 bookmark? Thanks!

For i = 1 To Activity1.ListCount
   If Activity1.Selected(i - 1) Then
        myString = myString + Activity1.List(i - 1) & vbCrLf
   End If
Next
ActiveDocument.Bookmarks("Activity1").Range.InsertBefore myString

For i = 1 To Activity2.ListCount
   If Activity2.Selected(i - 1) Then
        myString = myString + Activity2.List(i - 1) & vbCrLf
   End If
Next
ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
Russ - 26 Jul 2007 18:56 GMT
Reset myString =""
Between the loops.

> I want to create multiple list boxes in a user form, and then have the
> selections appear in corresponding bookmarks.
[quoted text clipped - 16 lines]
> Next
> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 26 Jul 2007 19:01 GMT
Don't use the word Reset, just:
myString = ""
> Reset myString =""
> Between the loops.
[quoted text clipped - 19 lines]
>> Next
>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 26 Jul 2007 19:25 GMT
Since this is the beginner's forum, I'll explain a little more.
The line:
myString = myString + ...

Probably should be:
myString = myString & ...
And means set myString equal to the current contents of myString and
concatenate whatever follows. The plus (+) should be left for arithmetic
operations like MyNumber = MyNumber + 1 to increment MyNumber.

Between the loops you still have the old stuff in myString, until you reset
it to an empty string with "". So after you use myString with the bookmark,
reset it.

> Don't use the word Reset, just:
>  myString = ""
[quoted text clipped - 22 lines]
>>> Next
>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Amy - 26 Jul 2007 22:40 GMT
I see. Looks good. Thanks!

> Since this is the beginner's forum, I'll explain a little more.
> The line:
[quoted text clipped - 36 lines]
> >>> Next
> >>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
Amy - 26 Jul 2007 23:22 GMT
Is there a way to remove the paragraph mark from the last list item? As it is
now, I have an extra paragraph at the end of the list.

Thank you!
Amy

> Since this is the beginner's forum, I'll explain a little more.
> The line:
[quoted text clipped - 36 lines]
> >>> Next
> >>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
Russ - 27 Jul 2007 06:02 GMT
Yes below the Next line use:
myString = Left(myString, Len(myString) - 1)

Nested functions usually are parsed from the inside out.
So first the number of characters in myString are found with Len().
1 is subtract from that number.
The result is used to tell Left() how many characters on the left side of
the myString we want.
And finally, myString is set equal to that result.

So we are dropping off the last character, which was a vbCrLf character.

> Is there a way to remove the paragraph mark from the last list item? As it is
> now, I have an extra paragraph at the end of the list.
[quoted text clipped - 42 lines]
>>>>> Next
>>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Amy - 28 Jul 2007 00:04 GMT
Ah interesting. I'd like to give users the option to not have to fill out
everything. With this line, seems like they need to fill out all the
listboxes. Is that true?

Amy

> Yes below the Next line use:
> myString = Left(myString, Len(myString) - 1)
[quoted text clipped - 54 lines]
> >>>>> Next
> >>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
Russ - 28 Jul 2007 19:12 GMT
Amy,
The code snippet that you have shown is apparently inserting selected
choices at bookmarks and that is all I see it doing. I don't see where it is
validating the choices. If you want the computer to do something different,
then you need to tell it to do something different. A computer tries to do
what you tell it to do, not necessarily what you intend it to do. I have no
idea why you believe users are forced to fill out everything. Are there
error messages that pop up when everything is not filled out? Can the user
not skip over a listbox? Is that the way you set up the form?

"With this line, seems like they need to fill out all the
listboxes. Is that true?" What line are you talking about? You must have
forgotten to include the line you were talking about in your reply. The line
I gave you, *myString = Left(myString, Len(myString) - 1)*, just dropped off
the last paragraph mark, like you requested. You were suppose to insert it
under the two lines in the code snippet that just had the word "Next".
Didn't it remove the last paragraph mark for you?

> Ah interesting. I'd like to give users the option to not have to fill out
> everything. With this line, seems like they need to fill out all the
[quoted text clipped - 61 lines]
>>>>>>> Next
>>>>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Amy - 31 Jul 2007 17:52 GMT
When I add myString = Left(myString, Len(myString) - 1) after Next, I get an
error message that says "Run-time error 5, Invalid procedure call or
argument." Then when I switch to the VB screen, myString = Left(myString,
Len(myString) - 1) is highlighted.

This error occurs when I leave the listbox blank. When I remove myString =
Left(myString, Len(myString) - 1), then I do not get an error when I leave
the listbox blank. What is the reason for this? I've provided more of my code
below, for your reference. Thanks so much for your assistance! Amy

My code is:

Private Sub CommandButton1_Click()
Dim i As Long
Dim myString As String

With ActiveDocument
   
For i = 1 To Activity1.ListCount
   If Activity1.Selected(i - 1) Then
        myString = myString & Activity1.List(i - 1) & vbCrLf
   End If
Next
myString = Left(myString, Len(myString) - 1)
ActiveDocument.Bookmarks("Activity1").Range.InsertBefore myString
myString = ""

For i = 1 To Activity2.ListCount
   If Activity2.Selected(i - 1) Then
        myString = myString & Activity2.List(i - 1) & vbCrLf
   End If
Next
myString = Left(myString, Len(myString) - 1)
ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
myString = ""

End With

Me.Hide

End Sub

> Amy,
> The code snippet that you have shown is apparently inserting selected
[quoted text clipped - 79 lines]
> >>>>>>> Next
> >>>>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
Russ - 31 Jul 2007 18:22 GMT
Encapsulate that line with an empty string check.
If Not myString = "" then
   myString = Left(myString, Len(myString) - 1)
End If

> When I add myString = Left(myString, Len(myString) - 1) after Next, I get an
> error message that says "Run-time error 5, Invalid procedure call or
[quoted text clipped - 125 lines]
>>>>>>>>> Next
>>>>>>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 31 Jul 2007 18:27 GMT
I think you get an error because with an empty string the Len (or length) is
zero, then trying to subtract a 1 to get rid of a trailing paragraph mark
gives a -1, which the Left() function does not expect, it wants positive
numbers.

> When I add myString = Left(myString, Len(myString) - 1) after Next, I get an
> error message that says "Run-time error 5, Invalid procedure call or
[quoted text clipped - 125 lines]
>>>>>>>>> Next
>>>>>>>>> ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


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.