lock all in form

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

lock all in form

Post by sal21 »

i run a code from button1.
the code takes a long time for execution.
during this time, i need to not permit to the user to do any other action, click other butto, select from listview....ecc..

naturally lock all in form until the code in button1 is finished...
How to?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: lock all in form

Post by HansV »

See if this does what you want:

Code: Select all

Private Sub Button1_Click()
    LockControls True
    ' Your code goes here
    ' ...
    LockControls False
End Sub

Private Sub LockControls(OnOff As Boolean)
    Dim ctl As Control
    On Error Resume Next
    For Each ctl In Me.Controls
        If ctl.Name <> "Button1" Then
            ctl.Locked = OnOff
        End If
    Next ctl
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: lock all in form

Post by sal21 »

HansV wrote:
25 Jan 2021, 10:54
See if this does what you want:

Code: Select all

Private Sub Button1_Click()
    LockControls True
    ' Your code goes here
    ' ...
    LockControls False
End Sub

Private Sub LockControls(OnOff As Boolean)
    Dim ctl As Control
    On Error Resume Next
    For Each ctl In Me.Controls
        If ctl.Name <> "Button1" Then
            ctl.Locked = OnOff '<<<< ERROR HERE
        End If
    Next ctl
End Sub
You do not have the required permissions to view the files attached to this post.

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: lock all in form

Post by sal21 »

USED enebled and it work...
is a good way?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: lock all in form

Post by HansV »

You omitted the line

On Error Resume Next

But you can use Enabled too.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: lock all in form

Post by sal21 »

HansV wrote:
25 Jan 2021, 12:29
You omitted the line

On Error Resume Next

But you can use Enabled too.
HUMMMM...
How to use this sub as Pulic sub to use for each form in my project?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: lock all in form

Post by HansV »

Change LockControls as follows:

Code: Select all

Public Sub LockControls(frm As Form, OnOff As Boolean)
    Dim ctl As Control
    On Error Resume Next
    For Each ctl In frm.Controls
        If ctl.Name <> "Button1" Then
            ctl.Enabled = OnOff
        End If
    Next ctl
End Sub
Call it like this:

Code: Select all

Private Sub Button1_Click()
    LockControls Me, False
    ' Your code goes here
    ' ...
    LockControls Me, True
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: lock all in form

Post by sal21 »

HansV wrote:
24 Apr 2021, 12:43
Change LockControls as follows:

Code: Select all

Public Sub LockControls(frm As Form, OnOff As Boolean)
    Dim ctl As Control
    On Error Resume Next
    For Each ctl In frm.Controls
        If ctl.Name <> "Button1" Then
            ctl.Enabled = OnOff
        End If
    Next ctl
End Sub
Call it like this:

Code: Select all

Private Sub Button1_Click()
    LockControls Me, False
    ' Your code goes here
    ' ...
    LockControls Me, True
End Sub
EXCELLENT!