Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Populating List Box from SQL Server

How do i populate a listbox from a recordset returned from SQL server. The
recordset will have three fields. I can populate the listbox from a single
field using transpose to swap the data around from getrows. But how do i do
this with a recordset of multiple fields ?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Populating List Box from SQL Server

Listbox1.ColumnCount = 3
Listbox1.List = Application.Transpose( your record set)

--
Regards,
Tom Ogilvy


"John Peach" wrote in message
...
How do i populate a listbox from a recordset returned from SQL server. The
recordset will have three fields. I can populate the listbox from a single
field using transpose to swap the data around from getrows. But how do i

do
this with a recordset of multiple fields ?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Populating List Box from SQL Server

That doesn't work, I get a Type Mismatch error, here my coe if that helps:
Dim cn As ADODB.Connection

Dim rs As ADODB.Recordset

Dim vArray() As Variant

Dim sql As String

Set cn = New ADODB.Connection

cn.ConnectionString = "Provider=SQLOLEDB.1;Password=lizarduser;Persi st

Security Info=True;User ID=lizarduser;Initial Catalog=Thorn_Lizard;Data

Source=UKSPTHDB01"

cn.Open

sql = "SELECT Name AS [Company Name], PostCode AS [Post Code],

PostCodeSuffix AS Suffix "

sql = sql & "FROM TblBranch INNER JOIN TblCompany ON TblBranch.CompanyId =

TblCompany.Id "

sql = sql & "WHERE (PostCodeSuffix LIKE '%crown%') OR (PostCode LIKE

'%crown%') OR (Name LIKE '%crown%')"

Set rs = New ADODB.Recordset

rs.Open sql, cn, adOpenStatic, adLockReadOnly

If Not (rs.BOF And rs.EOF) Then

vArray = rs.GetRows

rs.Close

ListBox1.ColumnCount = 3

ListBox1.List = Application.Transpose(vArray)


End If


"Tom Ogilvy" wrote in message
...
Listbox1.ColumnCount = 3
Listbox1.List = Application.Transpose( your record set)

--
Regards,
Tom Ogilvy


"John Peach" wrote in message
...
How do i populate a listbox from a recordset returned from SQL server.
The
recordset will have three fields. I can populate the listbox from a
single
field using transpose to swap the data around from getrows. But how do i

do
this with a recordset of multiple fields ?






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Populating List Box from SQL Server

If you array is larger than 5461 elements, then you would get that error.

If not, it should work.

If it is, then you will have to loop and transpose it yourself or use the
column method.

try

listbox1.Column = vArray

as a demonstration:

Private Sub UserForm_Initialize()
Dim v(1 To 3, 1 To 5)
For j = 1 To 3
For i = 1 To 5
v(j, i) = i & "," & j
Next
Next
ListBox1.ColumnCount = 3
ListBox1.Column = v
End Sub


--
Regards,
Tom Ogilvy


"John Peach" wrote in message
...
That doesn't work, I get a Type Mismatch error, here my coe if that helps:
Dim cn As ADODB.Connection

Dim rs As ADODB.Recordset

Dim vArray() As Variant

Dim sql As String

Set cn = New ADODB.Connection

cn.ConnectionString = "Provider=SQLOLEDB.1;Password=lizarduser;Persi st

Security Info=True;User ID=lizarduser;Initial Catalog=Thorn_Lizard;Data

Source=UKSPTHDB01"

cn.Open

sql = "SELECT Name AS [Company Name], PostCode AS [Post Code],

PostCodeSuffix AS Suffix "

sql = sql & "FROM TblBranch INNER JOIN TblCompany ON TblBranch.CompanyId =

TblCompany.Id "

sql = sql & "WHERE (PostCodeSuffix LIKE '%crown%') OR (PostCode LIKE

'%crown%') OR (Name LIKE '%crown%')"

Set rs = New ADODB.Recordset

rs.Open sql, cn, adOpenStatic, adLockReadOnly

If Not (rs.BOF And rs.EOF) Then

vArray = rs.GetRows

rs.Close

ListBox1.ColumnCount = 3

ListBox1.List = Application.Transpose(vArray)


End If


"Tom Ogilvy" wrote in message
...
Listbox1.ColumnCount = 3
Listbox1.List = Application.Transpose( your record set)

--
Regards,
Tom Ogilvy


"John Peach" wrote in message
...
How do i populate a listbox from a recordset returned from SQL server.
The
recordset will have three fields. I can populate the listbox from a
single
field using transpose to swap the data around from getrows. But how do

i
do
this with a recordset of multiple fields ?








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
populating list and formatting Matt Excel Discussion (Misc queries) 1 March 17th 10 08:25 PM
.AddItem list and populating combobox with created list pallaver Excel Discussion (Misc queries) 8 June 27th 08 12:36 PM
populating list box with contacts GeorgeFD29 Excel Programming 1 May 6th 05 05:48 PM
populating list Wazooli Excel Programming 4 March 7th 05 10:50 PM
Populating dropdown list 2 with data depending upon what was selected in list 1 karambos Excel Programming 2 November 9th 04 05:32 PM


All times are GMT +1. The time now is 06:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"