Check for Access form list box no selection - ms-access

I'm trying to see if a list box is empty. I've seen the recommendation to use
If IsNull(txtLevel) Then
MsgBox "No Item is Selected"
but this is returning the error MsgBox even when items are select.
Another issue I have had in using other code If txtLevel.ListIndex = "-1" or If txtLevel.listount = 0 is it works well the first time, but if you select and then unselect, this doesn't trigger the error message.
Edit: the answer for me that works is: If txtLevel.ItemsSelected.Count = 0

You can also use the .ItemsSelected property which returns a variant array that contains the row number of the entries that are selected, or the .Selected property that returns True when the row specified in the parameter is selected.

The answer that works is "If txtLevel.ItemsSelected.Count = 0"

Try this code - see if you can see your solution in this. I left comments to explain what's going on.
ComboData is a combobox control
CheckNoComboData is a checkbox control
CheckSelection is a checkbox control
CheckNoSelection is a checkbox control
Code:
Dim intIter As Integer
Dim boolItems As Boolean
' Check if there is no Row Source data
If Nz(Me.ComboData.RowSource, "") = "" Then
Me.CheckNoComboData = True
Else
Me.CheckNoComboData = False
End If
' Check if there is a row source, but no
' items resulting from that rowsource
If Me.ComboData.ListCount = 0 Then
Me.CheckNoComboData = True
Else
Me.CheckNoComboData = False
End If
' Check if any items in the listbox are selected or not
Items = False
' Loop through each item in the combobox
For intIter = 0 To (Me.ComboData.ListCount - 1)
' If its selected, then we know items are selected
If Me.ComboData.Selected(intIter) Then
Items = True
Exit For
End If
Next
' Return Results
Me.CheckSelection = Items
Me.CheckNoSelection = Not Items

Related

Moving to position of multi select listbox

I have a multi select listbox, which I would like to make searchable. If the searched for value is found in the listbox, I'd like to scroll to that position, but not select it. Is this possible? The code I have so far for searching is :-
With lstComm
For i = 0 To .ListCount - 1
If .Column(6, i) = txtSearch.Value Then
End If
Next i
End With
...but I'm not sure how to complete the scroll.
This should work fine:
Dim index As Long
With lstComm
Dim match As Boolean
For index = 0 To .ListCount - 1
If .Column(1, index) = txtSearch.Value Then
match = True
Exit For
End If
Next
If Not match Then Exit Sub
Dim isSelected As Boolean
isSelected = .Selected(index)
.Selected(index) = True
.Selected(index) = isSelected
End With
It retrieves the searched item of the listbox.
If no item has been found it exits.
Otherwise it stores the current selection state of that item, selects it to position the listbox, and restores the stored state of the item.

Access, combobox with multiple selections: how to see if an certain item is included?

In my Access form I have a ComboBox that allows multiple selection. I would like to see if a certain item is selected. How to do?
It would be great if exists something like:
if Me.<ComboBox name>.Options.<Item_5>.Selected = True Then
msgbox "it is included"
End If
When the Control Source of the ComboBox points to a field which may contain multiple values, the Value property of the ComboBox returns an array.
Therefore, the task is reduced to testing whether the array contains the item in question, which may be achieved using the following (amongst other ways):
InStr(1, Join(<ComboBoxName>.Value, ","), <Item_5>) > 0
EDIT: Following Erik's comment, the below may be a more appropriate way to test for the presence of the item:
Dim itm As Variant
Dim rtn As Boolean
For Each itm In <ComboBoxName>.Value
If itm = <Item_5> Then
rtn = True
Exit For
End If
Next itm
If rtn Then MsgBox "Item is included"

MS Access - How to Add Items to List Box and Select only Items that are found in Recordset

I have a list box that is populated by a recordset. I am trying to then select the items in that list box based on the values in another recordset. I am able to populate the list box, but when I try to select the values based on another recordset the list box Me.ToolUsed1 is Null. I call another function for selecting the values because I plan on using the same procedure for other list boxes. I really appreciate any help that you can provide.
'Populate the tool list box
While Not rsToolList.EOF
Me.ToolUsed1.AddItem Item:=rsToolList.Fields(0)
rsToolList.MoveNext
Wend
matchKey = "MatchKey = """ & rsActivities.Fields(0) & """"
If rsTools.RecordCount > 0 Then
rsTools.MoveFirst
rsTools.FindFirst (matchKey)
toolIndex = rsTools.Fields(2)
While Not rsTools.EOF
If (rsTools.Fields(2) = toolIndex) Then
SelectListValues Me.ToolUsed1, rsTools.Fields(1)
End If
rsTools.MoveNext
Wend
End If
Private Sub SelectListValues(tempListBox As Object, selectString As String)
Dim i As Integer
Dim found As Boolean
i = 0
found = False
'select the value in the listbox
While i < tempListBox.ListCount And Not found
If tempListBox.Value(i) = selectString Then
tempListBox.Selected(i) = True
found = True
End If
i = i + 1
Wend
'if the string wasn't found, add it
If Not found Then
tempListBox.AddItem (selectString)
End If
End Sub
Consider using a query recordsource for your listbox instead of value items to add. Listboxes like comboxes maintain the RowSource property, allowing for Table/Query sources which you can set to the first recordset, rsToolList. Then, just open one recordset, rsTools, and loop through it to decide selected items. Do note, with table/query sources the bound column is the value of the listbox, not any of the other columns.
' POPULATE TOOL LIST BOX TO QUERY
Me.tempListBox.RowSource = "ToolList" ' OR USE SELECT SQL STATEMENT HERE
Me.tempListBox.RowSourceType = "Table/Query"
Me.tempListBox.Requery
' LOOP THROUGH LISTBOX AND RECORDSET FOR SELECTED ITEMS
Dim rsTools As Recordset, i As Integer
Set rsTools = CurrentDb.OpenRecordset("Tools", dbOpenDynaset)
rsTools.MoveLast
rsTools.MoveFirst
If rsTools.RecordCount > 0 Then
While Not rsTools.EOF
i = 1
While i < Me.tempListBox.ListCount
' CHANGE C FUNCTION HERE TO NEEDED TYPE: CLng, CInt, CStr, CDate, ...
If CLng(Me.tempListBox.ItemData(i)) = rsTools.Fields(1) Then
Me.tempListBox.Selected(i) = True
End If
i = i + 1
Wend
rsTools.MoveNext
Wend
End If
rsTools.Close
Set rsTools = Nothing

OldValue and Value same on radio button change Access 2007 VB

I have an access 2007 front-end app. On a particular form, there are 2 radio buttons in a radio button group. I am trying to detect when the radio button group is changed and capture the old and new values, but my OldValue and Value properties are = in the save event, even if I have changed it. The OldValue is equal to the New radio button value, not what it was originally.
I tried coding this in the form's Save subroutine. The intent was to compares the RB value with the original dataset value to force setting the old value, but it doesn't like the 'SET' statements
If fraResistOption.Value = 1 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 2 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "Y"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "Y"
End If
End If
End If
End If
Could someone suggest a way to do this? Please and thank you.
The .OldValue property of the Option Group (sometimes referred to as "Frame") does work. I have a table named [optValues]:
[ID] - AutoNumber
[optValue] - Numeric (Long Integer)
It contains one record:
ID optValue
1 3
My form's Record Source is the [optValues] table. The form has an Option Group named "Frame0" whose Control Source is the [optValue] field. It contains three Option buttons
label: "foo", value: 1
label: "bar", value: 2
label: "baz", value: 3
The After Update event handler for Frame0 is:
Private Sub Frame0_AfterUpdate()
MsgBox "Old value: " & Me.Frame0.OldValue & ", New value: " & Me.Frame0.Value
End Sub
When I open the form, "baz" is selected (because [optValue] is 3 in the table):
When I click on "foo" I immediately see the (correct) old and new values:
The only way I can think of to detect and capture changes to Option-Group values is to handle the Form_Current event (save the Option Group value), then also handle the Option-Group After event. Although you can change the Option-Group.Value, OldValue is likely a protected (read-only) property. Hope something like the following helps:
Dim OldValue As Byte
Dim CurrentValue As Byte
Private Sub Form_Current()
OldValue = Frame0.Value
End Sub
Private Sub Frame0_AfterUpdate()
CurrentValue = Frame0.Value
Debug.Print "AFTER: OldValue=" & OldValue & "' CurrentValue=" & CurrentValue
End Sub
I don't know why but .oldvalue doesn't work for me.
If you are in the same boat as me, you can use the BeforeUpdate event of the optionGroup and set a static variable.
Then read the static variable in afterUpdate event and reset it for the next change.

ms Access runtime error '2115" when setting ListIndex to -1

I have Find Forms that are basically a large list box with some filter, sorting and action buttons and some text boxes to implement the filter.
I have been using these forms for quite a while without problem. The SELECT button is disabled unless an item from the list box is selected. I am trying to unselect any listbox entry when the filtering or sorting is changed.
To do this I set the focus to the listbox (no multi selection option) to -1. There are a number of equivalent actions and I have tried most of them. Once ListIndex is set to -1 I get the '2115' runtime error if I execute a requery action on the listbox or a refresh action on the containing form. In addition, I cannot set the focus to any of the text boxes or buttons on the form getting a variety of runtime errors that say that the field must be saved.
Anybody have an ideas about how this is supposed to function?
In the code snippet the actual code getting the error is the .requery line near the bottom after the Unselect listbox comment
Here is a code snippet of one of the ways I have had this occur:
With Me.lbxFoundItems
If strCurrentSearchText = vbNullString Then 'Nothing to search for
.Visible = False
Call ButtonSettings
Exit Sub
End If
strQry = strSql & strWhere & strOrderBy
If Nz(.RowSource, vbNullString) = vbNullString Then 'First time through
Set qry = Nothing
On Error Resume Next
Set qry = CurrentDb.CreateQueryDef(strQname, strQry)
If qry Is Nothing Then
Set qry = CurrentDb.QueryDefs(strQname)
qry.sql = strQry
End If
colGarbage.Add CurrentDb.QueryDefs(qry.Name), qry.Name
.RowSource = qry.Name
Else
CurrentDb.QueryDefs(qry.Name).sql = strQry
End If
' Unselect the listbox entry
.SetFocus
.Selected(.ListIndex + 1) = False
.Requery
Me.Refresh
Me.tbxListCount = .ListCount - 1
.Visible = True
End With