Relationship between datacombo and label
Here is the scenario,
i have a table containing two data field - ProductName and UnitPrice.
How can the UnitPrice of a particular product being automatically displayed on a Label.caption whenever i select a ProductName from dataCombo?
Please let me know as detail as possible since i'm just a VB beginner. Thank u.
[331 byte] By [
qiaojun] at [2007-11-15 16:16:21]

# 1 Re: Relationship between datacombo and label
DataCombo1 contents ProductName
Label1 - UnitPrice
private Sub DataCombo1_Click(Area as Integer)
Dim conAdo as new ADODB.Connection
Dim rsAdo as new ADODB.Recordset
conAdo.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\biblio.mdb;Persist Security Info=false"
rsAdo.Open "Select UnitPrice from ProductTable where ProductName='" & DataCombo1.Text & "'"
Label1.Caption = rsAdo.Fields(0).Value
End Sub
You can replace string
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\biblio.mdb;Persist Security Info=False" on connection string to your database.
Tower at 2007-11-10 0:59:19 >

# 2 Re: Relationship between datacombo and label
it didn't work at all. I changed your code to this:
private Sub DataCombo_Click(Index as Integer, Area as Integer)
'i have multiple datacombo
Dim conAdo as new ADODB.Connection
Dim rsAdo as new ADODB.Recordset
conAdo.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=IMS2.mdb;Persist Security Info=false"
rsAdo.Open "Select UnitPrice from Products where ProductName='" & DataCombo(Index).Text & "'"
Label1.Caption = rsAdo.Fields(0).Value
End Sub
an error message says," Run-time error '3709': Operation is not allowed on an object referencing a closed or invalid connection."
What's that? In the form, it contains a datacombo, a label, and a adodc. Anything need to set in their poperties before using your code?
# 3 Re: Relationship between datacombo and label
Correct line:
rsAdo.Open "Select UnitPrice from Products where ProductName='" & DataCombo(Index).Text & "'", conAdo, adOpenForwardOnly, adLockReadOnly, adCmdText
Tower at 2007-11-10 1:01:17 >

# 4 Re: Relationship between datacombo and label
Thank you so much. I've spent a lot of time on this problem, still doesn't have result, until you provide me the solution.
Yes, after changing the code, it works perfectly! But one more problem arises. I can only click on the first datacombo1(containing product name) and display the corresponding unit price. When the second datacombo1is clicked, it displays such error message:-
Run-time error '3021':
Either BOF or EOF is true, or the current record has been deleted. Requested operation requires a current record.
Do u have any idea?
# 5 Re: Relationship between datacombo and label
Change sub
private Sub DataCombo_Click(Index as Integer, Area as Integer)
Dim conAdo as new ADODB.Connection
Dim rsAdo as new ADODB.Recordsetcon
Ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=IMS2.mdb;Persist Security Info=false"
rsAdo.Open "Select UnitPrice from Products where ProductName='" & DataCombo(Index).Text & "'", conAdo, adOpenForwardOnly, adLockReadOnly, adCmdText
Label1.Caption = rsAdo.Fields(0).Value
rsAdo.Close
set rsAdo = nothing
Ado.Close
set Ado = nothing
End Sub
Test this code and write me about result
Tower at 2007-11-10 1:03:13 >

# 6 Re: Relationship between datacombo and label
Accroding to your code, is "Ado" in line 4, 9 & 10 referred to conAdo? If not, it will generate error message. When i correct them, it is back to normal.
After correcting the code, it looks like:
private Sub DataCombo_Click(Index as Integer, Area as Integer)
Dim conAdo as new ADODB.Connection
Dim rsAdo as new ADODB.Recordset
conAdo.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;Persist Security Info=false"
rsAdo.Open "Select UnitPrice from Products where ProductName='" & DataCombo(Index).Text & "'", conAdo, adOpenForwardOnly, adLockReadOnly, adCmdText
Label1(Index).Caption = rsAdo.Fields(0).Value
rsAdo.Close
set rsAdo = nothing
conAdo.Close
set conAdo = nothing
End Sub
But the same problem still persists, it highlights the line "Label1(Index).Caption = rsAdo.Fields(Index).Value" when i click on any datacombos. Is something wrong with "rsAdo.Fields(Index).Value"?
"Dim conAdo As New ADODB.Connection" - Does ADODB refer to adodc? What if there are more than one adodc existing in the same form?
I've sent you my program to your email a/c (aag@nkaz.kemerovo.su). Pls take a look.
# 7 Re: Relationship between datacombo and label
did not read all the posts, so please do not shot at me if you already know this
>>Dim conAdo As New ADODB.Connection
means:
add a reference to Microsoft Activex Data Objects 2.5 Library
via menu->project->references
(you may have 2.0 to 2.7)
Adodb refers to that library.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood,
Bruno Paris and all the other wonderful people who made and make dev-archive
a great place.
Come back soon, you Gurus.
The Rater
# 8 Re: Relationship between datacombo and label
Accroding to your code, is "Ado" in line 4, 9 & 10 referred to conAdo? If not, it will generate error message. When i correct them, it is back to normal.
After correcting the code, it looks like:
private Sub DataCombo_Click(Index as Integer, Area as Integer)
Dim conAdo as new ADODB.Connection
Dim rsAdo as new ADODB.Recordset
conAdo.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;Persist Security Info=false"
rsAdo.Open "Select UnitPrice from Products where ProductName='" & DataCombo(Index).Text & "'", conAdo, adOpenForwardOnly, adLockReadOnly, adCmdText
Label1(Index).Caption = rsAdo.Fields(0).Value
rsAdo.Close
set rsAdo = nothing
conAdo.Close
set conAdo = nothing
End Sub
But the same problem still persists, it highlights the line "Label1(Index).Caption = rsAdo.Fields(Index).Value" when i click on any datacombos. Is something wrong with "rsAdo.Fields(Index).Value"?
"Dim conAdo As New ADODB.Connection" - Does ADODB refer to adodc? What if there are more than one adodc existing in the same form?
I've sent you my program to your email a/c (aag@nkaz.kemerovo.su). Pls take a look.
