Formatting a combo box - vb.net

I have added a ComboBox to a form, no additional formatting so far. I have a text box that the user enters a number of competitors (value) between 20 and 100 into. I want to populate the ComboBox so that the user can select a competitor from 1 to 100 in the ComboBox. So the user will be able to click the drop-down menu and select a competitor from a list of competitors, for example Competitor 1 to Competitor 100.
Please let me know if you need any extra info.

Try this, explanations included:
yourComboBox.Items.Clear() 'to make sure the ComboBox is empty before populating and to avoid duplicating records
If Val(yourTextbox.Text) > 0 Then 'basic checking
For x = 1 To Val(yourTextbox.Text) 'loop from 1 up to the value you entered into the textbox
yourComboBox.Items.Add(x) 'add the value of x which holds the current loop number/competitor
Next
End If

Related

How to change visibility of a combo box based on another combo box value in VBA

I am using Microsoft Access at the moment.
I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:
Private Sub cmb_BoxValue_AfterUpdate()
Dim Size1 As Integer
Size1 = Me.cmb_BoxValue.Value
Select Case Me.cmb_BoxValue
Case 1
Me.cmb_boxBox1 = True
etc...
But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you
Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop, with a test if index is <= cmb_BoxValue input to set Visible property:
For x = 1 to 20
Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next

Want a list box to show 1-10, but what is shown in the drop-down is dependent on another list item

I want to create a Combo box (Combo Box B) on an Access form containing numbers 1-10. In other words, the drop-down shows numbers 1-10 sequentially.
However, what is shown in the drop-down is dependent on Combo box A.
If x shows in Combo Box A, items 1-10 should show in Combo Box B.
If y shows in Combo Box A, numbers 1-5 should only show in Combo Box B, or at minimum prevent someone from selecting 6 or larger.
If z shows in Combo Box A, nothing should be selectable in Combo Box B.
My coding skills are rusty as I've not done much in over 10 years. Is this something easily achievable in Access, or will I need some VBA to assist?
Assuming that your question isn't just a contrived example, and that you really want to display the integers 1-10 and 1-5 in your combobox, then in it's simplest form, you could use the following function evaluated on the After Update event of your combobox 'A' and on the On Load event of your form:
Function UpdateComboRowSource(cmbCom As ComboBox, ByVal strVal As String)
cmbCom.RowSourceType = "Value List"
Select Case LCase(strVal)
Case "x": cmbCom.RowSource = "1;2;3;4;5;6;7;8;9;10"
Case "y": cmbCom.RowSource = "1;2;3;4;5"
Case Else: cmbCom.RowSource = ""
End Select
End Function
Private Sub Form_Load()
UpdateComboRowSource ComboB, ComboA.Value
End Sub
Private Sub ComboA_AfterUpdate()
UpdateComboRowSource ComboB, ComboA.Value
End Sub

Use multiple combo boxes to select cell

I'm trying to make a simple user form in Excel to enter the date that a staff member did a certain task. I have a spreadsheet that lists all of my staff down column B and I have a list of all the tasks along row 5.
I've created a form with a couple of combo boxes so I can select the staff member and also select the task. There is also a text box which I will type a date into.
What I need is when I click the "submit" button it will enter the date contained in the text box into the cell defined by the 2 selections in the combo boxes. If either of the combo boxes have nothing selected then no data should be entered into the spreadsheet when the button is clicked.
Dim nameRow As Integer
Dim job1col As Integer
nameRow = cbxStaffName.ListIndex + 6
job1col = cbxJob1.ListIndex + 3
ThisWorkbook.Sheets("Job Rotation").Cells(nameRow, job1col).Value = tbxDate.Value
The combo box containing the list of staff names is called cbxStaffName. The combobox containg the jobs is cbxJob1. The textbox containing the date I want to enter is named tbxDate.
The combo boxes have been populated with data that exists on the spreadsheet, simply names down the left and jobs along the top. If for example Jim did the job welding I want to select Jim from one combo box, select welding from the other box and when I click the button the date will go into that cell on the spreadsheet.
You would just need an If statement to check if both checkboxes have a value:
If cbxStaffName.Value <> vbNullString And cbxJob1.Value <> vbNullString Then
'write into cell
Else
MsgBox "Please select staff and job first"
End If
Alternatively you can check the ListIndex which should be -1 if nothing was selected.
Note that I recommend to always use Long instead of Integer especially when dealing with row counts. Excel has more rows than Integer can handle.

VBA - Based on User Input - look up value in table

I have created a form that asks for two user inputs, site location and sku. Site location is a drop down and SKU is a text box. Below it there is a textbox which I want to populate based on user input after they hit the "whats my price?" button.user form
I have a matrix of prices with the SKU in column B and the sites across the top in row 1 with their respective prices in the matrix(columns D-H). I have attached a sample of the table. Please note that the "SKU" and "Site" titles will not be in my actual matrix.
pricing table
I need assistance coding the "What's my price?" button in the user form.
I feel as though I would need an if statement using some sort of look up but i'm a little lost as to how to start the code.
Here is what you have to do:
Read the value from the site field;
Read the value from the SKU field;
Display the matching in the Your Price is field, using the following formula:
WorksheetFunction.Index(Range,site field, sku field)
More about WorksheetFunction.Index here:
https://msdn.microsoft.com/en-us/library/office/ff197581.aspx
You need to use Application.Match on the row and the column independently, then get the corresponding cell. Try the following code, but replace the control's names (txtSku, cmbSite) and the sheet's code name (mySheet) with yours.
Sub WhatsMyPrice_Click()
Dim rowNum, colNum
rowNum = Application.Match(txtSku.Value, MySheet.Range("B:B"), 0)
If IsError(rowNum) Then MsgBox "SKU not found": Exit Sub
colNum = Application.Match(cmbSite.Value, MySheet.Rows(2), 0)
If IsError(rowNum) Then MsgBox "Site not found": Exit Sub
txtPrice.Value = MySheet.Cells(rowNum, colNum).Value2
End Sub

Multi-Column ComboBox in Excel VB

I have some ComboBoxes in a UserForm. One of the ComboBoxes is a multicolumn ComboBox with three columns and is dependent on another ComboBoxs' value.
Does anyone know WHY when I choose one of the lines in the multicolumn combobox shows only the first column value?
I want the value from the second column to be visible when I choose a line.
The Combox Property that set which column to display in the TextBox portion of the Combobox is TextColumn
Property values:
-1 = Display the first column whose width (set by ColumnWidths property) is > 0
0 = Display the value of ListIndex
1 = Display column 1
etc