|
-
Dec 8th, 2005, 03:25 PM
#1
Thread Starter
Frenzied Member
[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:
Private Sub ListBox1_Click()
Dim RS As ADODB.Recordset
Dim CN As ADODB.Connection
CString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\LOMAP\LOMAPMAIN.mdb;"
Set RS = New ADODB.Recordset
Set CN = New ADODB.Connection
CN.ConnectionString = CString
CN.Open
RS.ActiveConnection = CN
RS.Open "SELECT * FROM Clients"
Dim intN As Integer
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
End Sub
-
Dec 8th, 2005, 03:33 PM
#2
PowerPoster
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.
-
Dec 8th, 2005, 03:46 PM
#3
Thread Starter
Frenzied Member
Re: ListBox to TextBoxes
Pasvorto:
Yes, I know the loop is the problem.
What I don't know is how to correct it.
Thanks
-
Dec 8th, 2005, 03:56 PM
#4
PowerPoster
Re: ListBox to TextBoxes
Do something like this:
RS.Open "SELECT * FROM Clients where [cifname] = " & "'" & listindex.text & "'"
Text1.Text = RS.Fields.Item("ClFName").Value
-
Dec 8th, 2005, 04:10 PM
#5
New Member
Re: ListBox to TextBoxes
 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:
Private Sub ListBox1_Click()
Dim RS As ADODB.Recordset
Dim CN As ADODB.Connection
CString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\LOMAP\LOMAPMAIN.mdb;"
Set RS = New ADODB.Recordset
Set CN = New ADODB.Connection
CN.ConnectionString = CString
CN.Open
RS.ActiveConnection = CN
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:
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:
Text1.Text = RS![ClFName]
Text2.Text = RS![ClLName]
Text3.Text = RS![ClPhone]
Text4.Text = RS![ClFax]
-
Dec 8th, 2005, 04:58 PM
#6
Thread Starter
Frenzied Member
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
-
Dec 8th, 2005, 05:00 PM
#7
Re: ListBox to TextBoxes
Just add NULL to each record that may be null.
VB Code:
Text4.Text = RS![ClFax] & ""
-
Dec 8th, 2005, 05:37 PM
#8
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|