How do you update text box from combo box 
Author Message
 How do you update text box from combo box

Here is my code to update my combo box from an access 2.0 db. I can update
the combo box just fine with all records from the access db.

My problem is, how do I select an entry in my combo box and have
a separate text box output a record from the database that I selected.

In other words, I would like to click on the combo box to identify my
records and then when I select an entry in the combo box, I would like
to pull that record up in a separate text box or many text boxes off the
database.

So, If I clicked on the combo box and saw a bunch of cars ie
73 Toyota camry, 82 Volkswagon etc. I could select the Camry and
text boxes would display all the features of the particular Camry.
Textbox 1 may be the color, textbox 2 would be the gas mileage etc.
and all the text box records would come from the access database.

Here is my existing code for loading the combo box which works great.I
just need to figure out the next procedure.

========================================================================
Sub Form_Load ()
'Loads projects into combo box

Dim Vbdb As database
Dim pds As Dynaset, t As table
Dim pdsSort As Dynaset

Set Vbdb = OpenDatabase("c:\access\VB.MDB")
Set t = Vbdb.OpenTable("VBtest")

Set pds = t.CreateDynaset()

pds.Sort = "[Projectid] Asc"
Set pdsSort = pds.CreateDynaset()

'**----------------------------------------------**
'** LOAD COMBOBOX                                **
'**----------------------------------------------**

Dim key, nm, dt As String

Dim ProjectRow
'Dim Projectid As Integer

Projectid = pdsSort("Projectid") 'initialize project on form load

Do Until pdsSort.EOF
'Projectid = pdsSort("Projectid")

    key = pdsSort("projectid")
    nm = pdsSort("project")
    dt = pdsSort("date")

    ProjectRow = key & " " & nm & " " & dt

cboProject.AddItem ProjectRow

pdsSort.MoveNext

Loop

cboProject.ListIndex = 0

End Sub



Tue, 09 Jun 1998 03:00:00 GMT  
 How do you update text box from combo box

Quote:

>Here is my code to update my combo box from an access 2.0 db. I can update
>the combo box just fine with all records from the access db.
>My problem is, how do I select an entry in my combo box and have
>a separate text box output a record from the database that I selected.
>In other words, I would like to click on the combo box to identify my
>records and then when I select an entry in the combo box, I would like
>to pull that record up in a separate text box or many text boxes off the
>database.
>So, If I clicked on the combo box and saw a bunch of cars ie
>73 Toyota camry, 82 Volkswagon etc. I could select the Camry and
>text boxes would display all the features of the particular Camry.
>Textbox 1 may be the color, textbox 2 would be the gas mileage etc.
>and all the text box records would come from the access database.

>Here is my existing code for loading the combo box which works great.I
>just need to figure out the next procedure.
>========================================================================
>Sub Form_Load ()
>'Loads projects into combo box
>Dim Vbdb As database
>Dim pds As Dynaset, t As table
>Dim pdsSort As Dynaset
>Set Vbdb = OpenDatabase("c:\access\VB.MDB")
>Set t = Vbdb.OpenTable("VBtest")
>Set pds = t.CreateDynaset()
>pds.Sort = "[Projectid] Asc"
>Set pdsSort = pds.CreateDynaset()
>'**----------------------------------------------**
>'** LOAD COMBOBOX                                **
>'**----------------------------------------------**
>Dim key, nm, dt As String
>Dim ProjectRow
>'Dim Projectid As Integer
>Projectid = pdsSort("Projectid") 'initialize project on form load
>Do Until pdsSort.EOF
>'Projectid = pdsSort("Projectid")
>    key = pdsSort("projectid")
>    nm = pdsSort("project")
>    dt = pdsSort("date")

>    ProjectRow = key & " " & nm & " " & dt
>cboProject.AddItem ProjectRow
>pdsSort.MoveNext
>Loop
>cboProject.ListIndex = 0
>End Sub

Your best bet is to take an appropriate  value from the combo box and
use it as a condition for a SQL  WHERE clause or as a filter for a
recordset.   One place to do this is on the Click event of the combo
box.

One potential problem is getting the correct data from the combo box
to use as a SQL WHERE.  I assume that the projectid field is the value
you need to return the appropriate records from the database upon
selection.  If you're using the combo box that comes with VB, it may
be easier to use itemdata property to store the projectid.  Then it's
easy to get the value you need to query the database for the
appropriate records.  Here's an example of how to use itemdata:

'VB 4.0
'Note VB 3.0 is similar
'Set Applications combobox snapshot
    sSQL = "Select * from Applications;"    
'Load Apps combo box, storing the AppID for each selection in the
'Combos ItemData property
    Set ssApp = d.OpenRecordset(sSQL, dbOpenSnapshot)
    If Not (ssApp.BOF And ssApp.EOF) Then
        Do Until ssApp.EOF
            cboApps.AddItem ssApp!Application
            cboApps.ItemData(cboApps.NewIndex) = ssApp!appid
            ssApp.MoveNext
        Loop
    End If

Doing something like this would make getting the projectid easy to
return.  Once you have this value, you can write a SQL WHERE clause to
return only the records with,  say , the same projectid.  These
records can then be used to populate the text boxes.  Here's a sample
that takes the value from one combo box (stored in the itemdata
property of the combo box) and fills another box based on the value:

'VB 4.0
'VB 3.0 would be similar
'Note cboApps (combo box) is filled from previous procedure.

Private Sub cboApps_Click()

Dim sFilter As String, ssFilCat As Recordset

On Error GoTo Err_cboApps_Click

    screen.mousepointer = 11

'Build WHERE condition based on value in cboApps
    sFilter = "[AppId] = " & cboApps.ItemData(cboApps.ListIndex) & ";"

    Set ssFilCat = gDB.OpenRecordset("SELECT * FROM Categories _
WHERE " & sFilter, dbOpenSnapshot)

'Fill another combo box with the filtered data    
    If Not (ssFilCat.BOF And ssFilCat.EOF) Then
        Do Until ssFilCat.EOF
            cboCategory.AddItem ssFilCat!Category
            cboCategory.ItemData(cboCategory.NewIndex) =
ssFilCat!category_id
            ssFilCat.MoveNext
        Loop
    End If

    screen.mousepointer = 0
    ssFilCat.Close
    Set ssFilCat = Nothing
Err_cboApps_Click:
    MS False

    Select Case Err.Number
    Case 0
    Case Else
        ErrorHandler Err.Number, Err.Description, "cboApps_Click"
    End Select
    Err.Clear

End Sub

Hope this helps....

Mark



Wed, 17 Jun 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Vb 4.0 Problems updating Text Boxes from Combo Box linked to Data Control

2. Limiting Access to Text Boxes, List Boxes and Combo Boxes

3. update text box with a timer whilst editing same text box

4. Updating list boxes and combo boxes

5. Updating Text Fields from a Combo Box

6. Unable to populate text box from combo box

7. Putting a value from a combo box into a text box on a form

8. Filling combo box text box area from data

9. Created Form in Word - Irregular movement of combo boxes, text boxes on scrolling

10. Adding to a combo box or text box through the keyboard

11. Combo Boxes and Text Boxes

12. Combo Box and Text Box - Plz Help

 

 
Powered by phpBB® Forum Software