I'm having trouble using a DBList control 
Author Message
 I'm having trouble using a DBList control

i am a humble high school student who is doing my best to write a
simple "phone book" program. i have not had much experience coding in
visual basic and prior to learning this language i have only toyed
with QBasic. my project consists of a single form which contains all
the text boxes necessary to display each field in a single record. i
have managed to follow the help files and a few books which i have on
visual basic to bind these objects to a data control and i can step
through the records easily enough. i have spent weeks on my computer
late into the night trying to figure out how i could list all of the
records in a single list box in the form "Last name, First name". i
have managed to list all the records in a standard list box but can
not figure out how to select a particular record by clicking it. on my
quest to find out how to do this i came upon the DBList control. this
seems a little more usefull in my situation however, i can not even
get the records to list into this DBlist control as the loading syntax
is different. also i have multiple last names in this phone book so
the first name is required in the list box.

i realise that this is not a difficult task and from what i have read
i understand basically what it is that i'm required to do. BUT, i
simply can not find the correct syntax, if some one could throw me a
line or two of code that could help me or even just tell me of a place
where i can find information with examples i would be greatly
appreciative. thanks in advance. and sorry about the spelling :)

John Elliot.

p.s. after i figure out this little problem i'm going to have a whole
new one of trying to search for a record!! but i can get to that later
(search capability is not really required, especially if the names in
the list box can be sorted.)

this project is just to help me learn about visual basic and i think
it would solve my phone book problem. atm all of my freinds names are
scraweld on little bits of paper which are kept in my top draw!! not
very efficient.

thanks again. J.E.

Wed, 26 Dec 2001 03:00:00 GMT
 I'm having trouble using a DBList control

oops, i forgot to mention that the database is an Access 7 one and
that i'm using the standard data control (which shall remain visible
on the form). just incase someone need to know that.

thanks again. John.

Wed, 26 Dec 2001 03:00:00 GMT
 I'm having trouble using a DBList control


> i am a humble high school student who is doing my best to write a
> simple "phone book" program. <snip>

  A humble h.s. student - now there's something we don't see every day!
I'm impressed that you're teaching yourself VB - I'm self-taught, but a
little beyond high school.  Yeah, I'm a geezer as well as a geek.
  You never said what ver of VB you're using.  I work in VB6, and I'm
guessing you have an older ver?  So I'd use an ADO DataControl and an
ADO Grid for this application - you might have a DataGrid available in
your VB Toolbox that you could put on the form and bind to the
  Why not display the whole record (names and phone #) all at once?
Then all the info you need is visible.  Do you want the extra step (pick
a name, then display phone #) just as a learning process? 'Cause that's
cool, but with such a brief record, the extra step would not be a common
way to work in "the real world."  Whatever.
  I'll walk you through a process, but I set this up very quickly using
only one name - not two separate fields for first and last names.  I
suggest you try this using just the last name in your database, then
expand on it to work with two name fields instead of one.  It's not that
{*filter*}ce ya got the basic idea, but I'm not gonna spend the time on it
  So who needs the DBList control?  You said you already got all your
names to be displayed in the standard ListBox.  Now you want to do
something when one is selected.  I suggest using the DoubleClick event
rather than the Click - just to be sure the user REALLY meant to select
that name.  Here's some code that works; paste this into the DblClick
event of the ListBox (which I named List1, so substitute your ListBox's
name if different):

Dim strName As String, i

 For i = 0 To List1.ListCount - 1
    If List1.Selected(i) Then
    strName = List1.Text
    MsgBox "selected item is " & List1.Text  
    End If
 Next i

  The way it works:  Each item in the ListBox has an index that tells VB
where it is on the list.  The first name on list is index(0), next is
index(1), etc.  This code starts with the name at index(0) and looks
through the list.  If the name is Selected, it notes that name and puts
it into a string called "strName" - then, to let you knw it has the
correct name, it shows a message box to tell you what name was selected.
  Now, since strName is the Name of the person we want, let's get their
phone number.  Add the code below right after our code that got the
strName - You'll need to replace the terms I used with your actual terms
- Data1 is my DataControl, PhNum is the field in my table that holds the
PhoneNumber, Friends is the name of table in database that has all the
names and numbers.

 With Data1
'make sure next line is all on ONE line in your code
 .RecordSource = "SELECT PhNum FROM Friends WHERE name = '" & strName &

 MsgBox "Phone Number is " & .Recordset!PhNum
 End With

  The way it works: we change the data that the DataControl gets for us
by telling it to select only the phone number for the person who's name
is in strName.  Since you originally designed the DataControl to get
different info, we say "Refresh" to tell the DataControl to actually go
get this new data.  Then, the message box displays the phone number.

  Keep at it, and have fun - VB is a very useful skill.  And in
September, you'll be able to write a great essay on "How I Spent My
Summer Vacation"  By the way, I highly recommend Peter Wright's book
"Beginning Visual Basic 6" on Wrox Press.  He has earlier versions of
the book for VB5, 4...  One of the projects he shows you is a very handy
database viewer - you'll learn a lot about retrieving and updating info
from that chapter.
  If you have other questions, lemme know.
Jim in Cleveland
If you're writing to me, in my address
change "REAL_Address.see.below" to "worldnet.att.net"

"What's so funny 'bout peace, love & understanding?"
     - Nick Lowe

Wed, 26 Dec 2001 03:00:00 GMT
 I'm having trouble using a DBList control
On Sat, 10 Jul 1999 14:16:23 -0400, Jim in Cleveland


>  You never said what ver of VB you're using.  I work in VB6, and I'm
>guessing you have an older ver?  
correct. vb5.
>  Why not display the whole record (names and phone #) all at once?

i am displaying the whole record at once. the list box exists so that
i can select a record i'm looking for quickly. the list box is on the
left of the screen and all the databound txt boxes are on the right.
there are 17 fields in each record so i won't list them but they are
just your average phonebook kind. D.O.B, Phone, etc.
>Then all the info you need is visible.  Do you want the extra step (pick
>a name, then display phone #) just as a learning process?

not really, i can manage that. (especially now that you've given me
the bit of code i would need :)

from the code i was given i came up with this:

Private Sub lstListBox_DblClick()

Dim strName As String, i, strTemp As String

strTemp = lstListBox.Text

i = 1

While chrTemp <> ","
   chrTemp = Mid(strTemp, i, 1)
   i = i + 1

strLastName = Mid(strTemp, 1, i - 2)
strFirstName = Mid(strTemp, i + 1, Len(strTemp))

 With dtaPhoneBook
 .RecordSource = "SELECT Ph1 FROM PhoneBook WHERE LastName = '" &
strLastName & "' AND FirstName ='" & strFirstName & "'"
 End With

End Sub

This code works to update the txtbox which is bound to the Ph1 (phone
number) field. when i double click on a name in the listbox. but it
only updates this field and all other fields are left blank. also
after using the list box i am unable to use the data control to move
through the records. and if i try to add a new record it says "no
current record selected". do i have to use the SELECT statement for
every field in the record? or is there an easier way to point the
datacontrol to the record?

Sat, 29 Dec 2001 03:00:00 GMT
 [ 4 post ] 

 Relevant Pages 

1. I'm having trouble using the same table in two forms

2. trouble with linking DBList controls to my database.

3. Trouble with dblist controls

4. Please help: I'm having trouble with a trigger

5. I'm having troubles with -19523

6. I'm having trouble inserting text in a memo field in SQL Server 6.5

7. I'm having trouble with this sql query

8. I'm having somme trouble to use the applyfilter command

9. DBList Control Problem, please help me I'm an idiot

10. I'm having trouble getting connected

11. Who's having Trouble with System 10?

12. BDE and Win95 - I'm having trouble

Powered by phpBB® Forum Software