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?
lock all in form
-
- Administrator
- Posts: 78585
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: lock all in form
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
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: lock all in form
HansV wrote: ↑25 Jan 2021, 10:54See 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.
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: lock all in form
USED enebled and it work...
is a good way?
is a good way?
-
- Administrator
- Posts: 78585
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: lock all in form
You omitted the line
On Error Resume Next
But you can use Enabled too.
On Error Resume Next
But you can use Enabled too.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78585
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: lock all in form
Change LockControls as follows:
Call it like this:
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
Code: Select all
Private Sub Button1_Click()
LockControls Me, False
' Your code goes here
' ...
LockControls Me, True
End Sub
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: lock all in form
EXCELLENT!HansV wrote: ↑24 Apr 2021, 12:43Change LockControls as follows:
Call it like this: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
Code: Select all
Private Sub Button1_Click() LockControls Me, False ' Your code goes here ' ... LockControls Me, True End Sub