Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Populate Drop-Down Listbox from Query?

I have a database I can access through ODBC. I'm familiar with how to
populate the spreadsheet from the database, but would like to find a
way to populate a drop-down list with information from a table in the
database, *without* dropping the list onto the spreadsheet (i.e., if
this were a validation box, we'd access the query info directly instead
of referencing a group of cells).

I imagine this can be done with some programming but its a weak point
of mine - can anyoen help?

O.Ben.D.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Populate Drop-Down Listbox from Query?

Do you know how to use Event Procedures? If not, search here and read up on
them.

The Query result table is a QueryTable object, which has an AfterRefresh
event. You can build an event procedure using AfterRefresh to populate the
combobox. The QueryTable.ResultRange gives you a way to access your
querytable (and so you can know the exact number of rows in it after the
refresh). Then the combo's .ListFillRange can be filled with whichever cells
you need.
--
- K Dales


"Oggie Ben Doggie" wrote:

I have a database I can access through ODBC. I'm familiar with how to
populate the spreadsheet from the database, but would like to find a
way to populate a drop-down list with information from a table in the
database, *without* dropping the list onto the spreadsheet (i.e., if
this were a validation box, we'd access the query info directly instead
of referencing a group of cells).

I imagine this can be done with some programming but its a weak point
of mine - can anyoen help?

O.Ben.D.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Populate Drop-Down Listbox from Query?

Upon reread realized that you want to bypass the querytable alltogether?

This is a little more advanced. Use ADO. Need to add reference to
Microsoft ActiveX Data Objects to your project. You will also need to know
the command string to connect to your database; if you do not know it already
look at a querytable built off your database and in VBA look at its .Connect
property, which gives you the connection string.

The basic method to read the database table:

Sub GetMyData()
Dim MyDB as ADODB.Connection, MyRS as ADODB.Recordset
Dim SQLStr as String
Set MyDB = New ADODB.Connection
MyDB.Open(ConnectionString)
Set MyRS = New ADODB.RecordSet
With MyRS
.CursorLocation = adUseClient
' Modify the SQL String to retrieve the columns you want from your
table
' * in SQL means all columns; use SELECT Name, Code, .... FROM MyTable
' if you need to specify the columns
SQLStr = "SELECT * FROM MyTable"
.Open SQLStr, MyDB, adOpenDynamic, adLockReadOnly
' The above performed the query and stored the result in MyRS
' MyRS.RecordCount gives the number of records returned
If .RecordCount = 0 then
' No records; abort processing
Else
For i = 1 to RecordCount
ActiveSheet.ComboBox1.AddItem .Fields(0)
' Fields(0) above means column 1 - I think it is a
zero-based index?
' sorry - been a while and need to get going here!
Next i
End If
.. Close
End With

MyDB.Close
End Sub

Well, have to go but hope you got the idea!
--
- K Dales


"K Dales" wrote:

Do you know how to use Event Procedures? If not, search here and read up on
them.

The Query result table is a QueryTable object, which has an AfterRefresh
event. You can build an event procedure using AfterRefresh to populate the
combobox. The QueryTable.ResultRange gives you a way to access your
querytable (and so you can know the exact number of rows in it after the
refresh). Then the combo's .ListFillRange can be filled with whichever cells
you need.
--
- K Dales


"Oggie Ben Doggie" wrote:

I have a database I can access through ODBC. I'm familiar with how to
populate the spreadsheet from the database, but would like to find a
way to populate a drop-down list with information from a table in the
database, *without* dropping the list onto the spreadsheet (i.e., if
this were a validation box, we'd access the query info directly instead
of referencing a group of cells).

I imagine this can be done with some programming but its a weak point
of mine - can anyoen help?

O.Ben.D.


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
Populate a ListBox JB in Kansas Excel Programming 6 September 19th 05 03:01 PM
populate listbox? Alen32 Excel Programming 2 June 4th 05 01:18 PM
populate listbox JSnader Excel Programming 2 December 6th 03 02:43 PM
Populate listBox Hamster Excel Programming 1 September 17th 03 01:14 PM
ListBox Populate Rod Taylor Excel Programming 3 July 22nd 03 12:11 AM


All times are GMT +1. The time now is 12:43 PM.

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"