SPLIT COMBO VISUAL BASIC 2005

using the following code

Module products
    Public Sub cbocomborecords(ByRef cbocombosecond As System.Windows.Forms.ComboBox)
        Dim rs As Object
        Dim cn As Object
        cn = New ADODB.Connection
        rs = New ADODB.Recordset
        cn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Injectors;Data Source=CBSCAD03")
        rs = New ADODB.Recordset
        rs.Open("Select * From Enhanced_products", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        While Not rs.EOF
            cbocombosecond.Items.Add(Left(rs.Fields("raw_CODE").Value & Space(3), 11) & Left(rs.Fields("Description").Value & Space(3), 50) & Left(rs.Fields("Team_member").Value & Space(3), 5) & Left(rs.Fields("Cycles").Value & Space(3), 5) & Left(rs.Fields("Target").Value & Space(3), 3) & Left(rs.Fields("Tare").Value & Space(3), 10))
            System.Windows.Forms.Application.DoEvents()
            rs.MoveNext()
        End While
        rs.Close()
        rs = Nothing
        cn.Close()
        cn = Nothing
        Exit Sub
errhandle:
        Err.Clear()
        Exit Sub
    End Sub
End Module


I am calling a table  then I putting this into a combobox  and tried to split this into txtboxes with the following code

Dim item As String = cbocombosecond.SelectedItem.ToString
        Dim itemSplit() As String = Split(item, "   ")
        txtcodesecond.Text = itemSplit(0)
        txtdessecond.Text = itemSplit(1)
        txtteamstandard.Text = itemSplit(2)
        txtsecondstandard.Text = itemSplit(3)
        txttargetsecond.Text = itemSplit(4)
        txtsecondtare.Text = itemSplit(5)


the problem I am having is that the items are not being split correctly since they do not are the same lenght
Any ideas how can use do this that regarless of the lenght of the item always give me the same spacing
teogosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
though this is not how I would do this, try this change to your current code:
Dim item As String = cbocombosecond.SelectedItem.ToString
        ite.Replace("  "," ") 'replaces every occurnece of "  " (two blanks with a " " (single blank), thus collapsing all multiple blank entries in the line to a single  blank in that line
        Dim itemSplit() As String = Split(item, " ")
        txtcodesecond.Text = itemSplit(0)
        txtdessecond.Text = itemSplit(1)
        txtteamstandard.Text = itemSplit(2)
        txtsecondstandard.Text = itemSplit(3)
        txttargetsecond.Text = itemSplit(4)
        txtsecondtare.Text = itemSplit(5)

Open in new window

0
teogosAuthor Commented:
Which way would you do this? can you give some samples
0
Arthur_WoodCommented:
I would create a Help class, with the properties for each of the fields that you want to show in the textBoxes.  Then create an instance of the Help class for each row in the recordset, assign that instance as the Tag propety of the inserted row of the combobox (Yes, the Tag property can hold a Object).

Then when you select a Row, use the Tag object to show the associated data in the textboxes:


Now the following is air-code, sibce I do not have your database, but this should be very close.
Public Class ProductHelper
   Private _raw_Code as String
   Private _description as String
   Private _team_Member as String
   Private _cycles as String
   Private _target as String
   Private _tare as String
 
   Public Property Raw_Code() as String
      Property Get()
         Return _raw_Code
      End Get
      Property Set(value as String
         _raw_Code = Value
      End Set
   End Property
 
   Public Property Description() as String
      Property Get()
         Return _description
      End Get
      Property Set(value as String
         _description = Value
      End Set
   End Property
 
   Public Property Team_Member() as String
      Property Get()
         Return _team_Member
      End Get
      Property Set(value as String
         _team_Member = Value
      End Set
   End Property
 
   Public Property Cycles() as String
      Property Get()
         Return _cycles
      End Get
      Property Set(value as String
         _cycles = Value
      End Set
   End Property
 
   Public Property Target() as String
      Property Get()
         Return _target
      End Get
      Property Set(value as String
         _target = Value
      End Set
   End Property
 
   Public Property Tare() as String
      Property Get()
         Return _tare
      End Get
      Property Set(value as String
         _tare = Value
      End Set
   End Property
 
End Class
 
Module products
    Public Sub cbocomborecords(ByRef cbocombosecond As System.Windows.Forms.ComboBox)
        Dim rs As Object
        Dim cn As Object
        cn = New ADODB.Connection
        rs = New ADODB.Recordset
        cn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Injectors;Data Source=CBSCAD03")
        rs = New ADODB.Recordset
        rs.Open("Select * From Enhanced_products", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        While Not rs.EOF
            Dim prodHelper as New ProductHelper
            cbocombosecond.Items.Add(Left(rs.Fields("raw_CODE").Value & Space(3), 11) & Left(rs.Fields("Description").Value & Space(3), 50) & Left(rs.Fields("Team_member").Value & Space(3), 5) & Left(rs.Fields("Cycles").Value & Space(3), 5) & Left(rs.Fields("Target").Value & Space(3), 3) & Left(rs.Fields("Tare").Value & Space(3), 10))
            With prodHelper
               .Raw_Code = rs.Fields("raw_CODE").Value 
               .Description = rs.Fields("Description").Value 
               .Team_Member = rs.Fields("Team_member").Value 
               .Cycles = rs.Fields("Cycles").Value
               .Target = rs.Fields("Target").Value 
               .Tare = rs.Fields("Tare").Value 
            End With
            cbocombosecond.Item(cbocombosecond.Items.Count-1).Tag = prodHelper
            System.Windows.Forms.Application.DoEvents()
            rs.MoveNext()
        End While
        rs.Close()
        rs = Nothing
        cn.Close()
        cn = Nothing
        Exit Sub
errhandle:
        Err.Clear()
        Exit Sub
    End Sub
End Module
.
.
.
.
Dim item As ProductHelper = cType(cbocombosecond.SelectedItem.Tag,ProductHelper)
        with item
           txtcodesecond.Text = .Raw_Code
           txtdessecond.Text = .Description
           txtteamstandard.Text = .TeamMember
           txtsecondstandard.Text = .Cycles
           txttargetsecond.Text = .Target
           txtsecondtare.Text = .Tare
         End With
  

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

teogosAuthor Commented:
Public Property Raw_Code() as String
      Property Get()
         Return _raw_Code
      End Get
      Property Set(value as String
         _raw_Code = Value
      End Set
   End Property


Property without a ReadOnly ro Writeonly Specifier must provide a get and Set
0
teogosAuthor Commented:
OK that part is OK
Public Property Raw_Code() As String
        Get
            Return _raw_code
        End Get
        Set(ByVal value As String)

        End Set

    End Property



But now I am getting this

 cbocombosecond.Item(cbocombosecond.Items.Count-1).Tag = prodHelper


Public Member .tag or type not found

0
Arthur_WoodCommented:
ok, try this change (should work now)


 
Public Class ProductHelper
   Private _raw_Code as String
   Private _description as String
   Private _team_Member as String
   Private _cycles as String
   Private _target as String
   Private _tare as String
 
   Public Property Raw_Code() as String
      Property Get()
         Return _raw_Code
      End Get
      Property Set(value as String
         _raw_Code = Value
      End Set
   End Property
 
   Public Property Description() as String
      Property Get()
         Return _description
      End Get
      Property Set(value as String
         _description = Value
      End Set
   End Property
 
   Public Property Team_Member() as String
      Property Get()
         Return _team_Member
      End Get
      Property Set(value as String
         _team_Member = Value
      End Set
   End Property
 
   Public Property Cycles() as String
      Property Get()
         Return _cycles
      End Get
      Property Set(value as String
         _cycles = Value
      End Set
   End Property
 
   Public Property Target() as String
      Property Get()
         Return _target
      End Get
      Property Set(value as String
         _target = Value
      End Set
   End Property
 
   Public Property Tare() as String
      Property Get()
         Return _tare
      End Get
      Property Set(value as String
         _tare = Value
      End Set
   End Property
 
End Class
 
Module products
    Public Sub cbocomborecords(ByRef cbocombosecond As System.Windows.Forms.ComboBox)
        Dim rs As Object
        Dim cn As Object
        cn = New ADODB.Connection
        rs = New ADODB.Recordset
        cn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Injectors;Data Source=CBSCAD03")
        rs = New ADODB.Recordset
        rs.Open("Select * From Enhanced_products", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        While Not rs.EOF
            Dim prodHelper as New ProductHelper
            Dim prodHelpers as New ArrayList
            cbocombosecond.Items.Add(Left(rs.Fields("raw_CODE").Value & Space(3), 11) & Left(rs.Fields("Description").Value & Space(3), 50) & Left(rs.Fields("Team_member").Value & Space(3), 5) & Left(rs.Fields("Cycles").Value & Space(3), 5) & Left(rs.Fields("Target").Value & Space(3), 3) & Left(rs.Fields("Tare").Value & Space(3), 10))
            With prodHelper
               .Raw_Code = rs.Fields("raw_CODE").Value 
               .Description = rs.Fields("Description").Value 
               .Team_Member = rs.Fields("Team_member").Value 
               .Cycles = rs.Fields("Cycles").Value
               .Target = rs.Fields("Target").Value 
               .Tare = rs.Fields("Tare").Value 
            End With
            proHelpers.Add(prodHelper)
            System.Windows.Forms.Application.DoEvents()
            rs.MoveNext()
        End While
        cbocbocombosecond.Tag = prodHelpers
        rs.Close()
        rs = Nothing
        cn.Close()
        cn = Nothing
        Exit Sub
errhandle:
        Err.Clear()
        Exit Sub
    End Sub
End Module
.
.
.
.
in the cboComboSecond_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbocombosecond.SelectedIndexChanged
Dim prodHelpers as ArrayList = CType(ComboBox1.Tag, ArrayList)
Dim Item As ProdHelper = CType(Helpers.Item(ComboBox1.SelectedIndex), ProdHelper)
Dim item As ProductHelper = proHelpers.Item(cbocombosecond.ListIndex -1)
        with item
           txtcodesecond.Text = .Raw_Code
           txtdessecond.Text = .Description
           txtteamstandard.Text = .TeamMember
           txtsecondstandard.Text = .Cycles
           txttargetsecond.Text = .Target
           txtsecondtare.Text = .Tare
         End With
End Sub  

Open in new window

0
Arthur_WoodCommented:
slight correction, sorry
Public Class ProductHelper
   Private _raw_Code as String
   Private _description as String
   Private _team_Member as String
   Private _cycles as String
   Private _target as String
   Private _tare as String
 
   Public Property Raw_Code() as String
      Property Get()
         Return _raw_Code
      End Get
      Property Set(value as String
         _raw_Code = Value
      End Set
   End Property
 
   Public Property Description() as String
      Property Get()
         Return _description
      End Get
      Property Set(value as String
         _description = Value
      End Set
   End Property
 
   Public Property Team_Member() as String
      Property Get()
         Return _team_Member
      End Get
      Property Set(value as String
         _team_Member = Value
      End Set
   End Property
 
   Public Property Cycles() as String
      Property Get()
         Return _cycles
      End Get
      Property Set(value as String
         _cycles = Value
      End Set
   End Property
 
   Public Property Target() as String
      Property Get()
         Return _target
      End Get
      Property Set(value as String
         _target = Value
      End Set
   End Property
 
   Public Property Tare() as String
      Property Get()
         Return _tare
      End Get
      Property Set(value as String
         _tare = Value
      End Set
   End Property
 
End Class
 
Module products
    Public Sub cbocomborecords(ByRef cbocombosecond As System.Windows.Forms.ComboBox)
        Dim rs As Object
        Dim cn As Object
        cn = New ADODB.Connection
        rs = New ADODB.Recordset
        cn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Injectors;Data Source=CBSCAD03")
        rs = New ADODB.Recordset
        rs.Open("Select * From Enhanced_products", cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        While Not rs.EOF
            Dim prodHelper as New ProductHelper
            Dim prodHelpers as New ArrayList
            cbocombosecond.Items.Add(Left(rs.Fields("raw_CODE").Value & Space(3), 11) & Left(rs.Fields("Description").Value & Space(3), 50) & Left(rs.Fields("Team_member").Value & Space(3), 5) & Left(rs.Fields("Cycles").Value & Space(3), 5) & Left(rs.Fields("Target").Value & Space(3), 3) & Left(rs.Fields("Tare").Value & Space(3), 10))
            With prodHelper
               .Raw_Code = rs.Fields("raw_CODE").Value 
               .Description = rs.Fields("Description").Value 
               .Team_Member = rs.Fields("Team_member").Value 
               .Cycles = rs.Fields("Cycles").Value
               .Target = rs.Fields("Target").Value 
               .Tare = rs.Fields("Tare").Value 
            End With
            proHelpers.Add(prodHelper)
            System.Windows.Forms.Application.DoEvents()
            rs.MoveNext()
        End While
        cbocbocombosecond.Tag = prodHelpers
        rs.Close()
        rs = Nothing
        cn.Close()
        cn = Nothing
        Exit Sub
errhandle:
        Err.Clear()
        Exit Sub
    End Sub
End Module
.
.
.
.
in the cboComboSecond_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbocombosecond.SelectedIndexChanged
Dim prodHelpers as ArrayList = CType(ComboBox1.Tag, ArrayList)
Dim Item As ProductHelper = CType(prodHelpers.Item(cboComboSecond.SelectedIndex), ProductHelper)
 
        with item
           txtcodesecond.Text = .Raw_Code
           txtdessecond.Text = .Description
           txtteamstandard.Text = .TeamMember
           txtsecondstandard.Text = .Cycles
           txttargetsecond.Text = .Target
           txtsecondtare.Text = .Tare
         End With
End Sub  

Open in new window

0
teogosAuthor Commented:
On this which way is the correct way? like this or the other below

Public Property Raw_Code() as String
      Property Get()
         Return _raw_Code
      End Get
      Property Set(value as String
         _raw_Code = Value
      End Set
   End Property




Public Property Raw_Code() As String
        Get
            Return _raw_code
        End Get
        Set(ByVal value As String)

        End Set

    End Property

0
teogosAuthor Commented:
cbocombosecond.Tag = prodhelpers
 Prodhelpers     NOT DECLARED

0
teogosAuthor Commented:
I can see on this line you are creating another combobox
Dim prodHelpers as ArrayList = CType(ComboBox1.Tag, ArrayList)
What is the reason behind this
0
Arthur_WoodCommented:
No, that was a mistake, that should have been cboComboSecond.  I wrote the code with my own copy of VS, and did not change the name of the ComboBox control from the default when I added the control to the form.  Sorry.

Also, I declared ProdHelpers in the Form declaration section.  That way you could instantiate it in both procedures, without having to declare it again.

Private ProdHelpers as ArrayList

before the first sub in the Form code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
teogosAuthor Commented:
Thanks very much for your help,  enjoy the points
0
Arthur_WoodCommented:
glad to be of assistance

AW
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.