Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box value List

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
Is it possible to have a value list for a combo box in VB..

I am moving my databases to SQL and using VB as the front end. I want a combo box to be linked to a certain field and give the user certain choices to choose from..

Can someone help?

Thanks DVannoy
A+,Network+,CNA
dvannoy@onyxes.com
 
Not really sure what you are trying to do, could you explain a bit more.
Alternatively have you experimented with the DataList controls, maybe they will help do what you want?
 
what im trying to do is, have a datacombo box on a form thats linked to a DB. when the users drops down the combobox there are only certain choices the person can choose from. once they choose the data then gets writen into that field. Like in access, with a combo box you can have a value list and list the items to choose from. that way you donot have to link the combo box to a table. if i have to create tables in SQL and then link the datacombo box to that, i guess that would be OK. I am just trying to duplicate the same things i have done in access so i donot have to waste time with more developmnt and testing.

any input would be great.

Thanks DVannoy
A+,Network+,CNA
dvannoy@onyxes.com
 
I think this is what u want.

Create the COMBOBOX, under the properties

Under the CATEGORIZED

Go To LIST
Option: List (List)

Click on the dropdown box, type in the Option u want to assign, NOTE: after each option press (Ctrl + Enter) to confirm/save the option on the list.
 
This is a simple class that I wrote to do exactly what I think you want. It does the same thing that selecting "table/query" under lookup does in Access. It's for an application I wrote for physicians to track their surgical patients. In addition, the .Distinct property allows you to search from a table with existing data and make any previous entries placed into the combobox. It also works for listboxes. The code is ready to go, just change the connection string and put the following into a new class module titled "clsPopulateBox." Then, in the form the contains the combobox, place this code.

-----------------------------Form Code---------------------
Dim obj As clsPopulateBox

Set obj = New clsPopulateBox

obj.distinct = False
obj.listsource = "tblAdditionalOrganList"
obj.listfield = "organ"
obj.populatelist comboboxname

----------------------Class Code-----------------------

Option Explicit
Const ConnectString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\SurgOnc\Oncology\oncology.mdb;Persist Security Info=False"

Private m_listsource As String 'local copy
Private m_listfield As String 'local copy

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Private m_distinct As Boolean

Public Property Let distinct(ByVal vData As Boolean)
m_distinct = vData
End Property


Public Property Get distinct() As Boolean
Set distinct = m_distinct
End Property


Public Sub populatelist(controlbox As Control)
Set rst = New ADODB.Recordset
rst.ActiveConnection = cnn
rst.CursorType = adOpenForwardOnly
rst.LockType = adLockReadOnly

If m_distinct Then
rst.Source = "SELECT DISTINCT "
Else
rst.Source = "SELECT "
End If

rst.Source = rst.Source & m_listfield & " FROM " & m_listsource & " ORDER BY " & m_listfield & ""
rst.Open

If rst.RecordCount Then
Do While Not rst.EOF
If rst(m_listfield) <> &quot;&quot; Then controlbox.AddItem rst(m_listfield)
rst.MoveNext
Loop
End If

Set rst = Nothing

End Sub


Public Property Let listfield(ByVal vData As String)
m_listfield = vData
End Property


Public Property Get listfield() As String
Set listfield = m_listfield
End Property


Public Property Let listsource(ByVal vData As String)
m_listsource = vData
End Property


Public Property Get listsource() As String
listsource = m_listsource
End Property


Private Sub Class_Initialize()

Set cnn = New ADODB.Connection
cnn.ConnectionString = ConnectString
cnn.Open

End Sub

Private Sub Class_Terminate()

Set cnn = Nothing
Set rst = Nothing

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top