473,545 Members | 2,715 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 5223
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_C lick) of post 1
1a) move these to a new sub within the form's code and name it "toggle_field_s tate"
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,564 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
3475
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 the cursor was on in the datasheet at the time I executed the macro. So, the questions are: 1) In the macro, how to I get my hands on the record...
1
1728
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 using binding source in this application.
5
2305
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 fields are default value = 0 on the table and on the form. Fields are filled in at different times and maybe by different people, so I wanted them all...
1
1444
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 DATE, PHYSICIAN NAME, LOCATION. DATE field should display intially system date, when highlighted it should be editable and it should allow to...
5
1722
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 selects formA, number 2 selects formB etc how would I do this? 2. E-mail Current Form Record. I found a similar thread to this but I used its code and...
2
4242
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. When a new record is created in the subform I want these values to automatically write to the new record in the subform. Right now I have this...
1
3805
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 to the line number info is shown. When a new record is added to the subform the new line number is added and is shown in the main form list box. The...
1
1954
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 "Auto-number" in Microsoft Access) http://raymondlamsk.blogspot.com/2008/03/faq-ms-sql-server-how-to-generate_24.html
3
2987
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 subform, allowing the user to type certain fields but complete other fields via code in the beforeinsert event as follows Private Sub...
2
2142
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 displayed.but there me cases when some day details are not updated. Hence, I need query where if current date record is not there then it should fetch records...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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 we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.