Restricting data Entry on a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RaoAli
    New Member
    • Feb 2015
    • 16

    #1

    Restricting data Entry on a form

    I have form to record payment from a customer. I want to lock the payments details in text boxes on the form unless the user selects some customer from a customers combo.
    hope i conveyed my idea :)
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    I would recommend using this code provided by Allen Browne: http://allenbrowne.com/ser-56.html

    Create a Subroutine that Locks the Form if a Customer isn't selected, and Unlocks if the Customer is selected. Then call the Subroutine from the Form's OnCurrent event and the Customer ComboBox AfterUpdate Event.
    The Sub could look like this:
    Code:
    Private Sub secureForm()
        LockBoundControls(Me, (Len(Me.cboCustomer.Value)=0), "cboCustomer")
    End Sub
    Last edited by jforbes; Feb 23 '15, 01:25 PM. Reason: added example

    Comment

    • RaoAli
      New Member
      • Feb 2015
      • 16

      #3
      thank you for the response, but what is this Lockboundcontro l()function?

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        The LockBoundContro ls() Function is provided by the above link.
        Here is a copy of it, but the link will give greater detail of what is happening:
        Code:
        Public Function LockBoundControls(frm As Form, bLock As Boolean, ParamArray avarExceptionList())
        On Error GoTo Err_Handler
            'Purpose:   Lock the bound controls and prevent deletes on the form any its subforms.
            'Arguments  frm = the form to be locked
            '           bLock = True to lock, False to unlock.
            '           avarExceptionList: Names of the controls NOT to lock (variant array of strings).
            'Usage:     Call LockBoundControls(Me. True)
            Dim ctl As Control      'Each control on the form
            Dim lngI As Long        'Loop controller.
            Dim bSkip As Boolean
            
            'Save any edits.
            If frm.Dirty Then
                frm.Dirty = False
            End If
            'Block deletions.
            frm.AllowDeletions = Not bLock
            
            For Each ctl In frm.Controls
                Select Case ctl.ControlType
                Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox, acOptionButton, acToggleButton
                    'Lock/unlock these controls if bound to fields.
                    bSkip = False
                    For lngI = LBound(avarExceptionList) To UBound(avarExceptionList)
                        If avarExceptionList(lngI) = ctl.Name Then
                            bSkip = True
                            Exit For
                        End If
                    Next
                    If Not bSkip Then
                        If HasProperty(ctl, "ControlSource") Then
                            If Len(ctl.ControlSource) > 0 And Not ctl.ControlSource Like "=*" Then
                                If ctl.Locked <> bLock Then
                                    ctl.Locked = bLock
                                End If
                            End If
                        End If
                    End If
                    
                Case acSubform
                    'Recursive call to handle all subforms.
                    bSkip = False
                    For lngI = LBound(avarExceptionList) To UBound(avarExceptionList)
                        If avarExceptionList(lngI) = ctl.Name Then
                            bSkip = True
                            Exit For
                        End If
                    Next
                    If Not bSkip Then
                        If Len(Nz(ctl.SourceObject, vbNullString)) > 0 Then
                            ctl.Form.AllowDeletions = Not bLock
                            ctl.Form.AllowAdditions = Not bLock
                            Call LockBoundControls(ctl.Form, bLock)
                        End If
                    End If
                    
                Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl, acPage, acPageBreak, acImage, acObjectFrame
                    'Do nothing
                    
                Case Else
                    'Includes acBoundObjectFrame, acCustomControl
                    Debug.Print ctl.Name & " not handled " & Now()
                End Select
            Next
            
            'Set the visual indicators on the form.
            On Error Resume Next
            frm.cmdLock.Caption = IIf(bLock, "Un&lock", "&Lock")
            frm!rctLock.Visible = bLock
            
        
        Exit_Handler:
            Set ctl = Nothing
            Exit Function
        
        Err_Handler:
            MsgBox "Error " & Err.Number & " - " & Err.Description
            Resume Exit_Handler
        End Function
        
        Public Function HasProperty(obj As Object, strPropName As String) As Boolean
            'Purpose:   Return true if the object has the property.
            Dim varDummy As Variant
            On Error Resume Next
            varDummy = obj.Properties(strPropName)
            HasProperty = (Err.Number = 0)
        End Function

        Comment

        • RaoAli
          New Member
          • Feb 2015
          • 16

          #5
          I got this and worked fine...thank you once again :)

          Comment

          Working...