#1
|
|||
|
|||
I created a listbox in a UserForm. The list needs to contain 8 columns and is filled with data from access.
The code seems to work: all 8 columns show, the data is correct and I can use all data in textboxes and in the document. The only strange thing is that with 3 columns or more in the listbox I get an error message. With 2 columns no error message. Anyone an idea? Code:
Private Sub UserForm_Initialize() On Error GoTo UserForm_Initialize_Err Dim cnn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim i As Integer cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Users\Laptop Karin\Desktop\Test met adressen.mdb" rst.Open "SELECT [Bedrijf], [Achternaam],[Voorletters], [Adres], [ID] FROM NAW_Query ORDER BY [Bedrijf];", _ cnn, adOpenStatic rst.MoveFirst i = 0 With Me.ListBox1 .Clear Do .AddItem .List(i, 0) = rst![Bedrijf] .List(i, 1) = rst![Achternaam] .List(i, 2) = rst![Voorletters] .List(i, 3) = rst![Adres] .List(i, 4) = rst![ID] i = i + 1 rst.MoveNext Loop Until rst.EOF End With UserForm_Initialize_Exit: On Error Resume Next rst.Close cnn.Close Set rst = Nothing Set cnn = Nothing Exit Sub UserForm_Initialize_Err: MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!" Resume UserForm_Initialize_Exit End Sub Last edited by macropod; 02-27-2016 at 05:34 PM. Reason: Added code tags & formatting. Posts merged. |
#2
|
|||
|
|||
Without access to you database it is hard to say. However, I don't see why you need to AddItems one at a time. You might find this useful:
http://gregmaxey.com/word_tip_pages/...functions.html |
#3
|
|||
|
|||
Thanks Greg for your help.
It looks like a problem with the database, thanks for the hint :-) . I'll try to solve that first! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA: Selecting columns from multiple tables | LouterMattheus | Excel Programming | 1 | 07-22-2015 06:33 AM |
SUMIF not working across multiple columns | SteveBump | Excel | 3 | 05-14-2015 07:06 AM |
Link listbox (with multiple columns) to bookmark based on selection | dsjk9190 | Word VBA | 5 | 01-29-2015 11:28 PM |
This is a Userform LIstbox queston: A variable does not set to the value of a listbox | CatMan | Excel Programming | 14 | 08-18-2014 08:14 PM |
This is a Userform LIstbox queston: A variable does not set to the value of a listbox | CatMan | Excel | 1 | 08-08-2014 09:41 AM |