473,500 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lock fields when current record is not a new record

9 New Member
When a form is opened fields are locked. When a new record is added (by pressing an add new record button), fields are unlocked. When the record is saved the fields are then locked again.

However, if a user click the add new record button then the user uses the navagation button at the bottom of the screen to navigate to a previous record, the locked fields are unlocked because a new record was going to be created but in fact never was. How can I prevent this from happening?

My create new record code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddNewRec_Click()
  2.  
  3.     DoCmd.GoToRecord , , acNewRec
  4.  
  5.     If Me.NewRecord Then
  6.     Me.btnSaveNewRec.Visible = True
  7.     Me.[txtCoordArea].Locked = False
  8.     Me.[Full Name].Locked = False
  9.     Me.[Badge ID].Locked = False
  10.     Me.[SSN Last 5].Locked = False
  11.     Me.[Term Date].Locked = False
  12.     Me.[Requester Name].Locked = False
  13.     Me.[Supplier].Locked = False
  14.  
  15.     Else
  16.     If Not Me.NewRecord Then
  17.     Me.[txtCoordArea].Locked = True
  18.     Me.[Full Name].Locked = True
  19.     Me.[Badge ID].Locked = True
  20.     Me.[SSN Last 5].Locked = True
  21.     Me.[Term Date].Locked = True
  22.     Me.[Requester Name].Locked = True
  23.     Me.[Supplier].Locked = True
  24.  
  25.     End If
  26.  
  27.     End If
  28.  
  29. End Sub
  30.  
My Save New Record Code is:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. On Error GoTo ErrorMessage
  3.  
  4. Dim LResponse As Integer
  5.  
  6.     If Me.Dirty Then LResponse = MsgBox("Do you wish to SAVE your changes?", vbYesNo)
  7.  
  8.         If LResponse = vbYes Then    'User chose Yes - Updated
  9.         [txtDateRecordUpdated].Value = Now()
  10.         [RecUpdated].Value = True
  11.         [txtRecordUpdatedBy].Value = Forms!frmUtility!Full_Name
  12.         DoCmd.RunCommand acCmdSave
  13.  
  14.     Else    'User chose No - Not Updated
  15.  
  16.     End If
  17.  
  18.     Me.[txtCoordArea].Locked = True
  19.     Me.[Full Name].Locked = True
  20.     Me.[Badge ID].Locked = True
  21.     Me.[SSN Last 5].Locked = True
  22.     Me.[Term Date].Locked = True
  23.     Me.[Requester Name].Locked = True
  24.     Me.[Supplier].Locked = True
  25.  
  26.     Me.btnSaveNewRec.Visible = False
  27.  
  28. Exit_Form_BeforeUpdate:
  29.     Exit Sub
  30.  
  31. ErrorMessage:
  32.     MsgBox Err.Description
  33.     Resume Exit_Form_BeforeUpdate
  34.  
  35. End Sub
  36.  
Any assistance would be greatly appreciated.
Nov 14 '13 #1
5 5215
chip0105
9 New Member
After I took a short break and stepped away from my coding, I realized I could perform this function using the following:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. '
  3.    If Not Me.NewRecord Then
  4.       Me.btnSaveNewRec.Visible = False
  5. '
  6.       Me.[txtCoordArea].Locked = True
  7.       Me.[Full Name].Locked = True
  8.       Me.[Badge ID].Locked = True
  9.       Me.[SSN Last 5].Locked = True
  10.       Me.[Term Date].Locked = True
  11.       Me.[Requester Name].Locked = True
  12.       Me.[Supplier].Locked = True
  13. '
  14.     End If
  15. '
  16. End Sub
  17.  
Nov 14 '13 #2
zmbd
5,501 Recognized Expert Moderator Expert
Chip.
Glad you found the on_current event of the form.

Might I make a few suggestions to clean up your code:

Refering to JUST The first block of code in the original posting:
1) Take lines 5 thru 24 in 1st code block (btnAddNewRec_Click) of post 1
1a) move these to a new sub within the form's code and name it "toggle_field_state"
1b) Alter your code to this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAddNewRec_Click()
  2.  
  3.     DoCmd.GoToRecord , , acNewRec
  4.     Call toggle_field_state
  5.  
  6. End Sub
  7.  
2) in the on current event of the form reuse your state toggling code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.  
  3.     Call toggle_field_state
  4.  
  5. End Sub
3) There's no error trapping here. May not really be needed; however, I prefer to have error trapping in all my code just to keep things from the user.

4) Lines 15 and 16 can be combined into the "ELSEIF" structure, or, indeed, better yet line 16 can be deleted as the current record is either new or not; thus, no real need to test it twice, in either case you will need to remove the "ENDIF" associated with line 16 located at line 25.

If the second block of code was a second question then you need to split that question into a new thread.

And I haven't looked at your second post closely; however, you really should get used to indenting things properly. It makes the code easier to follow... I've done this for you in the second post. (^-^).

-z
Nov 14 '13 #3
ADezii
8,834 Recognized Expert Expert
The General Idea is as follows:
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Dim ctl As Control
  3.  
  4. For Each ctl In Me.Controls
  5.   ctl.Locked = (Not Me.NewRecord)
  6. Next
  7.  
  8. 'Can Restablish Error Checking here...
Nov 14 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
Nice loop, one I use myself provided that I want every control locked, including the command buttons :)
Although, I usually toss in a check for the control type, the control name, or for some value in the tag property so that I don't lock the command button.
Nov 14 '13 #5
NeoPa
32,557 Recognized Expert Moderator MVP
I would go with the following to ensure only new records can be added :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.     Me.AllowEdits = Me.NewRecord
  3. End Sub
Nov 14 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3471
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
1
1721
by: amanda | last post by:
What will be the syntax if I want to compare whether the phone number I am entering when adding a record already exists in database. The phone field in that table is set to primary key. We are...
5
2296
by: sara | last post by:
Hi - I have 2 new questions in a new database I'm trying to develop. The first has 6 fields on a table and allows the user to enter them on a form. The form is bound to the table. All 6...
1
1431
by: pdesh3 | last post by:
Hi, I am not getting any idea how to implement this: I have a form for which I need to create a top banner kind of the thing on top section of the Form. It should contain the feilds as...
5
1719
by: Marks | last post by:
Right I'm don't feel so nervous tackling VB as much now but basic stuff I'm great with so the next part of my script is: 1. Having a Front Menu. When you enter the number 1 in a textfield it...
2
4237
rsmccli
by: rsmccli | last post by:
Access 2002 Hi, I have a number of comboboxes/textboxes in the header of a form that the user selects values from. These comboboxes/textboxes correspond to controls in a datasheet view subform....
1
3795
by: MerlinS | last post by:
Using Access 2003. I have a form and a subform. On the main form, one of the fields is a lost box, which lists line numbers ie 1, 2, 3 etc. Then the list box is clicked, the subform corresponding...
1
1950
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the...
3
2985
by: pzh20 | last post by:
I have an unbound form/subform where I populate a combo box on the main form, and using the onchange event, display fields from a table in a datasheet subform. I want to add a new record via the...
2
2119
Soniad
by: Soniad | last post by:
Hi, In my website, I show commodity details on daily basis.These details are stored in one table for each day. I select record by matching current date and current date records are...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.