Correct use of ListIndex navigate form by query - vba

I have a subform that has a query in a combo box, and I want to navigate through the different records matching the query results.
Online, I found the "ListIndex" function for a combo box, and my code is as follows for a "next record" scenario:
Private Sub Button_Click()
comboCountry.SetFocus
If comboCountry.ListIndex <> comboCountry.ListCount - 1 Then
comboCountry.ListIndex = comboCountry.ListIndex + 1
Else
comboCountry.ListIndex = 0
End If
End Sub
Even though my initial query works to get in a finite number of values into the combo box, when I press the button I get the error:
Run-time error 7777; You've used the ListIndex property incorrectly
VBA Debugger says that the erroneous line of code is line 4,
comboCountry.ListIndex = comboCountry.ListIndex + 1
What is going wrong with my macro?
Thanks

Well, the on-line help - which you can access by one key press - is very clear: This property is read-only.
You can do something like this:
If comboCountry.ListIndex < comboCountry.ListCount - 1 Then
comboCountry.Value = comboCountry.ItemData(comboCountry.ListIndex + 1)
End If

I believe the source of your faulty code comes from another web site (maybe bytes?). I am not sure but I guess it is different in excel vba than in access vba. At least in ms access vba, listindex is read only and combo.value = combo.itemdata (i) is what is used to set the selections.
below is the code used to cycle through combobox list items using arrow keys.
Public Sub ArrowKeys(ByRef KeyCode As Integer, combo As ComboBox, ByRef Flag As Boolean)
' flag is used to be able to toggle normal action in combo_change()
Select Case KeyCode
Case vbKeyDown
KeyCode = 0 ' make sure the action wont be duplicated
If combo.ListIndex <> combo.ListCount - 1 Then
combo.Value = combo.ItemData(combo.ListIndex + 1)
Flag = True
combo.Dropdown
Else
combo.Value = combo.ItemData(0)
Flag = True
combo.Dropdown
End If
Case vbKeyUp
KeyCode = 0 ' make sure the action wont be duplicated
If combo.ListIndex = -1 Then ' In case nothingg is selected yet (-1 index)
combo.Value = combo.ItemData(combo.ListCount - 1)
combo.Dropdown
ElseIf combo.ListIndex <> 0 Then
combo.Value = combo.ItemData(combo.ListIndex - 1)
Flag = True
combo.Dropdown
Else
combo.Value = combo.ItemData(combo.ListCount - 1)
Flag = True
combo.Dropdown
End If
End Select
End Sub

Related

Conditional increasing ID in ms access formular

i have a ms access formular where there are given several Information. For "Status" Combobox there are several options like "1","2","3","4". If "4" is selected in "cbx_Status" then I want to add in Textbox "txt_ID_Order" an automatically increased ID and give an Order Time in textbox "txt_OrderTime". That's why I wrote this and works well:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.cbx_Status.Value = "4" Then
Me.txt_OrderTime = Now()
Me.txt_ID_Order = DMax("[ID_Order]", "tblX") + 1
Else:
Me.txt_ID_Order=""
Me.txt_OrderTime = ""
End If
End Sub
However, if Status "4" is for some reason changed and again selected , I want to keep that old ID. But right know wenn i do that, it's still increasing ID.
How can I fix it?
Thanks
Check for a value:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me!cbx_Status.Value = "4" Then
If IsNull(Me!txt_OrderTime.Value) Then
Me!txt_OrderTime.Value = Now()
Me!txt_ID_Order.Value = DMax("[ID_Order]", "tblX") + 1
End If
Else
Me!txt_ID_Order.Value = Null
Me!txt_OrderTime.Value = Null
End If
End Sub
Not sure about the logic though; if you select anything else than 4, the textboxes will be cleared.

Write value to a textbox in Userform - VBA

I am trying to write a value dynamically in Textbox which is placed on a Userform. This is my code, and I am getting an error in the last line. It says object required.
Sub Userform1_Display()
TotalSelected = 0
With Sheets("Main").Ent_ListBox
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Count = 1
TotalSelected = TotalSelected + Count
End If
Next i
End With
'Useform'
Questionaire.Show
'TextBox placed in Userform'
N_Ent_TextBox.Value = TotalSelected
End Sub
Kindly share your thoughts
It looks like your code isn't actually inside the userform's class module - so you need to fully qualify the object:
Questionaire.N_Ent_TextBox.Value = TotalSelected
N_Ent_TextBox.Text = TotalSelected
try to use this

Problems with Null in VBA

I want the code to display the selection in ListBox1 in a MsgBox and "Select a Capacity" if ListBox1 is empty/not selected.
If I try to use IsEmpty(), then ListBox1.Value is Null.
If I use IsNull(), then ListBox1.Value is "".
Private Sub CommandButton3_Click()
Dim Cap As Integer
If IsNull(ListBox1) = True Then
MsgBox "Select a Capacity"
Exit Sub
End If
Cap = Left(ListBox1.Value, 2)
MsgBox Cap
End Sub
Any suggestions would be appreciated.
You could try:
If ListBox1.ItemsSelected.Count = 0 Then
MsgBox "Select a capacity"
Exit Sub
End If
Cap = Left(ListBox1.Value, 2)
The IsEmpty function is used to check is a variable of type Variant has been initialised. It cannot be used to check if a ListBox contains any entries.
The IsNull function checks if a variable has been set to Null. This doesn't help with checking a ListBox for entries.
Instead, use If ListBox1.ListCount = 0 Then to check if the ListBox is empty and use If ListBox1.ListIndex = -1 Then to check if any entries have been selected.
If the ListBox allows multiple selections at once then, as mentioned by #shoegazer100, use something like:
Dim rowNumber As Long
For rowNumber = 0 To (ListBox1.ListCount - 1)
If ListBox1.Selected(rowNumber) Then
' do something
End If
Next rowNumber
to determine which rows are currently selected (if Selected returns True for a particular row then the corresponding row in the ListBox is selected)

Excel multi-select, multi-column listboxes

I am having trouble coding a userform that takes the selected data from one multi-column Listbox and adds it to another Listbox on the same userfrom. after adding, the data is removed from the source Listbox
"ListBox" is where the data is located, and "listbox1" is where it is being added to.
Private Sub add_Click()
For i = 0 To ListBox.ListCount - 1
If ListBox.Selected(i) = True Then
NextEmpty = ListBox1.ListCount
ListBox1.List(NextEmpty, 0) = ListBox.List(i, 0)
ListBox1.List(NextEmpty, 1) = ListBox.List(i, 1)
ListBox1.List(NextEmpty, 2) = ListBox.List(i, 2)
ListBox.RemoveItem (i)
End If
Next
End Sub
This code gives me a Run-time error '381'
"Could not set the list property. Invalid property array index."
I have done some looking around but can't seem to pinpoint how to use these properties correctly. Any help is greatly appreciated.
In order to do this, like Daniel said, we need to use an add function.
In the code below you can see how I used the .additem function in my with-block.
To remove the selection after moving it to a new Listbox, I run a backwards loop.
For i = MainListBox.ListCount - 1 To 0 Step -1
Private Sub add_Click()
Dim i As Integer
For i = 0 To MainListBox.ListCount - 1
If MainListBox.Selected(i) Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = MainListBox.List(i, 0)
.List(.ListCount - 1, 1) = MainListBox.List(i, 1)
.List(.ListCount - 1, 2) = MainListBox.List(i, 2)
End With
End If
Next i
For i = MainListBox.ListCount - 1 To 0 Step -1
If MainListBox.Selected(i) Then
MainListBox.RemoveItem (i)
End If
Next i
End Sub
You cannot set a value to an index greater than the maximum in the list (and the maximum is exactly ListCount (Or ListCount-1 if zero based).
So, you must add the values with ListBox1.Add

Update text box with each click of a button

I am trying to set up a userform that will be used to take orders. e.g. each time you click the Cappuccino button it will increment the text box by one indicating that you are ordering 1, 2, 3 etc.
As far as I can get it is to only populate the text box one time. Each additional click does not appear to do anything. This is the Code I currently have for it. I tried declaring num as public. I thought that might be part of the problem but it did not seem to make a difference. Could it be a type casting issue since it is a "text" box and I am trying to treat it as in integer?
Private Sub Capuccino_Click()
If (Cap_qty.Value = Null) Then
Dim num As Integer
num = 1
Cap_qty.Value = Cap_qty.Value + num
ElseIf (Cap_qty.Value = IsNotNull) Then
num = num + 1
Cap_qty.Value = num
'Cap_qty.Value = num + 1
'num = Cap_qty.Value
End If
End Sub
Well, that makes a difference. I looked at something somewhere that told me to use Null, IsNotNull. I was able to get it working with the following which at the moment does not make sense to me I will have to figure out why it works this way. I guess there is some background action happening that is letting me do math with stings
Private Sub CommandButton1_Click()
If (TextBox1.Value = vbNullString) Then
TextBox1.Value = 1
Else
TextBox1.Value = TextBox1.Value + 1
End If
End Sub
​