Need Help to rectify following vba coding

bkhawaja

New member
Local time
Today, 16:24
Joined
Nov 2, 2018
Messages
3
I have a continuous form in which 3 fields are editable. I want to prevent it from being edit with condition. To do this I used following code

PHP:

Dim ctl As Access.Control

For Each ctl In screen.activeform
If ctl.Tag = "edit" Then
If Me.PBY <> Forms!nhome!EID Then
MsgBox "not acceptable"
ctl.Locked = True
Else
ctl.Locked = False
End If
End If
Next ctl

Error_Handler_Exit:
On Error Resume Next
If Not ctl Is Nothing Then Set ctl = Nothing
Exit Sub

It works well if i remove msgbox but with msgbox it doesnot work. Also when I click on checkbox it show msgbox
 
The message box receives focus meaning that screen.activeform isn't current anymore.

You would be better to simply make a warning label on the form visible / invisible based on the check you are carrying out. Having to click a through a message box when you make an error is not very user friendly.
 
I agree with Minty. However, there is usually more than one way to skin a cat.

Code:
Dim ctl As Access.Control
[COLOR="Red"]Dim frm as Access.Form[/COLOR]

[COLOR="red"]set frm = screen.activeform[/COLOR]
For Each ctl In [COLOR="Red"]frm[/COLOR]
If ctl.Tag = "edit" Then
If Me.PBY <> Forms!nhome!EID Then
MsgBox "not acceptable"
ctl.Locked = True
Else
ctl.Locked = False
End If
End If
Next ctl

Error_Handler_Exit:
On Error Resume Next
If Not ctl Is Nothing Then Set ctl = Nothing
Exit Sub

Capturing the form reference outside the "For Each" loop no longer depends on the form retaining focus because now you have a relatively more persistent reference to it, as opposed to something that always depends on the dynamic focus on your screen.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom