Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2004, 03:33 AM
Authorized User
 
Join Date: Nov 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default combo box and textbox problem

Hi friends,

I used a combo box to populate the data from my table and I did it. My code goes like this. This code is written on the form load.

    rsCategory.MoveFirst
    Do Until rsCategory.EOF
        Combo1.AddItem rsCategory.Fields("CATEGORYNAME").Value
        rsCategory.MoveNext
    Loop

My problem is this. If I click the CATEGORYNAME in the combo box, I want the CATEGORYID will be written in the Text6.Text.

Ex: CATEGORYID CATEGORYNAME
      1 Analgesic
      2 Antipyritec

Hope u get my point.

Thank u

Rudner

'Text6.Text = rsCategory.Fields("CATEGORYID").Value


 
Old November 23rd, 2004, 09:09 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

There are two things:-
1) If CategoryName is unique in table, U can write a Function which takes CategoryName as parameter and returns CategoryID.

Like ....

In Click event of Combo

 Text6.text = GetCategoryID(Combo1.text)


Public Function GetCategoryID(ByVal CategoryName as String)

    Dim Rs as New ADODB.RecordSet
    Dim Sql As string
    Sql= "Select catID from table1 where CatName ='"
    Sql=Sql+trim(Combo1.text))+"'"

    Set Rs = Con.Execure (Sql)
    If Rs.Eof()=false then
         GetCategoryID = Rs("CateId")
     Else
         GetCategoryID = "" '''0 if numeric type
    Endif
    Rs.Close
    Set Rs = nothing
End Function
---------------------
And If Categoryname is not unique then
----------------------------


 rsCat.MoveFirst
    Do Until rsCat.EOF
        Combo1.AddItem rsCat("CATEGORYNAME")+"*"+RsCat("CatId")
        ''If Numeric type then use Cstr with CatID
        rsCategory.MoveNext
    Loop


In Click event of Combo

Dim TempArray

TempArray = Split(Trim(Combo1.Text),"*")
Text6.Text = Trim(TempArray(1))


Gud Luck

B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combo box to display items from parent combo box Gini Visual Studio 2008 0 June 18th, 2008 12:30 AM
Combo box choice creating filtered combo box stevensj5 Access 11 September 13th, 2007 11:33 AM
Combo box updating textbox needelp Access VBA 1 January 12th, 2007 12:26 PM
Problem with Combo Box ru1 Access 1 October 20th, 2006 06:32 AM
Combo box problem diegoblin Beginning VB 6 2 October 4th, 2006 05:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.