Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 15 checkbox in vb6.0 form. i want to display selected checkbox value in single textbox like 1,2,3,4,5 in sort order all working fine but one issue i just need comma in middle to value like 1,4,6,9 comma should not be display after or before value.

actually i have used code as below kindly help me to resolve this its urget to solve this but i'm new in vb6.0.

thanks in advance...

What I have tried:

VB
Sub chkcheck()
    Dim m As Integer
    m = Me.Controls.Count
    ReDim TheArray(m) As String 'Max size the array will be
    Dim cBox As Object, index As Integer
    
    index = 0
    For Each cBox In Me.Controls
        Debug.Print TypeName(cBox), cBox.Value
        If TypeName(cBox) = "CheckBox" Then
            If cBox.Value = True Then
                TheArray(index) = cBox.Caption
                index = index + 1
            End If
        End If
    Next
    ReDim Preserve TheArray(index - 1)  'Get rid of the empty spaces
    SortArray TheArray
    
    Dim s As String
    s = Join(TheArray, ",")
    Me.TextBox1.Text = s
End Sub

Public Sub SortArray(ByRef TheArray As Variant)
   Dim Sorted As Boolean
    Sorted = False
    Do While Not Sorted
        Sorted = True
        Dim X As Long, Temp As Variant
        For X = 0 To UBound(TheArray) - 1
            If TheArray(X) > TheArray(X + 1) Then
                Temp = TheArray(X + 1)
                TheArray(X + 1) = TheArray(X)
                TheArray(X) = Temp
                Sorted = False
            End If
        Next X
    Loop
End Sub
Posted
Updated 10-Dec-21 19:56pm
Comments
CHill60 9-Dec-21 5:34am    
When I run your code I do not get commas before or after the list of CheckBox names. What is your problem?
Also you state "you are new in vb6.0" - then you should stop using VB6 - it has been defunct for decades
Bhavesh Vankar 9-Dec-21 6:01am    
right sir but in my side when i select checkbox i got the comma backside. and vb6.0 is too old but our project is build in vb6.0 and this is small update in this project so that why i have to solve this.

Quote:
VB
ReDim TheArray(m) As String 'Max size the array will be
Not true. In VB6, the syntax for arrays specifies the upper bound of the array, not the length of the array.

ReDim Statement - Visual Basic | Microsoft Docs[^]

If you have 42 controls, your array will contain 43 elements, with indexes from 0 to 42. Since you're only filling 42 elements, you'll always have one blank element, which will give you an extra comma.

Change your code to:
VB
ReDim TheArray(m - 1) As String
 
Share this answer
 
Quote:
issue i just need comma in middle to value like 1,4,6,9 comma should not be display after or before value.

With a quick look at your code, I have 1 question:
What happen when some CheckBox have no caption ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
i used below line and its working fine but while control reset it occur error as below.
in below line its occur error

Me.Text2.Text = Mid$(s, 1, Len(s) - 1) ' Replaces last comma.

error is :-
runtime error '5'
invalid procedure call or argument




Sub cBoxSelection()
    Dim m As Integer
    m = Me.Controls.Count
    ReDim TheArray(m) As String 'Max size the array will be
    Dim cBoxes As Object, Index As Integer
     Index = 0
         For Each cBoxes In Me.Controls
          'Debug.Print TypeName(cBoxes), cBoxes.Value
            If TypeName(cBoxes) = "CheckBox" Then
             If cBoxes.Value = 1 Then
            TheArray(Index) = cBoxes.Caption
            Index = Index + 1
            End If
        End If
    Next
    ReDim Preserve TheArray(Index)  'Get rid of the empty spaces
      'SortArray TheArray
        Dim s As String
    s = Join(TheArray, ",")
    Me.Text2.Text = Mid$(s, 1, Len(s) - 1) ' Replaces last comma.
End Sub
 
Share this answer
 
Comments
Patrice T 11-Dec-21 1:30am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
And remove this non answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900