Locking form fields (1 Viewer)

T-Dal

New member
Local time
Yesterday, 21:19
Joined
Oct 25, 2008
Messages
8
What i want to do here is have a button on a form which locks every field minus the date field for every new record, then the lock can be taken off by pressing another button. The idea of this is to make multiple record entries easier for the user so that the user will only has to alter the date and all other values default will be what they was in the previouse record entry.

http://www.access-programmers.co.uk/forums/showthread.php?t=137158
This sample database puts my idea into practice, - the order id is locked after one record has been inputted, could someone explain to me how that form including the subform was made?

Any input will be appreciated :)
 
Last edited:

CyberLynx

Stuck On My Opinions
Local time
Yesterday, 21:19
Joined
Jan 31, 2008
Messages
585
http://www.access-programmers.co.uk/...d.php?t=137158
This sample database puts my idea into practice, - the order id is locked after one record has been inputted, could someone explain to me how that form including the subform was made?

The Best person to ask is in fact the person the created it. As you know (or may not know) ajetrumpet is a regular participant within this very Forum.

Bottom line though, the Sub Form is a Sub Form of the Main Form In DataSheet View with no Master and Child Links between the SubForm and the Main Form. Everything is first placed into a Temporary Table which is the Record Source (bound to) for Main Form. The SubForm's Record Source is based from a Query which pulls its Data from the very same Temporary Table. When the User selects the Complete Order button, code beneath the OnClick event of that button fires a dynamically created INSERT query which ultimately stores the Records held within that Temporary Table into the Main Table the data is intended for, the Orders Table.

The Order ID Text Box is Disabled once the first order has been placed so that it can not be tampered with until the Complete Order button is selected which in turn then clears the Main Form so the the User can enter a New Order ID and start a completely new order or enter into a preexisting order number so as to add to that order.

Keep in mind this is just a sample to illustrate how you could go about entering multiple records. There is a lot more required to make this completely functional in a full blown Database.

Moving onward.....

I don't know how many Controls you have on your Form and now I'm not sure if you want to Lock those Form Fields or disable them but one way would be to enumerate through the Form's Controls and disable them all except the SubForm Control and the Date Text Box you want to leave Enabled.

When the so called other button is selected then enumerate through the Form's Controls again and enable them. You could created a Function to do tis so that you don't need to duplicate the code which does it. Perhaps something like this:

Code:
Public Function SetAllControlsTo(Frm As Form, _
                                 Prop As String, _
                                 PropState As Boolean, _
                                 Optional ByVal CtrlList As Variant)
   Dim Ctrl As Control
   Dim SetFocusTo As Control
   Dim HasProp As Boolean
   Dim CtrlsArray() As String
   Dim P As Property
   Dim I As Integer, SkipIt As Integer
  
   
   If Len(Nz(CtrlList, "")) <> 0 Then
      CtrlsArray = Split(CtrlList, ",")
      On Error Resume Next
      Frm.Controls(CtrlsArray(0)).SetFocus
      If Err.Number = 2465 Then
         Err.Clear
         MsgBox "Can not locate the Control specified within the" & vbCr & _
                "'CtrlList' argument.", vbExclamation, "Control Not Found"
         Exit Function
      End If
   Else
      If Prop = "Enabled" Then
         MsgBox "'SetAllControlsTo' Function Error:" & vbCr & vbCr & _
                "When Disabling all  Controls on a Form you must" & vbCr & _
                "have at  least one Control to  set Focus to since" & vbCr & _
                "any Control on Form that contains Focus can not" & vbCr & _
                "be disabled and will generate an Error." & vbCr & vbCr & _
                "Place  the name of the  Control to  set  Focus  to" & vbCr & _
                "into the 'CtrlList' argument.", vbExclamation, "No " & _
                "Focus Element"
         Exit Function
      End If
   End If
   
   For Each Ctrl In Frm.Controls
      If Len(Nz(CtrlList, "")) <> 0 Then
         For I = LBound(CtrlsArray) To UBound(CtrlsArray)
            If Ctrl.Name = CtrlsArray(I) Then SkipIt = 1: Exit For
         Next I
         If SkipIt = 1 Then GoTo SkippedControl
      End If
      
      GoSub DoesItHaveProperty
      If HasProp Then
         Ctrl.Properties(Prop) = PropState
      End If
      HasProp = False
     
SkippedControl:
   Next Ctrl
   
Exit_DisableLockControls:
   Set Ctrl = Nothing
   Set P = Nothing
   Erase CtrlsArray
   Exit Function
   
DoesItHaveProperty:
   For Each P In Ctrl.Properties
      If P.Name = Prop Then HasProp = True: Exit For
   Next P
   Return
End Function

Now, as you can see I use a Gosub in the above Function. Usually I never do this, I would normally create a function to carry out the task rather than use a Gosub but demonstration purposes I decided to keep it compacted into a single function.

What this Function does is it allows you to set a specific property for every control located on a specified Form. A property name is passed to the Function and the supplied state (or value) in which you would like that property to be set at. As the Function enumerate through each Control on the specified Form it checks to see if the supplied property actually exists for each control encountered. If it does the property is set. If it doesn't then the Control is bypassed.

The above Function also allows you to supply a list of Control Names (the name of he Control contained within the Name Property). You can supply as many Control Control Names as you like as long as they are delimited by a comma with NO SPACES. You must supply at least one control name if you are setting the Enabled property since you cannot disable a Control in Form if it contains Focus.

Why this Function? With it, you can decide which method you would like your Controls on Form to be Disabled. Either by way of the Enabled property or the Locked property (where no editing can take place).

How to use it:

Call SetAllControlsTo(Me, "Enabled", False, "MyControlName1,MyControlName2)


Result: Disables all Controls on Form except the Controls named MyControlName1 and MyControlName2.

OR

Call SetAllControlsTo(Me, "Enabled", False, "MyControlName1)


Result: Disables all Controls on Form except the Control named MyControlName1.

OR

Call SetAllControlsTo(Me, "Enabled", True, "MyControlName1,MyControlName2)[/B]

Result: Enables all Controls on Form except the Controls named MyControlName1 and MyControlName2.

OR

Call SetAllControlsTo(Me, "Locked", True)

Result: Locks all Controls on Form.

OR

Call SetAllControlsTo(Me, "Locked", False)

Result: UnLocks all Controls on Form.


The first argument is to contain the Form object itself, Me is used.
The second argument is the name of the Property ot which we want to change the Property value of.
The third argument is the Property value to be.
The fourth argument is Optional. It would contain a delimited list of Controls we don't want processed. If nothing is supplied then the specified property for all Controls on the specified Form are changed to the Specified Value (if the property exists).

You would run this Function again within the OnClick event of your second button so as to Enable or Unlock the Controls in Form.

Hope this helps....somewhat

.
 

Users who are viewing this thread

Top Bottom