Results 1 to 14 of 14
  1. #1
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109

    Save Data so Escape key doesn't work


    I have 3 fields (locked and not enabled) that are autofilled after insert. The problem is I can't figure out how to lock those fields down so a user can't use the escape key to blank the autofilled fields out right after insert. I tried adding the last line to the afterinsert but I get an error. The error and code are below. The line that is causing the issue is in red. Any help would be greatly appreciated. Thank you.


    Code:
    Private Sub Form_AfterInsert()
    
    Me.[OldBox#] = DMax("Val([OldBox#])", "[DataEntry]") + 1
    Me.[EnteredBy] = Environ("UserName")
    Me.[EntryDate] = date
    RunCommand acCmdSaveRecord
    End Sub
    Run-time error '2115'
    The macro or function set to the BeforeUpdate or validation rule property for this field is preventing Microsoft Access from saving the data in the field
    Extra Information:
    BeforeUpdate is not used in my form.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Review http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    I have never used the form Insert events so just speculation but maybe use BeforeInsert to set those fields then AfterInsert to Requery or maybe just Refresh.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    When I changed the code to BeforeInsert it didn't allow the current events to start. I tried all types of saving in 'afterinsert'. I have to click on my save button (in the same form) to make those fields stick. The save button has the 'RunCommand acCmdSaveRecord' code but for some reason after insert it is in some place where no commands work. Since I know that clicking on my save button works, how can I make that button execute in VBA?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If code in same form as the button, try:

    Call yourButtonName_Click

    If from another form:

    Call Forms("form name").yourButtonName_Click

    I use both syntax but, as stated not using Insert events, so don't know if will work for you.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    It didn't work. I got the same error. It is weird that I get stuck afterinsert. Maybe access wants to give the user one last chance to exit so I can't force a save. The strange part is all I have to do it click on a control after the insert and click save button or just hit the save button to make it stay yet I can't automate that.

    Edit - I have a little bit more information. When I finish filling out the form and click into the subform or go to the next record, the 3 fields populate. In either case, if I immediately hit escape, they get blanked out. I am completely lost on this issue.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    So the code populating the fields works? But not forcing a record save? Will any other code run in the AfterInsert? Test a MsgBox or Debug.Print. What about form Refresh or Requery or SetFocus to subform? Will that run and also commit the edits?

    Maybe form KeyPress event code to capture the ESC key press? Unfortunately, the event would fire with every keystroke. I have used a textbox KeyPress event to capture Enter key press.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    "Me.DOC.Setfocus" (DOC = subform) does not work. I get an error saying access cannot set focus. Me.Requery and/or Me.Refresh gives the error in my first post. The only thing that worked is a message box but that didn't fix the problem. How would I capture the key press. Like if they hit escape, it would give the user a message box that would either have them delete the record or cancel the key press.

  8. #8
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I found a website with a similar problem to mine. One of the comments has an alternate solution using code but I don't understand if the code will work for me.

    http://database.itags.org/ms-access-database/96766/

  9. #9
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I think there is a workaround for this. I could do an after update event. If all three values are null, it would fill them in. How should I write the code for it? I tried doing the if statement below but it doesn't add the information to the record for some reason and I don't get an error.

    Code:
    Private Sub Form_AfterUpdate()
    If IsNull(Me.OldBox#) And IsNull(Me.EnteredBy) And IsNull(Me.EntryDate) Then
        Me.[OldBox#] = DMax("Val([OldBox#])", "[DataEntry]") + 1
        Me.[EnteredBy] = Environ("UserName")
        Me.[EntryDate] = date
    End If
    End Sub
    Edit - I got the code to work with the code below. I still have some problems with the escape key. I'll have to figure out where to put it. I put it in AfterUpdate and Current. They both work but when I hit escape it still removes the information.

    Code:
    If IsNull([OldBox#] & [EnteredBy] & [EntryDate]) Then
        Me.[OldBox#] = DMax("Val([OldBox#])", "[DataEntry]") + 1
        Me.[EnteredBy] = Environ("UserName")
        Me.[EntryDate] = date
    End If

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Did you try BeforeUpdate?

    What exactly is triggering the Update event? My understanding is it would need to be form close, move to new record, or SaveRecord.

    Google: Access disable ESC key

    Everything I read involves trapping keystroke (KeyDown, KeyUp, KeyPress).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I can't use BeforeUpdate because it conflicts with my current event for some reason. The KeyPress seems to be the only option. I got it working without an if statement but I don't want it to execute unless the fields are null. I listed the key press code below and what I want it to check below that. I can't figure out a way to combine the two so that when the escape key is pressed, it checks if all 3 fields are no and if so, it fills in the information (or cancels key press. preferred method). If they are not null, I would like it to continue with the keypress.


    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyEscape Then
    Code:
    If IsNull([OldBox#] & [EnteredBy] & [EntryDate])
        Me.[OldBox#] = DMax("Val([OldBox#])", "[DataEntry]") + 1
        Me.[EnteredBy] = Environ("UserName")
        Me.[EntryDate] = date
    End If

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Check out this article http://www.techonthenet.com/access/f...isable_esc.php

    If that works then maybe just need to populate the 3 fields in Current event or when record is commited.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    I got the nested if statement to work in the key press. It looks like everything is fixed now. Thank you for all your help.

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Would you like to share your final code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Buttons doesn't work in Subforms
    By fedesc in forum Access
    Replies: 6
    Last Post: 09-25-2011, 12:58 AM
  2. App doesn't work with runtime
    By bubba55 in forum Access
    Replies: 0
    Last Post: 09-21-2011, 08:33 AM
  3. Can Grow doesn't work
    By gg80 in forum Reports
    Replies: 6
    Last Post: 05-13-2011, 07:14 PM
  4. Query doesn't work the day after
    By sithis876 in forum Queries
    Replies: 1
    Last Post: 07-13-2010, 07:11 AM
  5. 2007 doesn't work in Windows 7
    By InvGrp in forum Access
    Replies: 2
    Last Post: 05-24-2010, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums