Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2016, 08:08 AM
Kaatje Kaatje is offline ListBox with multiple columns Windows 10 ListBox with multiple columns Office 2013
Novice
ListBox with multiple columns
 
Join Date: Feb 2016
Location: The Netherlands
Posts: 2
Kaatje is on a distinguished road
Default

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.
Reply With Quote
  #2  
Old 02-27-2016, 09:02 AM
gmaxey gmaxey is offline ListBox with multiple columns Windows 7 32bit ListBox with multiple columns Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-27-2016, 09:37 AM
Kaatje Kaatje is offline ListBox with multiple columns Windows 10 ListBox with multiple columns Office 2013
Novice
ListBox with multiple columns
 
Join Date: Feb 2016
Location: The Netherlands
Posts: 2
Kaatje is on a distinguished road
Default

Thanks Greg for your help.

It looks like a problem with the database, thanks for the hint :-) . I'll try to solve that first!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox with multiple columns VBA: Selecting columns from multiple tables LouterMattheus Excel Programming 1 07-22-2015 06:33 AM
ListBox with multiple columns 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
ListBox with multiple columns 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft