User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 391,913 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,740 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Views: 1610 | Replies: 2
Reply
Join Date: Jun 2007
Posts: 29
Reputation: shahriar2403 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
shahriar2403 shahriar2403 is offline Offline
Light Poster

Question How to search from a database, selecting from a combo or list box in vb?

  #1  
Jun 3rd, 2007
Let, i have a database in ms access with two field named-"name" & "roll". I also have a combo box or list box in vb containing those names. I have to select a name from that list and appear the corresponding "roll" from that database in a text box in vb form. How can i do it?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: India
Posts: 493
Reputation: choudhuryshouvi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 45
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro in Training

Re: How to search from a database, selecting from a combo or list box in vb?

  #2  
Jun 3rd, 2007
u need to first open the database and assign its table's contents to a recordset object. all these u have to do in form_load event. try this code snippet :-

'in general section
dim db as database,rs as recordset

'in form_load event
set db=opendatabase(app.path & "\yourdbname.mdb")
set rs=db.openrecordset("yourtablename",dbopentable)
if rs.recordcount>0 then rs.movefirst

'in listbox click event
set rs=db.openrecordset("select roll from yourtablename where name='" & list1.text & "'",dbopendybaset)
if rs.recordcount>0 then
text1.text=rs("roll")
endif
set rs=nothing
Reply With Quote  
Join Date: Apr 2007
Posts: 34
Reputation: pbrookee is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
pbrookee pbrookee is offline Offline
Light Poster

Re: How to search from a database, selecting from a combo or list box in vb?

  #3  
Jun 4th, 2007
In vb form_load() put in code to which will fill your listbox with the database info :
'your code here to connect to db
'sql line to select from db
' sql = ("Select * from db_name")
rec.Open sql, cn, adOpenKeyset, adLockPessimistic
While Not rec.EOF
Me.listbox.AddItem rec(1).Value
rec.MoveNext

'click and data shows up in text box
Private Sub listbox_Click()
Dim sql As String
'connect to db

For intXfor = 0 To Me.listbox.ListCount - 1
'when user is selected populate data from table into txtboxes
If Me.listbox.Selected(intXfor) = True Then

sql = ( _
"Select * from db_name where row = '" _
& Me.listbox.List(intXfor) & "'")

rec.Open sql, cn, adOpenKeyset, adLockPessimistic
'display data in textbox
Me.textbox_name.Text = rec(0).Value

rec.Close
Set rec = Nothing
cn.Close
End If
Next
End Sub
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 7:58 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC