Cbrine
Well-known Member
- Joined
- Dec 2, 2003
- Messages
- 3,196
It's been a while since I last posted here, hope all the old chums are doing well. Ran into a small issue with a solution that I'm putting together here at work, and I'm hoping someone has encountered this issue and found a resolution so that I don't have to.
I'm trying to dynamically load data into an excel listbox control, and then using vba I'm changing the rowsource to the data added. The issue is that, even though I have the Headers turned on, when I dynamically change the row source, the headers are not appearing the heading until I close and then reopen the form. Once I reopen the form, eventhing looks perfect. I've tried to repaint the control and the form, and that doesn't seem to work. Hopefully someone can help me out a bit. Here's the code I'm currently using.
Thanks in advance!
cbrine
I'm trying to dynamically load data into an excel listbox control, and then using vba I'm changing the rowsource to the data added. The issue is that, even though I have the Headers turned on, when I dynamically change the row source, the headers are not appearing the heading until I close and then reopen the form. Once I reopen the form, eventhing looks perfect. I've tried to repaint the control and the form, and that doesn't seem to work. Hopefully someone can help me out a bit. Here's the code I'm currently using.
Code:
Private Sub CommandButton5_Click()
Dim oConn As New ADODB.Connection, rs As New ADODB.Recordset, rs2 As New ADODB.Recordset
Dim wb As Workbook, Twf As Worksheet
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"""
Set wb = ActiveWorkbook
Set Twf = wb.Sheets("Found Data")
'Search General Survey Title Values.
rs.Open "select F1, F2, F3, Len(F1) as lenF1 from [Job Information$A:C] where ucase(F2) like '%" & UCase(TextBox1.Text) & "%' and len(F1)>3;", oConn, adOpenStatic, adLockOptimistic
'Clear Current Found Data
If Twf.Range("A2") <> "" Then
Twf.Range("A2", Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(1, 20)).ClearContents
End If
Do Until rs.EOF
'Open recordset to detailed data for found Job code and populate found sheet for inclusion into listbox control
rs2.Open "select * from [Raw Data$A:EZ] where F3='" & rs(0) & "'", oConn, adOpenStatic, adLockOptimistic
Do Until rs2.EOF
Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(1, 0) = rs(0)
Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 1) = rs(1)
Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 2) = rs2(4)
Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 3) = rs2(5)
rs2.MoveNext
Loop
Set rs2 = Nothing
rs.MoveNext
Loop
ListBox1.RowSource = "='" & Twf.Name & "'!A1:" & Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 3).Address
Set rs = Nothing
Set oConn = Nothing
End Sub
Thanks in advance!
cbrine
Last edited: