Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lock text's on form (A2k)

    A few days ago, a member posted a request to loop thru text's on a form and manipulate them as neccessary.
    I've tried finding the post but can't remember who posted it.
    I think it could have been Charlotte.

    In some of my pop-up admin forms, I have locked the text boxes(from properties) so the user cannot edit them unless a certain keystroke is pressed (vbKeyF3).

    The code posted earlier would help me to unlock these items within the keypress rather than:

    Text1.Locked = False
    Text2.Locked = False
    Text3.Locked = False
    Text4.Locked = False etc etc

    Would one of the Moderators help out, I think you still have a way of searching the forum.
    Thanks in haste

  2. #2
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    I found this in one of my folders which was a private sub.
    I've changed it to a Public Function in the modules collection but errors out on the line for some reason: <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    For Each ctl In Controls



    Option Compare Database
    Option Explicit
    Public Function LockControls()
    Dim ctl As Control
    For Each ctl In Controls
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox, acToggleButton, acListBox, acOptionGroup, acSubform
    ctl.Locked = True
    Case acCommandButton, acTabCtl
    'ctl.enabled = False
    Case Else
    'skip lines, boxes, bound frames, etc.
    End Select
    Next ctl
    End Function

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    No, the moderators can't search any more than you can!

    If your text boxes are really named Text1, Text2, ..., Text37, you can use something like this:

    Dim i As Integer
    For i = 1 To 37
    Me.Controls("Text" & i).Locked = False
    Next i

    If they have different names, you can unlock all text boxes on the form as follows:

    Dim ctl As Control
    For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
    ctl.Locked = False
    End If
    Next ctl
    Set ctl = Nothing

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    You can't put it in a standard module "as is" because Controls is a property of a form (or report). It makes no sense outside of the context of the form. You can modify it for more general use, though, by passing the form it must be applied to as an argument:

    Public Function LockControls(frm As Form)
    Dim ctl As Control
    For Each ctl in frm.Controls
    ...
    End Function

    Call it like this in the code behind a form:

    LockControls Me

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Thanks Hans

    The second version is exactly what I wanted.
    This will save me loads of time, and I've created a "UnlockControls" also <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Option Compare Database
    Option Explicit
    Public Function LockControls(frm As Form)
    Dim ctl As Control
    For Each ctl In frm.controls
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox, acToggleButton, acListBox, acOptionGroup, acSubform
    ctl.Locked = True
    Case acCommandButton, acTabCtl
    'ctl.enabled = False
    Case Else
    'skip lines, boxes, bound frames, etc.
    End Select
    Next ctl
    End Function

    Option Compare Database
    Option Explicit
    Public Function UnLockControls(frm As Form)
    Dim ctl As Control
    For Each ctl In frm.controls
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox, acToggleButton, acListBox, acOptionGroup, acSubform
    ctl.Locked = False
    Case acCommandButton, acTabCtl
    'ctl.enabled = False
    Case Else
    'skip lines, boxes, bound frames, etc.
    End Select
    Next ctl
    End Function

  6. #6
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Hans
    Thats even more interesting.
    That for the update.

    (Damn, I've just changed about a dozen forms before reading it, Nevermind)

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    Dave,

    It can be even more economical by having a second argument that specifies whether to lock or unlock the controls:

    Public Function LockControls(frm As Form, Flag As Boolean)
    Dim ctl As Control
    For Each ctl In frm.controls
    Select Case ctl.ControlType
    Case acTextBox, acCheckBox, acComboBox, acToggleButton, acListBox, acOptionGroup, acSubform
    ctl.Locked = Flag
    Case Else
    ' Skip everything else
    End Select
    Next ctl
    End Function

    Use LockControls Me, True to lock controls, and LockControls Me, False to unlock controls.

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Lock text's on form (A2k)

    What this what you are talking about?
    Charlotte

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Charlotte
    It wasn't the one I was referring to but very interesting any-way.
    I've sorted the problem out now though.

    Thanks for the extended help.

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Hans
    With the help of your prev posts in reference to locking controls.
    With the process of Unlocking the controls, enabling the user to add or edit data, I needed a way to let the user choose, whether to Edit or Create New.

    I came up with this, an unbound form with only two buttons, "Edit" , "New".
    As I didn't only, want to hide the form, I wanted to close it instead and be able to use it with any other form within the database.
    I've deleted the .Visible = False line and added my own, DoCmd.Close acForm, Me.name.
    So the form opens fresh every time.

    Heres the code:

    Option Compare Database
    Option Explicit
    Private Sub cmdEdit_Click()
    Dim T As String
    T = Me.Tag
    If T <> "" Then
    DoCmd.Close acForm, Me.Name
    Forms(T).SetFocus
    UnLockControls Forms(T)
    End If
    End Sub

    Private Sub cmdNew_Click()
    Dim T As String
    T = Me.Tag
    If T <> "" Then
    DoCmd.Close acForm, Me.Name
    Forms(T).SetFocus
    UnLockControls Forms(T)
    DoCmd.GoToRecord , , acNewRec
    End If
    End Sub

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyF10
    DoCmd.Close acForm, Me.Name
    End Select
    End Sub

    Private Sub Form_Open(Cancel As Integer)
    DoCmd.MoveSize Right:=7980, Down:=3945
    If Not IsNull(Me.OpenArgs) Then
    Me.Tag = Me.OpenArgs
    End If
    End Sub

    A couple of questions.

    1. Does the code look OK (for other forms as well) (quite pleased with myself)

    2. When the form closes and sets focus, is there a line I should add to setfocus on the first text on the form, without naming it explicitly (hoping again, to pick the first text of any form)

    This little utility if re-usable, will hopefully prevent me from re-creating it many times.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    Dave,

    1. I haven't actually tested your code, but it looks OK.

    2. You could check the TabOrder property. This is 0 for the first control in the tab order, 1 for the second control, etc. Insert the following into UnlockControls after the line ctl.Locked = False:

    If ctl.TabIndex = 0 Then
    ctl.SetFocus
    End If

    This is not 100% dependable - if the first control in the tab order is not one of those you unlock, nothing happens - but it should work in most cases.

  12. #12
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Hans
    I've added the code as you suggested, it momentarily works, then disapears.
    If I click the detail of the form I regain focus on the control (Text).

    The TabIndex is correct on the form I am testing.
    Something down the line is interfering with the code .

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    Does this happen when you click cmdEdit, or cmdNew, or both?

  14. #14
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lock text's on form (A2k)

    Both ??

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lock text's on form (A2k)

    This kind of thing can be difficult to pin down, because there are so many events involved, and single-stepping through their code may not be exactly equivalent to running them. But still, perhaps you can find out what's happening by setting a breakpoint in the code and single-stepping through it (F8). If you get stuck in the For Each ctl In frm.Controls loop, you can put the insertion point below the loop and use Debug | Run to Cursor (Ctrl+F8).

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •