PDA

View Full Version : Listbox column headers from SQL recordset



cjmitton
06-30-2017, 04:01 AM
I'm using a custom form in Word 2016, getting data using ADODB from a MS-SQL 2014 database.

I'm trying to get either custom headers or the field names as column headers on a list box.

My current code is this:


rst.Open "SELECT Distinct AddressID, Name, Address1, PostCode FROM Address WHERE WorkType = '" & WrkType1 & "' AND AddType = '" & AddType1 & "'", cnt
If rst.EOF Then
Me.LstBxAdd.Clear
GoTo UserForm_Initialize_Exit
End If
rst.MoveFirst
With Me.LstBxAdd
.Clear
.ColumnHeads = True
Do
.AddItem rst![AddressID]
.List(iii, 0) = rst![AddressID]
.List(iii, 1) = rst![Name]
.List(iii, 2) = rst![Address1]
.List(iii, 3) = rst![PostCode]
iii = iii + 1
rst.MoveNext
Loop Until rst.EOF
.ColumnCount = 4
.ColumnWidths = "0;200;200;200"
.ListIndex = 0
End With


I want to either manually add the headers or use the recordset field headers. At the moment the column headers are blank.