virgilar
asked on
ItemData in visual basic 2005
Below is my code:
ByVal sDisplayfield As String, Optional ByVal sDatafield As String = "")
Dim i As Integer
'On Error GoTo EH
If Not rs.EOF Then rs.MoveFirst()
cmb.Items.Clear()
i = 0
While Not rs.EOF
this line works just fine: cmb.Items.Insert(i, rs.Fields(sDisplayfield).V alue)
it display the name of the state
My problem is right here: I am trying to store the stateid in the same combobox. Visual basic 6.0 had an itemdata for the drop down box but 2005 does not have. I have seen a solution from Microsoft but cannot apply it to my concept below.
Any help is welcomed. Again, the code works fine. I just need to find a way to store the stateid in the dropdown.
Thanks Andre
Problematic line: 'If (Len(sDatafield) > 0) Then VB6.SetItemData(cmb, i, Val(rs.Fields(sDatafield). Value))
i = i + 1
rs.MoveNext()
End While
Exit Sub
EH:
Err.Description = "LoadCombobox-> " & Err.Description
Err.Raise(Err.Number, Err.Source, Err.Description)
ByVal sDisplayfield As String, Optional ByVal sDatafield As String = "")
Dim i As Integer
'On Error GoTo EH
If Not rs.EOF Then rs.MoveFirst()
cmb.Items.Clear()
i = 0
While Not rs.EOF
this line works just fine: cmb.Items.Insert(i, rs.Fields(sDisplayfield).V
it display the name of the state
My problem is right here: I am trying to store the stateid in the same combobox. Visual basic 6.0 had an itemdata for the drop down box but 2005 does not have. I have seen a solution from Microsoft but cannot apply it to my concept below.
Any help is welcomed. Again, the code works fine. I just need to find a way to store the stateid in the dropdown.
Thanks Andre
Problematic line: 'If (Len(sDatafield) > 0) Then VB6.SetItemData(cmb, i, Val(rs.Fields(sDatafield).
i = i + 1
rs.MoveNext()
End While
Exit Sub
EH:
Err.Description = "LoadCombobox-> " & Err.Description
Err.Raise(Err.Number, Err.Source, Err.Description)
ASKER
Public Sub LoadCombobox(ByVal cmb As ComboBox, ByVal rs As ADODB.Recordset, _
ByVal sDisplayfield As String, Optional ByVal sDatafield As String = "")
Dim i As Integer
'On Error GoTo EH
If Not rs.EOF Then rs.MoveFirst()
cmb.Items.Clear()
i = 0
While Not rs.EOF
'cmb.Items.Insert(i, rs.Fields(sDisplayfield).V alue)
---- cmb.DisplayMember.Insert(i , (rs.Fields(sDisplayfield). Value))
'If (Len(sDatafield) > 0) Then VB6.SetItemData(cmb, i, Val(rs.Fields(sDatafield). Value))
---- cmb.ValueMember.Insert(i, Val(rs.Fields(sDatafield). Value))
i = i + 1
rs.MoveNext()
End While
Exit Sub
EH:
Err.Description = "LoadCombobox-> " & Err.Description
Err.Raise(Err.Number, Err.Source, Err.Description)
End Sub
I inserted the two lines preceded by ---. I get an error that says: "Argument outofrangeexception was unhandled" it is related to the index which starts at 0 and increases by 1 as it goes through the loop. I do appreciate the help very much. The second help posted by BOB is the one i mentioned from microsoft. It looks good but i am not quite sure how to use that in vb 2005. This is my 3 day working with it.
Any additional comments would be helpful.
Andre
ByVal sDisplayfield As String, Optional ByVal sDatafield As String = "")
Dim i As Integer
'On Error GoTo EH
If Not rs.EOF Then rs.MoveFirst()
cmb.Items.Clear()
i = 0
While Not rs.EOF
'cmb.Items.Insert(i, rs.Fields(sDisplayfield).V
---- cmb.DisplayMember.Insert(i
'If (Len(sDatafield) > 0) Then VB6.SetItemData(cmb, i, Val(rs.Fields(sDatafield).
---- cmb.ValueMember.Insert(i, Val(rs.Fields(sDatafield).
i = i + 1
rs.MoveNext()
End While
Exit Sub
EH:
Err.Description = "LoadCombobox-> " & Err.Description
Err.Raise(Err.Number, Err.Source, Err.Description)
End Sub
I inserted the two lines preceded by ---. I get an error that says: "Argument outofrangeexception was unhandled" it is related to the index which starts at 0 and increases by 1 as it goes through the loop. I do appreciate the help very much. The second help posted by BOB is the one i mentioned from microsoft. It looks good but i am not quite sure how to use that in vb 2005. This is my 3 day working with it.
Any additional comments would be helpful.
Andre
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Your help has been helpful. I can now populate the dropdown with the statenames and the id (i believe)
my only problem is the id turns out zero everytime.
here is how i am trying to read the id: any quick sugestions?
Thanks again
Dim statename As String
Dim stateid As Long
stateid = Val(cmbState.ValueMember)
statename = cmbState.Text
MsgBox("The id is: " & stateid) ' shows always zero , the state name changes okay but id always 0
my only problem is the id turns out zero everytime.
here is how i am trying to read the id: any quick sugestions?
Thanks again
Dim statename As String
Dim stateid As Long
stateid = Val(cmbState.ValueMember)
statename = cmbState.Text
MsgBox("The id is: " & stateid) ' shows always zero , the state name changes okay but id always 0
Use
stateid = cmbState.SelectedValue
statename = cmbState.SelectedItem
instead
Roger
stateid = cmbState.SelectedValue
statename = cmbState.SelectedItem
instead
Roger
ASKER
I keep getting the following error when trying to read the id.
the stateid field in my database is a long
the statename is string
i have tried to change the stateid field to string and still have the same problem. The error:
Conversion from type 'Value' to type 'String' is not valid
keeps coming up whether it is long or string.
the Employee class created to handle the data are both string. I have tried to change them also with no good results.
I know that the id is being read becuase i can populate the dropdown with the id instead of the statename (just to test it)
It is a conversion problem which is causing me a lot problems.
Any ideas?
'Dim statename As String
Dim stateid As String
Dim statename As String
stateid = cmbState.SelectedValue
MsgBox("The id is: " & stateid)
the stateid field in my database is a long
the statename is string
i have tried to change the stateid field to string and still have the same problem. The error:
Conversion from type 'Value' to type 'String' is not valid
keeps coming up whether it is long or string.
the Employee class created to handle the data are both string. I have tried to change them also with no good results.
I know that the id is being read becuase i can populate the dropdown with the id instead of the statename (just to test it)
It is a conversion problem which is causing me a lot problems.
Any ideas?
'Dim statename As String
Dim stateid As String
Dim statename As String
stateid = cmbState.SelectedValue
MsgBox("The id is: " & stateid)
You'll have to do two things. First change the Employee Class as follows
Public Class Employee
Private m_Name As String
Private m_Id As Long 'CHANGE to Long
Public Sub New(ByVal Id As Long, ByVal Name As String) 'CHANGE Id to Long
m_Name = Name
m_Id = Id
End Sub
Public ReadOnly Property Name() As String
Get
Return m_Name
End Get
End Property
Public ReadOnly Property Id() As Long 'CHANGE to Long
Get
Return m_Id
End Get
End Property
End Class
although substituting stateid and statename.
Second, and this is my fault, sorry, to get the statename you will need to use either
statename = cmbstate.Text
as you originally suggested, or (perhaps better, in case the user has typed something new in the text portion of the combobox)
statename = CType(cmbstate.SelectedIte m, Employee).Name
substituting your class's name for Employee.
Roger
Public Class Employee
Private m_Name As String
Private m_Id As Long 'CHANGE to Long
Public Sub New(ByVal Id As Long, ByVal Name As String) 'CHANGE Id to Long
m_Name = Name
m_Id = Id
End Sub
Public ReadOnly Property Name() As String
Get
Return m_Name
End Get
End Property
Public ReadOnly Property Id() As Long 'CHANGE to Long
Get
Return m_Id
End Get
End Property
End Class
although substituting stateid and statename.
Second, and this is my fault, sorry, to get the statename you will need to use either
statename = cmbstate.Text
as you originally suggested, or (perhaps better, in case the user has typed something new in the text portion of the combobox)
statename = CType(cmbstate.SelectedIte
substituting your class's name for Employee.
Roger
MyComboBox.DataSource = MyDataTable
MyComboBox.DisplayMember = "DisplayField" 'equivalent to Item
MyComboBox.ValueMember = "DataField" ' equivalent to ItemData
and read the values from the combobox with .SelectedItem and .SelectedValue.
There are other ways, but it would involve you in creating a class to hold the Item/ItemData pairs, and putting them in a "list" of some sort - probably an Array. Using a datatable is simpler, in my view.
Also in VB.NET the normal approach would be to use ADO.NET rather than recordsets.
In VB.NET 2005 you could accomplish both the creation of the datatable and the binding of the combobox to it mostly by drag-and-drop within the IDE.
If you need further guidance on the "new" approach, just ask. I'm off to bed now but I'm sure someone else will pick it up if I'm asleep when you do so. But I'll look in again in the morning.
Roger