How do you display multiple combo box items in one text box? - vba

I have a combo with multiple selections enabled. I've made a new form, and want to display the combo box selections in a text box. I use the following to display the actual data instead of the key: =[MyComboBox].[Column](2) but it only displays the data where there is exactly 1 item selected in the record. For records where there are multiple items selected in the combo box it displays nothing.
How can I display all my selections in the text box?

Use ListBox and create a function like:
Public Function GetSelectedItems(list As ListBox, Optional index as int = 0) As String
Dim result As String
For Each varItem In list.ItemsSelected
result = result & "," & combo.Column(index)
Next
GetSelectedItems = Mid(result, 2)
End Function
Assign the result of this function to text box:
Me.MyTextBox = GetSelectedItems(Me.MyComboBox, 2)

Related

Is there an easy VBA code using Split() Function to get a list of values for a combo box that was originally from a text box as a string?

On my access form, I have a text box that will be a string of characters with multiple "/"s throughout the string. I want to use the split function to separate this string into a list of values to use for my combo box on a subform.
I know it's somewhere along the lines of:
Public Function MakeList()
Dim MyList as String
Dim txt as String
txt = [myTextBoxField].Value
MyList = Split(txt,"/")
Either:
[myComboBox].Value = MyList
Or:
[myTextBoxField].Value = MyList
End Sub
I am not sure if this is supposed to be on "Form Load" or in a module for the Public Function.
All other code shows a For Loop or Debug.Print. I am looking to store this list as a field in my table and then use that field for my Row Source in my combo box.
First, combobox RowSourceType property must be set to ValueList. Next, VBA sets RowSource property, not Value. List is not a property of combobox in Access. Simply:
Me.myComboBox.RowSource = Replace(Me.myTextBoxField, "/", ";")
Form Load event should be appropriate.

Dynamically assign text to text box in the loop

I want to assign Text to the textBox in the look, I tried
Dim textBoxHB As TextBox = FindName("txt_HB_" + iRecCnt.ToString())
Me.Controls(String.Format("txt_HB_" + iRecCnt.ToString()).Text = .HouseBill
My Text box name change form txt_HB_1 ,txt_HB_2 and so on, and i want to where iRecCnt has 1,2.. values and Text is coming form .HouseBill
Is there any other way i can try?
Replace the loop with this:
Dim boxes = Me.Controls.OfType(Of TextBox).Where(Function(b) b.Name.StartsWith("txt_HB_"))
For Each box As TextBox in boxes
box.Text = .HouseBill
Next

Adding text before and after selected text - VBA user form

I'm using a VBA user-form in order to edit some text by using a rich text format textbox from the InkEdit control.
I'm trying to insert html tags in specific location when the user click on a command button on the user-form.
for example if the user clicks the "strong" button the following code is executed and the text is inserted on the cursor location inside the textbox:
InkEdit1.SelText = "<strong>"
I have another button for the closing statment which runs:
InkEdit1.SelText = "</strong>"
I'm trying to find a way that the opening statement ans the closing statement will be applied together. When the user will select a text from the text box and click the button then "strong" will be inserted before the selection and "/strong" will be inserted after the selection:
Does this work:
InkEdit1.SelText = "<strong>" & InkEdit1.SelText & "</strong>"
If you really want to get fancy, you can reselect just the original text:
Dim lPos As Long
lPos = InkEdit1.SelStart
Dim lLength As Long
lLength = InkEdit1.SelLength
InkEdit1.SelText = "<strong>" & InkEdit1.SelText & "</strong>"
InkEdit1.SelStart = lPos + Len("<strong>")
InkEdit1.SelLength = lLength
Protip: Make sure the "HideSelection" property is set to "False"
If you're not using an actual RitchTextBox, try this: Toos > Additional Controls > check Microsoft Rich Textbox

how to retrieve the value as array from a dynamically created textbox using its ID in vb.net?

I am new to this programming method. The case is I have created the checkbox and textbox dynamically where the name of the text box and check box is equal and it is handled using database.
The data I need to get is from the textbox which is selected using checkbox by the user.
The details in the array need to be name of the textbox and the value of the textbox
I created the checkbox and textbox using this method. Some thing like this
Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
I successfully created checkbox at run time. Help me getting the value, and suggest me with the suitable array list.
the naming for the check box is number 1 to n values same for the textbox

collecting textbox text with checkboxlist item using vb.net

am building a task management system using vb.net. How to set a textbox field beside each checkboxlist so when the user tick the checkbox he can comment too for this item and submit the whole finished tasks ? below is my code
Sub GetGroups()
cblGroups.DataSource = Task.Components.Tasks.GetAllTasks
cblGroups.DataTextField = "TaskName"
cblGroups.DataValueField = "ID"
cblGroups.DataBind()
End Sub
For Each item As ListItem In cblGroups.Items
If item.Selected Then
'reading each item value
End if
Next
I think you should be using a DataGridView instead. In there you can have a checkbox column and a textbox column, also a new row placeholder to put new tasks. If you don't like DataGridView, you can use alternatives.
Your other option would be to maintain a list of CheckBox controls and their TextBox pairs, essentially doing data grid's job. You are okay at first, but then you may need scrolling etc., so why not use a built-in control, where such issues are already solved out of the box.
'Check through each of the items
For Each item As ListItem In cblGroups.Items
'If this particular item is checked
If item.Selected = True Then
'Dynamically create a HTML Textbox
item.Text = [String].Format("{0}<input id=""TextBox{0}"" name=""TextBox{0}"" / >", item.Text)
Else
'Otherwise simply store the normal value
item.Text = [String].Format("{0}", item.Text)
End If
Next