Re: Combo Box - How to populate from a table
From: MikeD (nobody_at_nowhere.edu)
Date: 06/15/04
- Next message: Veign: "Re: Opening Excel files"
- Previous message: Karl E. Peterson: "Re: about events and exceptions"
- In reply to: Lee Crisell: "Re: Combo Box - How to populate from a table"
- Next in thread: Robert Valentine: "Re: Combo Box - How to populate from a table"
- Reply: Robert Valentine: "Re: Combo Box - How to populate from a table"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 14 Jun 2004 20:17:04 -0400
"Lee Crisell" <leecrisell@coheecrisell.com> wrote in message
news:EcOdndNJAe-pe1DdRVn-jw@mminternet.net...
> Thanks for your help. Since you do not use data binding to populate combo
> boxes, how do you do it? I assume you do it with code.
Yep. Add a reference to either the DAO or ADO object library. Create a
connection to the database, open a recordset and retrieve the data, and loop
through the records in the recordset. Here's some example code using ADO.
You need to add a reference to "Microsoft ActiveX Data Objects 2.x Library".
You'll also need to change the SQL code to reflect field and table names in
your database, as well as the path and filename of the .mdb file.
-----BEGIN CODE
Option Explicit
Private Sub Form_Load()
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sSQL As String
Set oConn = New ADODB.Connection
With oConn
.Provider = "Microsoft.Jet.OLEDB.4.0"
'Path is hard-coded for example purposes only!
.Open "c:\my documents\db1.mdb"
End With
sSQL = "SELECT ID, ComboboxText FROM Table1 ORDER BY ComboboxText"
Set oRS = New ADODB.Recordset
oRS.Open sSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText
'You could also open the recordset this way, which opens the table
'and retrieves all rows in the table (same as the above SQL but the
'rows are not sorted)
' oRS.Open "Table1", oConn, adOpenForwardOnly, adLockReadOnly, adCmdTable
With oRS
If Not (.BOF And .EOF) Then
Do Until .EOF
Combo1.AddItem .Fields("ComboboxText").Value
Combo1.ItemData(Combo1.NewIndex) = .Fields("ID").Value
.MoveNext
Loop
Else
MsgBox "No records found"
End If
End With
CloseRecordset oRS
CloseConnection oConn
End Sub
Public Sub CloseConnection(oConn As ADODB.Connection)
If Not oConn Is Nothing Then
If (oConn.State And adStateOpen) = adStateOpen Then
oConn.Close
End If
Set oConn = Nothing
End If
End Sub
Public Sub CloseRecordset(oRS As ADODB.Recordset)
If Not oRS Is Nothing Then
If (oRS.State And adStateOpen) = adStateOpen Then
oRS.Close
End If
Set oRS = Nothing
End If
End Sub
-----END CODE
This might seem like more work and more code....and it is. But almost
everyone will tell you that it's 10 times better than using the data control
and bound controls. It gives you total and complete flexibility to do
exactly what you want or need (as opposed to data-binding, which can be very
restrictive).
Depending on your specific circumstances, you may want the oConn object
variable to be higher scope (module-level or possibly even global). In that
case, you probably wouldn't close the connection until your app closes.
There's debate as to whether it's best to open the connection and keep it
open for the life of the program, or to open and close the connection each
time you need to access the database. There's no definitive answer to this.
Which way is better depends on many factors.
You should never keep recordsets open any longer than what is necessary.
You also should know at least a little SQL to retrieve data and do inserts,
updates, and deletes. I know of nobody that writes "production code" that
uses ADO's properties and methods (like the Recordset object's Update
method) for this. It's best to use SQL, or if the DBMS (like SQL Server or
Oracle) supports them, stored procedures.
>
> My problem is that I want to allow the user to enter their own list and
then
> save it so it can be called again by the combo box. The same user will
have
> a different list in this combo box for the different companies that they
> will be accessing through the form.
>
> Does anyone use data from a column in a table to populate a combo box?
Sure. This is quite common.
> I usually program in Access VBA and it is very easy to use the contents of
a
> column in a table to populate a combo box. But I am at a loss in Visual
> Basic. (I'm using Visual Basic 6.0.)
>
Access and VB are two different products. There's no reason you should
expect them to "work" the same way or have the same set of features. The
combobox in Access is designed differently and has features specific to
databases and data access. The only thing VB and Access really have in
common is that they both use the same programming language engine, but there
are even enough differences in the language that not all your code can
simply be copied and pasted to the other (much of it can but not all of it).
Mike
- Next message: Veign: "Re: Opening Excel files"
- Previous message: Karl E. Peterson: "Re: about events and exceptions"
- In reply to: Lee Crisell: "Re: Combo Box - How to populate from a table"
- Next in thread: Robert Valentine: "Re: Combo Box - How to populate from a table"
- Reply: Robert Valentine: "Re: Combo Box - How to populate from a table"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|