Hi, I have a list box being populated from an external database. There are 6 columns in the list box. My code goes out to a database runs a query and populates a list box. I have the ColumnsHeads property set to true, it shows the column head boxes but it won't populate them. My code is as follows:
Can anybody please help me to get these coulmn heads to populate?
Thank you.
Code:
Private Sub UserForm_Initialize() Dim sdate As Date Dim fdate As Date fdate = ActiveProject.CurrentDate fdate = Format(fdate, "Ddddd") txtend.Value = fdate txtstart.SetFocus End Sub
Code:
Public Function test(sdate, fdate) As String
Dim cmd As ADODB.Connection
Dim rs As ADODB.Recordset
Dim qry As ADODB.Recordset
Dim prm As ADODB.Parameter
Dim prm1 As ADODB.Parameter
Dim strconn As String
Set cmd = New ADODB.Connection
strconn = "Provider= microsoft.ace.oledb.12.0;" & "Data Source = Z:\TRS Commercial\TRS Prod\data bases\parts status1.accdb"
cmd.Open strconn
Dim k As New ADODB.Command
Set qry = New ADODB.Recordset
Set rs = New ADODB.Recordset
cmd.CursorLocation = adUseClient
Dim tskdat() As String
Dim i As Integer
Dim objfield As ADODB.Field
Dim icol As Integer
Dim ifld As Integer
Dim n As Integer
Dim m As Integer
'opening and running the query
Set k.ActiveConnection = cmd
k.CommandType = adCmdStoredProc
k.CommandTimeout = 8000
k.CommandText = "updatetest"
Set prm = k.CreateParameter
prm.Type = adDate
k.Parameters.Append prm
k.Parameters(0).Value = sdate
Set prm1 = k.CreateParameter
prm1.Type = adDate
k.Parameters.Append prm1
k.Parameters(1).Value = fdate
Erase tskdat
qry.Open k
If Not qry.EOF Then qry.MoveLast
n = qry.RecordCount - 1
m = qry.Fields.Count - 1
ReDim tskdat(n, m)
qry.MoveFirst
' populating the list box
i = 0
Do Until qry.EOF
'objfield.Name
For icol = 0 To m
tskdat(i, icol) = qry.Fields(icol)
Next
i = i + 1
qry.MoveNext
Loop
lbdata.ColumnHeads = True
lbdata.List() = tskdat
cmd.Close
Set qry = Nothing
Set k = Nothing
Set cmd = Nothing
Set rs = Nothing
End Function
Thank you.
Comment