Reputation: 1
I want to create a listbox in a form, with multiple selection and checkbox. The liststyle property is not found and the instructions of the web pages do not work : https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/liststyle-property https://learn.microsoft.com/en-us/office/vba/api/outlook.listbox
Upvotes: 0
Views: 382
Reputation: 1321
it is unclear exactly what you are trying to do but format the checkbox and listbox to look like the same control. Then hook them up with vba behind the scenes. I found some of my really old code where I had a non standard data entry form and a bunch of checkboxes. at the time I added a button and hooked everything up in the button click event.
Private Sub cmdSaveLog_Click()
SaveLogEntry cmbHour1, cmbMinutes1, txtNotes1, chkAsleep1, chkSaveEntry1
SaveLogEntry cmbHour2, cmbMinutes2, txtNotes2, chkAsleep2, chkSaveEntry2
SaveLogEntry cmbHour3, cmbMinutes3, txtNotes3, chkAsleep3, chkSaveEntry3
SaveLogEntry cmbHour4, cmbMinutes4, txtNotes4, chkAsleep4, chkSaveEntry4
SaveLogEntry cmbHour5, cmbMinutes5, txtNotes5, chkAsleep5, chkSaveEntry5
SaveLogEntry cmbHour6, cmbMinutes6, txtNotes6, chkAsleep6, chkSaveEntry6
SaveLogEntry cmbHour7, cmbMinutes7, txtNotes7, chkAsleep7, chkSaveEntry7
SaveLogEntry cmbHour8, cmbMinutes8, txtNotes8, chkAsleep8, chkSaveEntry8
SaveLogEntry cmbHour9, cmbMinutes9, txtNotes9, chkAsleep9, chkSaveEntry9
SetFilter
End Sub
Private Sub SaveLogEntry(cmbHours As ComboBox, cmbMinutes As ComboBox, txtNotes As textbox, chkAsleep As CheckBox, chksave As CheckBox)
'note logtime is inserted as a double, txtdetails needs to be quoted in string
'If Trim(cmbHours.value & vbNullString) = vbNullString Then 'textbox empty
If chksave.value = True Then
Dim DailyID As Long
Dim item As Variant
Dim logtime As Double 'insert and dates don't mix
'Dim currentboy As Long
'logtime = CDbl(CDate(cmbDate.value)) + (txtHour.value + txtMinute.value / 60) / 24 'how to convert date, hour, minute to date with time
'logtime = Int(cmbDate.value) + cmbHours.value + cmbMinutes.value
' loop list box
'DailyID = GetDailyID(CLng(lstBoys.Column(0, item)), CDate(cmbDate.value))
DoCmd.SetWarnings False
For Each item In lstBoys.ItemsSelected
DailyID = GetDailyID(CLng(lstBoys.ItemData(item)), CDate(cmbDate.value)) 'group by date at start of shift
If cmbHours.value + cmbMinutes.value > 0.5 Then 'before midnight
'DailyID = GetDailyID(CLng(lstBoys.ItemData(item)), CDate(Int(cmbDate.value)))
'DailyID = GetDailyID(CLng(lstBoys.ItemData(item)), CDate(cmbDate.value))
logtime = CDate(cmbDate.value) + cmbHours.value + cmbMinutes.value
Else
'DailyID = GetDailyID(CLng(lstBoys.ItemData(item)), CDate(Int(DateAdd("d", 1, cmbDate.value))))
'DailyID = GetDailyID(CLng(lstBoys.Column(0, item)), CDate(Int(DateAdd("d", 1, cmbDate.value))))
logtime = DateAdd("d", 1, cmbDate.value) + cmbHours.value + cmbMinutes.value
End If
'DailyID = GetDailyID(CLng(lstBoys.ItemData(item)), CDate(Int(cmbDate.value))) ' for some reason date had a time stamp so int to truncate time
'DailyID = GetCorrectDailyID(CLng(lstBoys.ItemData(item)), CDate(Int(cmbDate.value)), cmbHours.value + cmbMinutes.value) ' for some reason date had a time stamp so int to truncate time
If IsNull(txtNotes) Or txtNotes = "" Then
DoCmd.RunSQL ("INSERT INTO NightLogs ( DailyID, InspectionTime, Asleep, StaffID) " & _
"SELECT " & DailyID & ", " & logtime & ", " & chkAsleep.value & ", " & cmbStaff.value)
Else
DoCmd.RunSQL ("INSERT INTO NightLogs ( DailyID, InspectionTime, Asleep, Details, StaffID ) " & _
"SELECT " & DailyID & ", " & logtime & ", " & chkAsleep.value & ", """ & txtNotes & """" & ", " & cmbStaff.value)
End If
Next item
DoCmd.SetWarnings True
Else
'do nothing
End If
End Sub
Depending on what you want to do you may also be able to use the checkbox click event rather than a button. The point is let the Access controls do the visual stuff they are good at and use VBA to refer to the controls like settings and finish the job.
Upvotes: 0