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

Tip: Looking for answers? Try searching our database.

Populate combo and mulitlist with same DAO source

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kerri - 09 Nov 2007 01:56 GMT
Hi,

I am currently working in Word 2003 VBA UserForms.  I could use a
little help populating my multi list combo box with my DAO data
(Excel).  I have the code, thanks to the help of these groups, for
populating a combo box.  However I want to populate both a combo box
and a multi list box differently.

cmbAuthorsInitials - This combo box is linked to the DAO data in
column 1. (In a memo this gets the Authors information from the Excel
table).  This part works fine.

lstTo -  I want this multi list combo box to display a different
column of data from the same DAO source as the cmbAuthorsInitinals .
(This will list the recipients the memo is going to).

I tried to populate both boxes with the same Load instructions but I
couldn't get it to work.  So I had the cmbAuthorsInitials box fill in
to the lstTo box.  This worked but the only problem is I want it to
list the Authors Name from column2 not column1.

Maybe there is a better way to go about this.  I have never done the
list boxes before so any help would be greatly appreciated.

Kerri

CODE:
Private Sub UserForm_Initialize()

'=====Load Authors data from Excel table=====
 'Contains a list of Authors and preferences
 'NOTE: Must Reference "Microsoft DAO 3.6 Object Library" by going to
"Tools | References..." for Word 2003
   Dim db As DAO.Database
   Dim rs As DAO.Recordset
   Dim NoOfRecords As Long

   ' Open the database
   Set db = OpenDatabase("c:\_Auth\AuthList.xls", False, False,
"Excel 8.0")

   ' Retrieve the recordset
   Set rs = db.OpenRecordset("SELECT * FROM `Authors`") '**Authors is
Named Range in the Excel Spreadsheet.

   ' Determine the number of retrieved records
   With rs
        .MoveLast
        NoOfRecords = .RecordCount
        .MoveFirst
   End With

   ' Set the number of Columns = number of Fields in recordset
   cmbAuthorsInitials.ColumnCount = rs.Fields.Count
''    lstTo.ColumnCount = rs.Fields.Count '*

   ' Load the ListBox with the retrieved records
   cmbAuthorsInitials.Column = rs.GetRows(NoOfRecords)
   'added to also fill in the Listbox to field
''   lstTo.List = rs.GetRows(NoOfRecords) '*

   ' Cleanup
   rs.Close
   db.Close

   Set rs = Nothing
   Set db = Nothing
'=======End Excel Datafile=================

lstTo.List = cmbAuthorsInitials.List   '* set to name (col. 2) and not
initials?

End Sub

Private Sub cmdOK_Click()

'* lstTo Listbox results create an array to get contents of each row.
   Dim strTo As String
   strTo = cmbTo
       For i = 1 To lstTo.ListCount
           If lstTo.Selected(i - 1) = True Then
               strTo = strTo & ", " & lstTo.List(i - 1)
           End If
       Next i
   UpdateBookmarkText .Bookmarks("To"), strTo

'Is there a way to make an "and" appear before the last name from the
multi list?

End Sub
Doug Robbins - Word MVP - 09 Nov 2007 03:51 GMT
You use the .BoundColumn attribute of a combobox or a listbox the specify
the column in the box that contains the data that you want to use.

You can control what is displayed in a combobox or listbox by adjusting the
columnwidth attribute of the box - Set the width of a column to 0 and it
won't be displayed.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi,
>
[quoted text clipped - 86 lines]
>
> End Sub
Kerri - 09 Nov 2007 05:54 GMT
Hi Doug,

Thank you for respondnig.  I think my problem happens before I use
the .boundcolumn.  I can't even get the rest of the columns from the
DAO data to show in the lstTo box.  So I am unable to use the
boundcolum.   I think it's because I am trying to pull the data that
was filled into the combo box from the DAO into the list box.  I tried
to change the column width property and it still only displayed the
first row of data.

This is the code I used to populate my listbox (lstTO).
lstTo.List = cmbAuthorsInitials.List

I also tried to use the following code to populate the lstTO box
 ' Set the number of Columns = number of Fields in recordset
   cmbAuthorsInitials.ColumnCount = rs.Fields.Count     'COMBO BOX
works great
''    lstTo.ColumnCount = rs.Fields.Count '*                    'Can
I populate List box also?

   ' Load the ListBox with the retrieved records
   cmbAuthorsInitials.Column = rs.GetRows(NoOfRecords)
   'added to also fill in the Listbox to field
''   lstTo.List = rs.GetRows(NoOfRecords) '*

Am I going about it the wrong way?  Basically, I'm trying to populate
a combo and list box with the same DAO data but they are independent
of each other.

Thank you again, for taking the time to answer my questions.
Kerri

On Nov 8, 8:51 pm, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org>
wrote:
> You use the .BoundColumn attribute of a combobox or a listbox the specify
> the column in the box that contains the data that you want to use.
[quoted text clipped - 103 lines]
>
> - Show quoted text -
Doug Robbins - Word MVP - 09 Nov 2007 06:59 GMT
You can populate the listbox with the data from the original source in
exactly the same way as you populate the combobox

Just substitute the combobox name with the name of the listbox.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi Doug,
>
[quoted text clipped - 138 lines]
>>
>> - Show quoted text -
Kerri - 12 Nov 2007 17:22 GMT
Hi Doug,

Thank you for your suggestion.  I created two versions of the DAO code
and it now populates the list box completly.  However, I still can't
seem to get the .boundcolum to work with the mulilist box.  Now that
the list box contains all the data in the listbox, I should be able to
pull in column 2 info, instead of column 1.   Any suggestions?

Is there a way to make an "and" appear before the last name that is
selected in the multi list?

Private Sub cmdOK_Click()
'* lstTo Listbox results create an array to get contents of each row.
   Dim strTo As String

       For i = 1 To lstTo.ListCount
           If lstTo.Selected(i - 1) = True Then
              lstTo.BoundColumn = 2  'Name
              strTo = strTo & "; " & lstTo.List(i - 1)
                  ' If _________  & " and"
           End If
       Next i
   UpdateBookmarkText .Bookmarks("To"), strTo

Thank you for your help!
Kerri

On Nov 8, 11:59 pm, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> You can populate the listbox with the data from the original source in
> exactly the same way as you populate the combobox
[quoted text clipped - 157 lines]
>
> - Show quoted text -
Doug Robbins - Word MVP - 15 Nov 2007 09:14 GMT
With a MultiSelect ListBox, you cannot use the .Value of the ListBox to
return the selected item.  Rather, you need to use  .List(rownum, colnum)

To create a string of the selected items in column 2 of a MultiSelect list
box with the items other than the last separated by semi-colons and the word
"and" before the last item, you need to use the following code:

Private Sub CommandButton1_Click()
Dim i As Integer, j As Long, k As Long
Dim MyString As String

MyString = ""
j = 0
With ListBox1
   For i = 0 To .ListCount - 1
       '..and check whether each is selected
       If ListBox1.selected(i) Then
           j = j + 1
       End If
   Next i
   If j > 1 Then
       k = 0
       For i = 0 To .ListCount - 1
           If .selected(i) Then
               k = k + 1
               If k = 1 Then
                   MyString = MyString & .list(i, 1)
               ElseIf k < j Then
                   MyString = MyString & "; " & .list(i, 1)
               Else
                   MyString = MyString & " and " & .list(i, 1)
               End If
           End If
       Next i
   Else
       For i = 0 To .ListCount - 1
           If .selected(i) Then
               MyString = .list(i, 1)
           End If
       Next i
   End If
End With
MsgBox MyString
End Sub

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi Doug,
>
[quoted text clipped - 196 lines]
>>
>> - Show quoted text -
Kerri - 15 Nov 2007 20:53 GMT
Doug, you are awesome...but you probably knew that already!  I went
through the code and put comments next to each item to explain the
purpose of the i,j and k.  I relisted the code with my comments.  If I
didn't explain it properly would you correct me.  I hope to be able to
do this on my own one day...I may never be as good as you but I'm
trying.

THANK YOU!!!!!! Kerri

Private Sub CommandButton1_Click()
Dim i As Integer, j As Long, k As Long
Dim MyString As String

MyString = ""
j = 0
With ListBox1
      '*This sections is to count number of selected items
    For i = 0 To .ListCount - 1  '-1 includes all columns, i=0 means
nothing selected
        '..and check whether each is selected
        If ListBox1.selected(i) Then
              '* J refers to number of selected items - if selected
then j increases by 1
            j = j + 1
        End If
    Next i

    If j  1 Then  'If multiple items are selected

          '*k is always one less than the selected items so you can
put "and" instead of ";"
        k = 0
          '*Gets the selected item from the list (i, 1) 1 indicates
column2 of the table bec. it
             'starts w/0
        For i = 0 To .ListCount - 1
            If .selected(i) Then

                k = k + 1
                If k = 1 Then
                    MyString = MyString & .list(i, 1) '*no
punctuation just insert name
                ElseIf k < j Then
                    MyString = MyString & "; " & .list(i, 1) '*if K
is less than j include " ;"
                Else
                       '*since there is more than one item put " and"
before last item selected.
                    MyString = MyString & " and " & .list(i, 1)
                End If
            End If
        Next i
    Else   '*if only one item is selected do this.
        For i = 0 To .ListCount - 1
            If .selected(i) Then
                MyString = .list(i, 1)
            End If
        Next i
    End If
End With
MsgBox MyString
End Sub

On Nov 15, 2:14 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> With a MultiSelectListBox, you cannot use the .Value of theListBoxto
> return the selected item.  Rather, you need to use  .List(rownum, colnum)
[quoted text clipped - 255 lines]
>
> - Show quoted text -
Doug Robbins - Word MVP - 16 Nov 2007 01:47 GMT
Yes, your understanding is correct.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Doug, you are awesome...but you probably knew that already!  I went
> through the code and put comments next to each item to explain the
[quoted text clipped - 332 lines]
>>
>> - Show quoted text -
 
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.