[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

SPLIT COMBO VISUAL BASIC 2005

Posted on 2008-01-31
13
Medium Priority
?
196 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:teogos
  • 7
  • 6
13 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20788446
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
 

Author Comment

by:teogos
ID: 20788759
Which way would you do this? can you give some samples
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20789306
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:teogos
ID: 20789525
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
 

Author Comment

by:teogos
ID: 20789792
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20791673
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20791710
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
 

Author Comment

by:teogos
ID: 20791883
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
 

Author Comment

by:teogos
ID: 20792090
cbocombosecond.Tag = prodhelpers
 Prodhelpers     NOT DECLARED

0
 

Author Comment

by:teogos
ID: 20792836
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 2000 total points
ID: 20792892
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
 

Author Closing Comment

by:teogos
ID: 31426810
Thanks very much for your help,  enjoy the points
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20793279
glad to be of assistance

AW
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question