Want to avoid the possibilty to edit field in listbox

H

Hallgeir

I fill my listbox with code example found on the internet written by Zameer
Abdulla, LVLoadData.mdb. I'm not good at programming, only good at copying
code :). When I click twice on the same row in the listbox it becomes
possible to edit the first column field. How can I avoid this. Appreciate any
tips. Code below:

Public Sub FillEmployees()
On Error GoTo ErrorHandler

'set variables
Dim rs As DAO.Recordset
Dim db As Database
Dim lstItem As ListItem
Dim strSQL As String

Set db = CurrentDb()
strSQL = "SELECT * FROM Employees where Lastname Like '*" &
Forms!frmlistviewemployees!txtSearch1 & "*'"
Set rs = db.OpenRecordset(strSQL)

With Me.ListView1
'Set ListView style
.View = lvwReport
'This is not supported by ListView 5
.GridLines = True
.FullRowSelect = True
'Clear Header and ListItems
.ListItems.Clear
.ColumnHeaders.Clear
End With
'Set up column headers
With Me.ListView1.ColumnHeaders
.Add , , "Emp ID", 1000, lvwColumnLeft
.Add , , "Salutation", 700, lvwColumnLeft
.Add , , "Last Name", 2000, lvwColumnLeft
.Add , , "First Name", 2000, lvwColumnLeft
.Add , , "Hire Date", 1500, lvwColumnRight
End With
' Add items and subitems to list control.

rs.MoveFirst
Do Until rs.EOF
Set lstItem = Me.ListView1.ListItems.Add()
lstItem.Text = rs!EmployeeID
lstItem.SubItems(1) = Nz(rs!TitleOfCourtesy, "N/A")
lstItem.SubItems(2) = Nz(Trim(rs!LastName))
lstItem.SubItems(3) = Nz(Trim(rs!FirstName))
lstItem.SubItems(4) = Format(rs!HireDate, "Medium Date")
rs.MoveNext
Loop
'close recordset
rs.Close
DoCmd.Echo True
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 3021 Then ' no current record
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If

End Sub
 
L

Larry Linson

I rarely, if ever, use a List Box. I prefer to use the Combo Box because it
requires less "screen real estate" -- using a Combo Box, set the Limit To
List property to Yes; then, unless you provide a NotInList event, the user
cannot edit the information in the Combo Box drop-down list. And, the
Control will not take up as much of the screen as would a List Box.
 
M

mat

Your listbox is an activex control; Larry didn't catch that. An Access
listbox control does not really have columns, and the issue you report
would not arise.

Many ActiveX controls let you into the properties dialog for the control
when you doubleclick in design view. There you may be able to set a
property like 'editable' or 'enable edits'. Likewise you'd be able to
set that property via code but since you're not a code hound, the
property dialog might be a better choice.

You could convert to using an Access listbox, they are pretty simple to
work with. I find listboxes to be quite useful, but I guess not everyone
does <g>.
 
S

Stuart McCall

Hallgeir said:
I fill my listbox with code example found on the internet written by Zameer
Abdulla, LVLoadData.mdb. I'm not good at programming, only good at copying
code :). When I click twice on the same row in the listbox it becomes
possible to edit the first column field. How can I avoid this. Appreciate
any
tips. Code below:

Public Sub FillEmployees()
On Error GoTo ErrorHandler

'set variables
Dim rs As DAO.Recordset
Dim db As Database
Dim lstItem As ListItem
Dim strSQL As String

Set db = CurrentDb()
strSQL = "SELECT * FROM Employees where Lastname Like '*" &
Forms!frmlistviewemployees!txtSearch1 & "*'"
Set rs = db.OpenRecordset(strSQL)

With Me.ListView1
'Set ListView style
.View = lvwReport
'This is not supported by ListView 5
.GridLines = True
.FullRowSelect = True
'Clear Header and ListItems
.ListItems.Clear
.ColumnHeaders.Clear
End With
'Set up column headers
With Me.ListView1.ColumnHeaders
.Add , , "Emp ID", 1000, lvwColumnLeft
.Add , , "Salutation", 700, lvwColumnLeft
.Add , , "Last Name", 2000, lvwColumnLeft
.Add , , "First Name", 2000, lvwColumnLeft
.Add , , "Hire Date", 1500, lvwColumnRight
End With
' Add items and subitems to list control.

rs.MoveFirst
Do Until rs.EOF
Set lstItem = Me.ListView1.ListItems.Add()
lstItem.Text = rs!EmployeeID
lstItem.SubItems(1) = Nz(rs!TitleOfCourtesy, "N/A")
lstItem.SubItems(2) = Nz(Trim(rs!LastName))
lstItem.SubItems(3) = Nz(Trim(rs!FirstName))
lstItem.SubItems(4) = Format(rs!HireDate, "Medium Date")
rs.MoveNext
Loop
'close recordset
rs.Close
DoCmd.Echo True
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 3021 Then ' no current record
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " &
Err.Description
Resume ErrorHandlerExit
End If

End Sub

In your With Me.ListView1 block:

..LabelEdit = lvwManual
 
H

Hallgeir

Thank you folks!
Sorry that I confused you buy calling it a listbox. I know better. Correct
me if I'm wrong but i have got the understanding that an activex listview
control has more possibilties than the access listbox. Stuarts tips did the
magic so I stick to the listview for the moment .
 
M

mat

Thank you folks!
Sorry that I confused you buy calling it a listbox. I know better. Correct
me if I'm wrong but i have got the understanding that an activex listview
control has more possibilties than the access listbox. Stuarts tips did the
magic so I stick to the listview for the moment .

Yes, the listview ActiveX control is more versatile; the Access listbox
control is easier for some common uses.
 
Top