How to get all listbox items names using access vba - ms-access

I've a list box in access trying to get listbox items in a variable and comapre those items with table column names i.e check all list box items with table columns if any of those are missing. How do i get listbox names.

The Controls.Item(index).ControlType property will return what type of control is on your form. You can loop through the Form.Controls collection looking for acListBox. Control.Name will return the control name. For example:
'placed this code into a form's module
dim i as long
dim c as control
For i = 0 to Me.Controls.count - 1
set c = Me.Controls.Item(i)
If c.ControlType = acListBox Then
debug.print c.Name
End if
Next i

Related

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"

Access For Loop Hide Objects

I'm trying to hide/unhide around 30 objects on my form when the user selects certain values from a dropdown menu. I tried the loop below, however I receive the following error: 'Object doesn't support this property or method.' I have this code running on the 'AfterUpdate' of the dropdown menu object.
Dim VisibleVisitFields() As String
Dim VisibleVisitFieldlist As String
Dim varVisibleVisit As Variant
VisibleVisitFieldlist = "VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event"
VisibleVisitFields = Split(VisibleVisitFieldlist, ",")
If (EventType = 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = True
Exit For
Next
Else
If (EventType <> 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = False
Exit For
Next
End If
End If
Which line triggers the error? Suspect it is reference to the subform that is flawed. Never seen code like that to loop through an array. Suggest naming subform container different from the object it holds, such as ctrEvent. What is EventType - a textbox/field on the form? Consider code:
Dim aryFields As Variant
Dim x As Integer
aryFields = Split("VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event", ",")
For x = 0 To UBound(aryFields)
Me.ctrEvent.Form.Controls(aryFields(x)).Visible = Me.EventType = 3
Next
Alternative methods not using array:
Set control Tag property then code loops through all controls on form and sets visibility for those that have particular value in Tag.
Dim ctrl As Control
For Each ctrl in Me.ctrEvent.Form.Controls
If ctrl.Tag = "something" Then ctrl.Visibility = Me.EventType = 3
Next
Another is to give controls similar names, like: Visit1, Visit2, etc. Then code:
Dim x As Integer
For x = 1 to 30
Me.ctrEvent.Form.Controls("Visit" & x).Visible = Me.EventType = 3
Next
Advise no spaces or punctuation/special characters (underscore only exception) in naming convention.
You are trying to iterate over an array of strings.
Dim VisibleVisitFields() As String
You need to declare the array to contain variants (which can still contain strings)
Dim VisibleVisitFields() As Variant

Access subform keeps adding new extra empty record into table

I am having issue with access form (Contains combo boxes) and subform (display data from form combo boxes combination). I have 3 combo box on form and a report button. When I click on report button, the sub form will show report data based on combo boxes values.
It shows right data based on user selection on combo boxes but it gives me an empty record also and add that record into table also. How to prevent adding an empty record into table?
My Code:--
Private Sub Department_AfterUpdate()
dsSQL = "A select statment"
Me![Student].RowSource = ""
Me![Student].RowSource = dsSQL
dcSQL = "A select statment"
Me![Course].RowSource = ""
Me![Course].RowSource = dcSQL
End Sub
Private Sub Student_AfterUpdate()
sSQL = "A Select statement"
Me![Course].RowSource = ""
Me![Course].RowSource = sSQL
End Sub
Thanks

How can I populate combo box from a database query?

I want to populate a combo box with the results of a query in Access. I'm just not seeing how to do it. As far as I understand, one must first create a record set, read the query results into the record set, then write the record set to the combo box's row source property. Is this correct? is there a simple example somewhere that I can follow? I haven't found one in any of the other threads.
Here's my attempt so far:
Dim RS As Recordset
Dim myDB As Database
Set RS = myDB.OpenRecordset("SourcesNotDisposed", dbOpenDynaset)
Do While Not RS.EOF
With Me.cmbSN
RowSource.AddItem
End With
Loop
With this code, I'm getting an "Object required" error at the RowSource line. cmbSN has data properties:
Row source Type = Table/Query
Bound Column = 0
Limit to List = Yes
Allow value list edits = Yes
Inherit value list = Yes
Show only row source = No
The query only has one visible column called "Serial Number"
Thanks in advance
Thanks for all the suggestions everyone. I've worked it out and found a very simple solution. With the combo box's Row Source Type property set to Table/Query, all I needed to do was set the Row Source property to a valid SQL string. e.g.:
strSQL = "SELECT Sources.[Serial Number] FROM Sources " & _
"WHERE (((Sources.Nuclide)='Cf-252') " & _
"AND ((Sources.[Location / Status])<>'Disposed')) " & _
"ORDER BY Sources.[Serial Number];"
Me.cmbItem.RowSource = strSQL
Me.cmbItem.Requery
Below code insert table fields into combo box. Add this code under onEnter event of combo
Private Sub CM_Enter()
'CM is combobox name
Dim strItem1 As String
Dim strItem2 As String
On Error Resume Next
Dim i As Integer 'Index for loop
With Me.CM
.RowSourceType = "Value List" 'Set rowsource type as Value list
.RowSource = "" 'Clean combo contents
End With
'Loop through field names of table and add them to your combo:
For i = 1 To CurrentDb.TableDefs("table1").Fields.Count - 1
Me.CM.AddItem (CurrentDb.TableDefs("table1").Fields(i - 1).Name)
Next i
'/***Delete unwanted items from the combo
strItem1 = "col1"
strItem2 = "col2"
'CM.RemoveItem strItem1
'CM.RemoveItem strItem2
End Sub
I think you might need to do a 'first read a record' before starting the loop.
Try using a RS.MoveFirst before the Do-While loop?
I think you may also need to do a .MoveNext inside your loop, just before the Loop statement; it's been a long while since I did anything like this in VBA, but it looks to me like it'll just add the same item over and over until it runs out of memory? I don't think the AddItem moves the record pointer to the next record by itself.
You may also need to check what happens if you MoveNext off the end of the record set...
HTH :)

how to update text field using listbox

i have access 2007 and i want to update a text field [eng] using a list box [List191] which contains 3 values value1,value2,value3
i want when i click on this list and select one or two values i get this values as text separated with (,) in that text field
something like :
Private Sub List191_Click()
Form_tbltest.[eng].Value = Form_tbltest.[eng].Value &","& Form_tbltest.List191.value
End Sub
this code is not working with me , any suggestions ???
If you have a multi-select list box, look at the Access help topics for ListBox.ItemsSelected Property and ListBox.ItemData Property.
In this example, I chose the list box's After Update event for the code. I named my text box txtEng. The code loops through the list box's ItemsSelected collection, and adds the ItemData value for each to a string variable, strEng. After the loop, the leading comma is discarded when the length of that string is > 0. Finally the string's value is assigned to the text box.
Private Sub List191_AfterUpdate()
Dim strEng As String
Dim varItem As Variant
For Each varItem In Me.List191.ItemsSelected
strEng = strEng & "," & Me.List191.ItemData(varItem)
Next
If Len(strEng) > 0 Then
strEng = Mid(strEng, 2) ' discard leading comma
End If
Me.txtEng = strEng
End Sub