Form/Controls Display in Edit or Add Mode

R

Rich1234

Hi everybody

Is it possible to have, say, the background and borders of all textboxes to
be set to transparent when the form is in "view only"
(allowadditions/edits/deletions set to no) and then have all borders and
backgrounds appear when the form is in edit/add/delete mode?
I know how to do this "manually" (ie setting these values for each control
individually when the form enters say allow edits) - but is there a
quicker/easier/ "generic" way to change the display of the form controls as
soon as the form changes mode? Is it possible to define one template for how
each field will appear in each mode?
If this is more complexity than it is worth, I might just do each control
individually.
Looking forward to your advice
rich
 
J

Justin Hoffman

Rich1234 said:
Hi everybody

Is it possible to have, say, the background and borders of all textboxes
to
be set to transparent when the form is in "view only"
(allowadditions/edits/deletions set to no) and then have all borders and
backgrounds appear when the form is in edit/add/delete mode?
I know how to do this "manually" (ie setting these values for each control
individually when the form enters say allow edits) - but is there a
quicker/easier/ "generic" way to change the display of the form controls
as
soon as the form changes mode? Is it possible to define one template for
how
each field will appear in each mode?
If this is more complexity than it is worth, I might just do each control
individually.
Looking forward to your advice
rich



Write a single piece of code to handle all forms in a standard way. Eg cut
and paste this code into a new module. You can then call it from inside the
form like SetEditMode Me, False. It is only a start as it only defines how
textboxes look - but you can see how to expand it for other controls as
well.


Public Sub SetEditMode(frm As Form, EditModeOn As Boolean)

On Error GoTo Err_Handler

Dim ctl As Control

If SysCmd(acSysCmdGetObjectState, acForm, frm.Name) = 0 Then
MsgBox "Form '" & frm.Name & "' is not loaded", _
vbExclamation, "Cannot set edit mode"
Exit Sub
End If

With frm
.AllowAdditions = EditModeOn
.AllowDeletions = EditModeOn
.AllowEdits = EditModeOn
End With

For Each ctl In frm.Controls

With ctl
Select Case .ControlType

Case acLabel

Case acTextBox

If EditModeOn Then
.BorderStyle = 1 ' Solid
.SpecialEffect = 2 ' Sunken
Else
.BorderStyle = 0 ' Transparent
.SpecialEffect = 0 ' Flat
End If

Case acComboBox

Case acCheckBox

End Select
End With

Next ctl

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
 
6

'69 Camaro

Hi, Rich.
Is it possible to have, say, the background and borders of all textboxes to
be set to transparent when the form is in "view only"
(allowadditions/edits/deletions set to no) and then have all borders and
backgrounds appear when the form is in edit/add/delete mode?

Yes. One may create a button to toggle this state back and forth. In the
form, try:

Private Sub ViewOrEditModeBtn_Click()

On Error GoTo ErrHandler

Static fToggle As Boolean

If (fToggle) Then
Call setEditOrViewMode(Me, "Edit")
Else
Call setEditOrViewMode(Me, "View")
End If

fToggle = Not (fToggle)

Exit Sub

ErrHandler:

MsgBox "Error in ViewOrEditModeBtn_Click( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

.. . . where the button is named ViewOrEditModeBtn.

In a standard module, try:

Public Sub setEditOrViewMode(frm As Form, sMode As String)

On Error GoTo ErrHandler

Dim idx As Long

'---------------------------------------------------------
' Determine whether in "Edit/Add" mode.
'---------------------------------------------------------

If (sMode = "Edit") Then
frm.AllowAdditions = True
frm.AllowDeletions = True
frm.AllowEdits = True

For idx = 0 To (frm.Controls.Count - 1)
If (frm.Controls.Item(idx).ControlType = acTextBox) Then
frm.Controls.Item(idx).BackStyle = 1
frm.Controls.Item(idx).BorderStyle = 1
End If
Next idx

'---------------------------------------------------------
' Determine whether in "View" mode.
'---------------------------------------------------------

ElseIf (sMode = "View") Then
frm.AllowAdditions = False
frm.AllowDeletions = False
frm.AllowEdits = False

For idx = 0 To (frm.Controls.Count - 1)
If (frm.Controls.Item(idx).ControlType = acTextBox) Then
frm.Controls.Item(idx).SetFocus
frm.Controls.Item(idx).BackStyle = 0
frm.Controls.Item(idx).BorderStyle = 0
End If
Next idx
End If

Exit Sub

ErrHandler:

MsgBox "Error in setEditOrViewMode( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Joined
Jan 28, 2022
Messages
1
Reaction score
0
I don't know if anyone is moderating this. Justin's suggested solution works perfectly for what was asked - I love it, but it prevents new records from being added. (If I remove the module and commands, the new record function works perfectly) Any ideas how to fix?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top