Use list from excel or access to populate combo/list box 
Author Message
 Use list from excel or access to populate combo/list box

Hi could anyone either help me or point me in the right direction to find
out how to use an excel or access list to populate a combo/list box

Thanks in advance

Nick



Tue, 19 Aug 2003 20:38:41 GMT  
 Use list from excel or access to populate combo/list box
Hi Nick,

Quote:
> Hi could anyone either help me or point me in the right direction to find
> out how to use an excel or access list to populate a combo/list box

See the following for an example of how to retrieve data from an Excel
Spreadsheet:

<http://www.mvps.org/word/FAQs/InterDev/XLToWordWithDAO.htm>

You will then have to load the retrieved data into your List/ComboBox.

--
Hope this helps.
ibby

Please post replies or follow-ups to the **newsgroup** so that participants
may benefit or contribute.



Tue, 19 Aug 2003 21:37:42 GMT  
 Use list from excel or access to populate combo/list box
Hi Nick,

Next to Ibby's, here's one additionally ...
Send you a couple of others in a few secs

Krgrds
Perry

'******************
You'll have to use a bit of automation to make this work.
I would use the List property of a Listbox instead of the
Rowsource property.
The A1:E4 range will be transfered to Word as an array
of values, and can be used to populate listboxes using
it's List property, something like:

Dim XL As Object
Set XL = GetObject("c:\temp\map1.xls")
XL.Application.Visible = False
ListBox1.List = XL.sheets("Sheet1").Range("A1:E4").Value

XL.Close 0
Set XL = Nothing

If still having problems, repost ...

Krgrds,
Perry



Quote:
> Hi could anyone either help me or point me in the right direction to find
> out how to use an excel or access list to populate a combo/list box

> Thanks in advance

> Nick



Wed, 20 Aug 2003 00:48:13 GMT  
 Use list from excel or access to populate combo/list box
Hi Nick,

And here's the other one ...

Krgrds,
Perry

*************************
Since your question resembles another one in the vba.general
I've answered in vba.general. Look at below message

Few remarks I forgot to mention:
The Excel example: no reference needed there; we're latebinding
The Access one: set a reference to ADODB in VBE Tools|Options

Krgrds,
Perry

**************** VBA GENERAL ***********

Hi John,

Here is an Excel example:
Situation
Workbook: c:\temp\NAW.xls containing 2 columns
A = ID and B = Name, data kicking off in A2 (A1 = columnheader)

Private Sub UserForm_Initialize()
    Dim xl As Object
    Dim wb As Object
    Dim myRng As Variant
    Set xl = CreateObject("Excel.Application")
    Set wb = xl.workbooks.Open("c:\temp\naw.xls")
    'we don't need to see Excel, make it inviz
    xl.Application.Visible = False
    'myRng variable will receive range data as an array of values
    myRng = wb.activesheet.Range("A2:B15").Value

    'word combobox, populate and hide column with ID values
    With ComboBox1
        .List = myRng            'store the array in the list property of
the combo
        .ColumnWidths = 0    'hide first column (ID values)
    End With

    wb.Close 0
    xl.Quit
    Set wb = Nothing
    Set xl = Nothing
End Sub

Here's an Access example
Situation
Database: c:\temp\db10.mdb
Tabel1 = tabel with 3 fields:
ID, FirstName, LastName
(7 records inputted, btw)

Private Sub UserForm_Initialize()

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim myArray As Variant
Dim myCorrArray As Variant
Dim numFields as Long

    'connection settings
    Set conn = New ADODB.Connection
    With conn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "data source=c:\temp\db10.mdb"
        .Open
    End With

    'recordset settings
    Set rs = New ADODB.Recordset
    rs.Open "SELECT Tabel1.* FROM Tabel1", conn, adOpenDynamic
    rs.MoveFirst
    numFields = rs.Fields.Count
    'pop the contents of recordset in array
    myArray = rs.GetRows

    'close the objects, and
    'reclaim memory from object vars
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing

    'determine records
    intid = UBound(myArray, numFields)
    ReDim myCorrArray(intid, numFields)

    'ADO returns records in 1st dimension
    'and fields in 2nd dimension of array to allow
    'backward compatibility with DAO and RDO
    'We'll have to reverse the order here
    'for a correct presentation in your combo
    For x = LBound(myArray) To UBound(myArray)
        For y = 0 To intid
            myCorrArray(y, x) = myArray(x, y)
        Next
    Next

    'pop the contents of correct array in
    'list property of combo
    ComboBox1.List = myCorrArray

End Sub

Krgrds,
Perry



Quote:
> Hi could anyone either help me or point me in the right direction to find
> out how to use an excel or access list to populate a combo/list box

> Thanks in advance

> Nick



Wed, 20 Aug 2003 00:50:28 GMT  
 Use list from excel or access to populate combo/list box
Hi Nick,m

Excel to Word via ADOR using an
ODBC driver connection type

Krgrds,
Perry

  Situation:
  Excel workbook c:\temp\MyData.xls
  Sheets(1), the sheet with data
  The way you set up the example in below coding, I've
  called the entire range: MyRange.

  Eventhough, connecting to Excel can be done
  using automation, I want to show another way
  of doing so, by means of an ODBC via ADOR method.
  Advantage: by using ODBC this will not put strain on your
  system, different than creating application objects via Automation.

  Here goes the code, it is tested and it works, however to position
  the tables in your document, you will need to play around with the
  code.

  Krgrds,
  Perry

  **** Here goes the code *****

  'Flag for new records
  Dim bNewRecord As Boolean

  '*************************
  Sub Test()
  'declarations
  '-------
  Dim rs As ADOR.Recordset
  Dim rst As ADOR.Recordset
  Dim conn As ADODB.Connection
  Dim myFields(0 To 3) As String
  Dim strTemp As String
  Dim strSQL As String

  'connection settings
  Set conn = New Connection
  With conn
      .ConnectionString = "Provider=MSDASQL;Driver={Microsoft Excel Driver
(*.xls)};DBQ=c:\temp\MyData.xls"
      .Open
  End With

  'Building up SQL string: note recordset sorted
  'by Variable2
  strSQL = "SELECT MyRange.Variable2, MyRange.Param_Num, "
  strSQL = strSQL + "MyRange.Param_Name, MyRange.Param_Format FROM "
  strSQL = strSQL + "MyRange ORDER BY Variable2"

  'recordset setting
  Set rs = New ADOR.Recordset
  With rs
      'default true
      bNewRecord = True
      'open recordset
      .Open strSQL, conn, adOpenDynamic, adLockBatchOptimistic

      'loop through records
      Do Until .EOF
          'test whether a new record is applicable
          If .Fields(0) <> strTemp Then bNewRecord = True
          'store values of each field in array
          For i = 0 To .Fields.Count - 1
              myFields(i) = IIf(IsNull(.Fields(i)), vbNullString,
.Fields(i))
          Next
          'if new record, make new table
          If bNewRecord Then
              Selection.MoveDown wdLine, 2
              Selection.TypeParagraph
              MakeTable myFields
          'else add row to table
          Else
              AddRow myFields
          End If
          'store old value, to compare with new one
          'that's about to come up
          strTemp = .Fields(0)
          'cursor to next record
          .MoveNext
      Loop
      'close loop
      .Close
  End With
  'close ODBC via ADO connection
  conn.Close
  'reclaim memory
  Set rs = Nothing
  Set conn = Nothing
  End Sub

  Private Sub MakeTable(arrFields)
      'add table, 4 columns; 1 row
      ActiveDocument.Tables.Add Selection.Range, 1, 4
      'populate cells with array values
      For x = LBound(arrFields) To UBound(arrFields)
          With Selection
          .Range.Text = arrFields(x)
          If x <> UBound(arrFields) Then .MoveRight wdCell
          End With
      Next
      'set flag to false
      bNewRecord = False
  End Sub

  Sub AddRow(arrFields)
  With Selection
      'add row
      .InsertRowsBelow 1
      'correct the cursos position
      .MoveLeft
      'populate cells with array values
      For x = LBound(arrFields) To UBound(arrFields)
          .Range.Text = Trim(arrFields(x))
          If x <> UBound(arrFields) Then .MoveRight wdCell
      Next
  End With

  End Sub



Quote:
> Hi could anyone either help me or point me in the right direction to find
> out how to use an excel or access list to populate a combo/list box

> Thanks in advance

> Nick



Wed, 20 Aug 2003 00:54:19 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Fill Combo List using Criteria selected in another Combo List

2. Fill Combo List using Criteria selected in another Combo List

3. How to list macro names in a combo box or a list box

4. Limiting Access to Text Boxes, List Boxes and Combo Boxes

5. Populating lists and combo boxes

6. Populate List Box with Large List

7. Populating a List Box with a large list

8. How to use Excel to populate List Box and Template

9. Listing all tables or queries in a list/combo box

10. How to list macro names in a combo box or a list

11. How to list macro names in a combo box or a list

12. Rebuild select list for combo/list box

 

 
Powered by phpBB® Forum Software