Results 1 to 8 of 8

Thread: [RESOLVED] ListBox to TextBoxes

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Resolved [RESOLVED] ListBox to TextBoxes

    I'm using VB6 and an Access 2003 database.

    The database has a table named Clients that contains first name, last name, phone number, and fax number.

    On my form I have a ListBox which I have filled with the last name (ClLName) field from the database and several empty TextBoxes.

    When I click on the last name in the ListBox I would like to populate the TextBoxes with first name, last name, phone number and fax number that correspond to the last name selected in the ListBox.

    My ListBox is populated with code in the form load event and that works fine.
    The code below doesn't work to populate the TextBoxes. All I am getting is the information for the last record in the database.

    I am sure this is pretty simple, but I can't seem to make it work.

    I would appreciate any help you can give.

    Thanks

    VB Code:
    1. Private Sub ListBox1_Click()
    2.     Dim RS As ADODB.Recordset
    3.     Dim CN As ADODB.Connection
    4.    
    5. CString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6.           "Data Source=C:\LOMAP\LOMAPMAIN.mdb;"
    7.  
    8. Set RS = New ADODB.Recordset
    9. Set CN = New ADODB.Connection
    10. CN.ConnectionString = CString
    11. CN.Open
    12. RS.ActiveConnection = CN
    13. RS.Open "SELECT * FROM Clients"
    14.  
    15. Dim intN As Integer
    16. For intN = ListIndex - 1 To 20
    17.  
    18. Text1.Text = RS.Fields.Item("ClFName").Value
    19. Text2.Text = RS.Fields.Item("ClLName").Value
    20. Text3.Text = RS.Fields.Item("ClPhone").Value
    21. Text4.Text = RS.Fields.Item("ClFax").Value
    22.  
    23. Next intN
    24.  
    25. End Sub

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: ListBox to TextBoxes

    Your loop is the issue

    For intN = ListIndex - 1 To 20

    Text1.Text = RS.Fields.Item("ClFName").Value
    Text2.Text = RS.Fields.Item("ClLName").Value
    Text3.Text = RS.Fields.Item("ClPhone").Value
    Text4.Text = RS.Fields.Item("ClFax").Value

    Next intN

    It goes through the list and keeps overwriting what was there before.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: ListBox to TextBoxes

    Pasvorto:

    Yes, I know the loop is the problem.

    What I don't know is how to correct it.

    Thanks

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: ListBox to TextBoxes

    Do something like this:

    RS.Open "SELECT * FROM Clients where [cifname] = " & "'" & listindex.text & "'"

    Text1.Text = RS.Fields.Item("ClFName").Value

  5. #5
    New Member
    Join Date
    Nov 2005
    Location
    NC, US
    Posts
    5

    Re: ListBox to TextBoxes

    Quote Originally Posted by AIS4U
    When I click on the last name in the ListBox I would like to populate the TextBoxes with first name, last name, phone number and fax number that correspond to the last name selected in the ListBox.

    VB Code:
    1. Private Sub ListBox1_Click()
    2.     Dim RS As ADODB.Recordset
    3.     Dim CN As ADODB.Connection
    4.    
    5. CString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6.           "Data Source=C:\LOMAP\LOMAPMAIN.mdb;"
    7.  
    8. Set RS = New ADODB.Recordset
    9. Set CN = New ADODB.Connection
    10. CN.ConnectionString = CString
    11. CN.Open
    12. RS.ActiveConnection = CN
    13. RS.Open "SELECT * FROM Clients"
    This opens ALL records in the Table. If you only want the ones that correspond to the name selected in the ListBox, you need to do

    VB Code:
    1. RS.Open "SELECT * FROM Clients WHERE [clLName]  = '" & ListBox1.Text & "'"

    Unless you have multiple records with the same last name, you will not need to loop through the records. Just populate the TextBoxes with the records

    VB Code:
    1. Text1.Text = RS![ClFName]
    2. Text2.Text = RS![ClLName]
    3. Text3.Text = RS![ClPhone]
    4. Text4.Text = RS![ClFax]

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: ListBox to TextBoxes

    Pasvorto:
    Pianoman:

    Thanks guys, that works great, but it caused another problem.

    In some of the database records there is not a fax number. So when I click on a last name in the ListBox that does not have a fax number entered in the record I get an error message:
    Run-time error 94.
    Invalid use of null.
    The fax number field is set to text, but no information is entered into that field if the
    client does not have a fax number.

    Do you have any suggestions about how I can correct this problem?

    Thanks

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ListBox to TextBoxes

    Just add NULL to each record that may be null.
    VB Code:
    1. Text4.Text = RS![ClFax] & ""

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: ListBox to TextBoxes

    dglienna:

    Thanks a lot! That made it work.

    Acutally I had a problem at first, but I used your code suggestion in an
    If/Then statement and it works just fine.

    Thanks again.

    I'll mark this one Resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width