Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316

    Unanswered: Disabling fields in multiple sub-forms

    I have the following routine to disable all controls in a form:

    Code:
    Dim ctl As Control, bolEnabled, bolLocked As Boolean, lngForeColor As Long
    
    'Hide key record and disable fields if key record is the current record.
    If txtPayNumber = 0 Then
        bolEnabled = False
        bolLocked = True
        lngForeColor = 16777215
    Else
        bolEnabled = True
        bolLocked = False
        lngForeColor = 0
    End If
    
    For Each ctl In Me
    If ctl.ControlType = acTextBox Or _
    ctl.ControlType = acComboBox Then
        ctl.Enabled = bolEnabled
        ctl.ForeColor = lngForeColor
    End If
    Next
    Can anyone help me modify this to Enable/Lock all fields in any subforms in Me? Help much appreciated.

  2. #2
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238

    Re: Disabling fields in multiple sub-forms

    Originally posted by bcass
    I have the following routine to disable all controls in a form:

    Code:
    Dim ctl As Control, bolEnabled, bolLocked As Boolean, lngForeColor As Long
    
    'Hide key record and disable fields if key record is the current record.
    If txtPayNumber = 0 Then
        bolEnabled = False
        bolLocked = True
        lngForeColor = 16777215
    Else
        bolEnabled = True
        bolLocked = False
        lngForeColor = 0
    End If
    
    For Each ctl In Me
    If ctl.ControlType = acTextBox Or _
    ctl.ControlType = acComboBox Then
        ctl.Enabled = bolEnabled
        ctl.ForeColor = lngForeColor
    End If
    Next
    Can anyone help me modify this to Enable/Lock all fields in any subforms in Me? Help much appreciated.
    You can use the following code:
    For Each ctl In Me("SubForm").Form
    If ctl.ControlType = acTextBox Or _
    ctl.ControlType = acComboBox Then
    ctl.Enabled = bolEnabled
    ctl.ForeColor = lngForeColor
    End If

    The difference being the line For Each ctl In Me("SubForm").Form to refer to all the controls on the subform

  3. #3
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    So do I have to have that block of code for each subform on my form (there are 5)? Or is there someway to refer to all controls in all subforms in the same loop as the one which enables/disables all fields in the main form?

  4. #4
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238
    Originally posted by bcass
    So do I have to have that block of code for each subform on my form (there are 5)? Or is there someway to refer to all controls in all subforms in the same loop as the one which enables/disables all fields in the main form?
    You would, but to simplify the code you could put the disable/enable code into a separate function and call that from the For Each loop. For example

    Private Function EnableControl(frm As Access.Form, _
    fEnableControl As Boolean, lngForecolor As Long) As Boolean
    Dim ctl As Access.Control
    On Error GoTo ErrorHere
    For Each ctl In frm
    With ctl
    If .ControlType = acTextBox Or _
    .ControlType = acComboBox Then
    .Enabled = fEnableControl
    .ForeColor = lngForecolor
    End If
    End With
    Next ctl
    EnableControl = True
    ExitHere:
    Set ctl = Nothing
    Exit Function
    ErrorHere:
    MsgBox "Error In: Form '" & Me.Name & "'" & vbCrLf _
    & "Procedure: EnableControl" & _
    vbCrLf & "Error Code: " & Err.Number & _
    vbCrLf & "Error: " & Err.Description, vbExclamation, "Error Alert"
    EnableControl = False
    Resume ExitHere
    End Function

    Then you can call the code for each sub form using either of the following methods

    Call EnableControl(Me("SubForm_1").Form, True, 0)
    If Call EnableControl(Me("SubForm_2").Form, True, 0) Then
    MsgBox "The Controls Were changed"
    Else
    Msgbox "The Controls Were Not Changed"
    End If

  5. #5
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    Thats great. Thanks for the help.

  6. #6
    Join Date
    Oct 2003
    Location
    São Paulo - Brazil
    Posts
    91
    Originally posted by bcass
    Thats great. Thanks for the help.
    Another way to do the same, to a sub-form:

    In a form event write:

    Me.subformName.Form.AllowEdits = False

    Your users will see, but won´t be albe to edit sub-form´s fields
    Last edited by Mixirica; 11-10-03 at 12:06.
    My environment: Windows XP/ Access 2000 - Using Microsoft DAO 3.6 Library

Posting Permissions

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