
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