Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display selected checkboxes value in textboxes. suppose i have 14th checkboxes if i select check3, check2, check7 in descending order i should be display in textbox in ascending order like check2, check3, check7 how to develop this. can anyone help to resolve this...?

What I have tried:

Private Sub CheckboxSelection()
Dim cBox As Control
For Each cBox In Me.Controls
If TypeName(cBox) = "CheckBox" Then
If cBox.Value = 1 Then
End If
End If
Next
txtBlockNo.Text = (Join(",", cBox))
End Sub
Posted
Updated 30-Mar-21 22:52pm
Comments
Richard MacCutchan 31-Mar-21 4:14am    
In your control loop you could build an array of the values you want to display. Then at the end join them into a single string and add that to the text box.

But since VB6 is long since out of support, you should think of switching to VB.NET.
Bhavesh Vankar 31-Mar-21 4:40am    
but my project is in vb6.0 and its currently online at client side. can you help with code example i am new in vb6.0.
Richard MacCutchan 31-Mar-21 4:53am    
There is not enough information in your question to guess what result you need to create, and there is not space to teach you VB6. See Strings Class (Microsoft.VisualBasic) | Microsoft Docs[^] for details on how to create a string from an array of string objects.

1 solution

As suggested by @Richard-MacCutchan, build an array, sort it ("ascending order" is in your requirements), join them into a single string and add it to the text box. Here is a working example in VBA. It's very similar to VB6 and I don't have VB6 any more - it went out of support decades ago.
VB
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.Name
            index = index + 1
        End If
    End If
Next
ReDim Preserve TheArray(index - 1)  'Get rid of the empty spaces
'Sort the array alphabetically
SortArray TheArray

Dim s As String
s = Join(TheArray, vbCrLf)
Me.TextBox1.Text = s
Note because I have joined the string using carriage return/linefeeds the text box will need to have Multi-line property = True.

Now let's address the elephant in the room...
Quote:
i am new in vb6.0.
If you are learning VB6 so that you can maintain (or rewrite) a legacy system then fair enough. But if you are learning VB6 for any other reason then please stop. VB6 is ancient technology. You would be far better off learning one of the .NET languages - and they are free to download. The transition from VB6 to VB.NET is not hard, but is not advised as you will miss out on the new way of doing things, and probably have to "unlearn" a lot of bad habits (I did it that way and have regretted it many times since)

Edit: Here is an example of sorting an array adapted from - FreeVBCode code snippet: Simplified Array Sorting[^]
VB
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


Edit: The full set of code that will produce the results as described
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
 
Share this answer
 
v3
Comments
CHill60 31-Mar-21 5:18am    
Which line is throwing the exception? I'm afraid I don't know what you mean by "checkbox showing block no"
CHill60 31-Mar-21 5:22am    
Also - the value of a checkbox is not 1,2,3 it is checked or unchecked (true/false, 1/0). However, there is enough there in my example for you do change what goes into the Array to be displayed.
Bhavesh Vankar 31-Mar-21 5:18am    
i have modified your code according to my project requirement. but their is only one issue. kindly help..

i want to display selected checkbox value like 1,2,3,4.....13,14 when i select checkbox it display like 123

Sub chkcheck()
Dim m As Integer
m = Me.Controls.Count
ReDim TheArray(m) As String
Dim cBox As Object, index As Integer
index = 0
For Each cBox In Me.Controls
'Debug.Print TypeName(cBox), cBox.Caption
If TypeName(cBox) = "CheckBox" Then
If cBox.Value = 1 Then
TheArray(index) = cBox.Caption
index = index + cBox

End If
End If
Next
ReDim Preserve TheArray(index + 1) 'Get rid of the empty spaces
'Sort the array alphabetically
'SortArray TheArray
Dim s As String
s = Join(TheArray, vbCrLf)
Me.txtBlockNo.Text = s

End Sub
CHill60 31-Mar-21 5:27am    
You have modified the code so that it will no longer work.
index = index + cBox
you should not even attempt to add a control to an integer! What are you actually trying to do there? Index is an index into the array so just leave that as
index = index + 1
You have also removed the sorting and yet you stated that you wanted this information in ascending order
Bhavesh Vankar 31-Mar-21 5:31am    
when i enable SortArray TheArray this line it generate error sub or function not defined.. i have write it as below

Sub chkcheck()
Dim m As Integer
m = Me.Controls.Count
ReDim TheArray(m) As String
Dim cBox As Object, index As Integer
index = 0
For Each cBox In Me.Controls
'Debug.Print TypeName(cBox), cBox.Caption
If TypeName(cBox) = "CheckBox" Then
If cBox.Value = 1 Then
TheArray(index) = cBox.Caption
index = index + 1

End If
End If
Next
ReDim Preserve TheArray(index + 1) 'Get rid of the empty spaces
'Sort the array alphabetically
SortArray TheArray
Dim s As String
s = Join(TheArray, vbCrLf)
Me.txtBlockNo.Text = s
End Sub


in short when i select one or multiple checkbox it should be display in textbox like 1,2,3,4

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