unlock textboxes if record is new

tt1611

Registered User.
Local time
Today, 11:33
Joined
Jul 17, 2009
Messages
132
Hi All
This is simple but driving me absolutely mental.

I have a form that loads with certain textboxes locked by default. I am trying to unlock these textboxes if newrecord = true and have the following code


Private Sub Form_Load()
dim ctr as control

if me.newrecord then
if typeof ctr is textbox then
ctr.***(unlocked) = true

end if

end if

end if
End Sub

Well turns out, ctr.locked (the even i am looking for) is not an option and the form generates an error on load.

When I try

dim ctr as textbox
in the declarations, i do have the locked event in the list but that generates a type mismatch error on load.

Am i missing something here?
 
I think you are looking for the 'enabled' flag. I assume the textbox is 'greyed' out, and that is what you mean by locked?
 
No the textboxes are not grayed out. They are locked meaning text can be copied from them but not edited.

I actually need the locked = false event (an event which is true for textboxes) but not available in this case

Thanks
 
Thanks for coming back Yarp but unfortunately thats not what I was looking for.

The main issue I am having is that when I declare a variable as a control and ask access to check the control type. I am not finding an option to unlock the control if the type of control is a textbox. (its not in the snippet list) which normally means its not seeing the control as a textbox even though i have told access that it is

dim ctr as control

if typeof ctr is textbox then
'unlock my text box
ctr.locked(the missing event) = false

I hope this makes sense..Let me know if you need it clarified. Thanks for checking anyways.
 
I found this on another forum, is this what you are looking for:
Code:
Dim ctrl As Control
For Each ctrl In Forms!MyForm.Controls
If ctrl.ControlType = acTextBox Then
' MsgBox ctrl.Name ' used for testing to ensure each control is being read
' MsgBox ctrl.Locked ' used for testing to check if control is locked
ctrl.Locked = Not ctrl.Locked
End If
Next ctrl

Looks like you will have to make some changes, but hopefully this will be the basis for the answer!
P.S. this will lock the control, not unlock it (but I am sure you would of spotted that!)
 
You may also want to refer to the section where the controls reside (which I presume is the Detail section):

Code:
For Each ctrl In Me.Section(acDetail).Controls
 
Thanks for the detailed response. Have been real busy and havent had a chance to test this yet. Will get round to that today and let you know how I got on..
 
By the way, all you do is put the code on the ON CURRENT event of the form:
Code:
private sub Form_Current()
dim ctrl as Control

For Each ctrl In Me.Section(acDetail).Controls
   If ctrl.ControlType = acTextBox and [B][COLOR=Red]ctl.tag[/COLOR][/B] = "lockme" Then
       ctrl.Locked = not me.newrecord
   end if
next
end sub
To identify which controls you want to lock, use the tag property. If you've got a few controls, it would be faster to write out all the controls instead using the loop.
 
If you need to repeatedly operate on a subset of the controls on a form you can also explicitly create an array, in this example exposed as a custom property of the form and built once on first reference ...

Code:
private m_controls as variant

Property Get MyControlArray as Variant
  if IsEmpty(m_controls) then m_controls = Array(me.ctrl1, me.ctrl2, me.ctrl3)
  MyControlArray = m_controls
End Property

Sub LockControls(Optional State as boolean = True)
  Dim var
  For Each var In Me.MyControlArray
    var.Locked = State
  Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom