Toggle Button

D

Dreamer

Hola grupo!

Necesito en un formulario un boton alternar que:


1- Que al abrir el Formulario, el boton alternar Bloquee todos los controles
y demás (CheckBox, TextBox, ComboBox, SubForms, ... ALL) Excepto un boton
+ un cuadro de texto (que son para buscar dentro del form principal)

2- Que al cambiar de registro actual, el boton se vuelva a activar (es
decir, que me bloquee todo nuevamente para evitar cambios en los demás
registros)

3- Ya este ultimo es solo un capricho y consiste en Que el Texto del Boton
cambie, es decir: Si esta activado que diga "Form Bloqueado" y si no lo está
que diga "Form Desbloqueado".


Hello group!

I need in a form, a "Toggle Button" that it does the following thing >>>

1- Which when opening the Form, the Toggle button appear pressed and locks
all the controls and others (CheckBox, TextBox, ComboBox, SubForms... ALL)
Except a Command Button + a TextBox (that are for looking for within
MainForm)

2- Which when changing of present registry, the Toggle button becomes to
already activate (that is to say, that blocks everything again to me to
avoid changes in the other registries)

3- Which the "Caption" of the Button changes, that is to say: If this
activated that says "Blocked Form" and if it is not it that "Unblocked Form"
says.

Thanks a lot, and sorry for my english!

Dreamer.-
 
E

Esteves

Dreamer said:
Hola grupo!

Necesito en un formulario un boton alternar que:


1- Que al abrir el Formulario, el boton alternar Bloquee todos los controles
y demás (CheckBox, TextBox, ComboBox, SubForms, ... ALL) Excepto un boton
+ un cuadro de texto (que son para buscar dentro del form principal)

2- Que al cambiar de registro actual, el boton se vuelva a activar (es
decir, que me bloquee todo nuevamente para evitar cambios en los demás
registros)

3- Ya este ultimo es solo un capricho y consiste en Que el Texto del Boton
cambie, es decir: Si esta activado que diga "Form Bloqueado" y si no lo está
que diga "Form Desbloqueado".


Hello group!

I need in a form, a "Toggle Button" that it does the following thing >>>

1- Which when opening the Form, the Toggle button appear pressed and locks
all the controls and others (CheckBox, TextBox, ComboBox, SubForms... ALL)
Except a Command Button + a TextBox (that are for looking for within
MainForm)

2- Which when changing of present registry, the Toggle button becomes to
already activate (that is to say, that blocks everything again to me to
avoid changes in the other registries)

3- Which the "Caption" of the Button changes, that is to say: If this
activated that says "Blocked Form" and if it is not it that "Unblocked Form"
says.

Thanks a lot, and sorry for my english!

Dreamer.-
 
A

Allen Browne

Since you want to be able to edit one text box, you cannot set the
AllowEdits property of the form. Instead you will need to set the Locked
property of all the controls to Yes.

The function below locks all the bound controls on the form. No change is
made to unbound controls. If there are other controls you want left
unlocked, list them as quotes as the final arguments. The code is recursive,
so if it finds a subform it proceeds down through any levels of subforms and
locks them as well, unless the subform is named in the exception list. It
also sets the AllowDeletions and AllowAdditions of the form and subforms as
well.

This example shows how to tie it to a toggle button named "tgl1", but not
lock a subform named "MySubform", or a combo named "MyCombo":
Private Sub tgl1_AfterUpdate
Call LockBoundControls(Me, Nz(Me.tgl1.Value), "MySubform", "MyCombo)
End Sub

In the Load event of your form, you want to make sure the toggle button is
depressed, and so the form is initially locked:
Private Sub Form_Load()
Me.tgl1.Value = True
Call Sub tgl1_AfterUpdate
End Sub

-----------------------------code starts------------------------------------
Public Function LockBoundControls(frm As Form, bLock As Boolean, _
ParamArray avarExceptionList())
On Error GoTo Err_Handler
'Purpose: Lock the bound controls and prevent deletes on the form any
its subforms.
'Arguments frm = the form to be locked
' bLock = True to lock, False to unlock.
' avarExceptionList: Names of the controls NOT to lock
(variant array of strings).
'Usage: Call LockBoundControls(Me. True)
Dim ctl As Control 'Each control on the form
Dim lngI As Long 'Loop controller.
Dim bSkip As Boolean

'Save any edits.
If frm.Dirty Then
frm.Dirty = False
End If
'Block deletions.
frm.AllowDeletions = Not bLock

For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox, _
acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0& And Not ctl.ControlSource
Like "=*" Then
If ctl.Locked <> bLock Then
ctl.Locked = bLock
End If
End If
End If
End If

Case acSubform
'Recursive call to handle all subforms.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If Len(Nz(ctl.SourceObject, vbNullString)) > 0& Then
ctl.Form.AllowDeletions = Not bLock
ctl.Form.AllowAdditions = Not bLock
Call LockBoundControls(ctl.Form, bLock)
End If
End If

Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl,
acPage, _
acPageBreak, acImage, acObjectFrame
'Do nothing

Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled on " & conMod & " at " &
Now()
End Select
Next

Exit_Handler:
Set ctl = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim vardummy As Variant

On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

-----------------------------code ends------------------------------------
 

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