Results 1 to 10 of 10
  1. #1
    rdenn_58 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    6

    Form Fields Data Retention

    I have a data entry form with multiple fields. I have made a macro in the form Before Update (if/else if) to give a message if data for a field is left blank. That works great.



    Now the problem. If a field is left blank and a message box appears, the user presses the OK button for the message box. All the fields in the form then go blank as if the form refreshes. I would like the fields to retain the data that was already entered for that record and the cursor to go to the blank field or, in the case of multiple blank fields, go to the first blank field.

    Thanks in advance for the help!!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    In VBA code it looks like this:

    http://www.baldyweb.com/BeforeUpdate.htm

    i don't use macros, but I assume you're missing the Cancel = True bit, or its macro equivalent.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rdenn_58 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    6
    Thanks for the quick reply, P!

    I went ahead and deleted the macro I had in the form Before Update and added your code as an event procedure. I tested it and it brought the messages up just fine for a blank field. But after selecting OK in the message box, the form still seemed to refresh with all the fields blank instead of keeping the original data. By the way, there was no new record created which is great too! I have listed the code I used in the form Before Update as I altered it from yours. I am sure this could have been cleaned up and worked more efficiently.......or am I going in the right direction?

    '------------------------------------------------------------
    ' Form_BeforeUpdate
    '
    '------------------------------------------------------------
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error GoTo Form_BeforeUpdate_Err


    If Len(Me.CDNumber & vbNullString) = 0 Then
    MsgBox "CD NUMBER CANNOT BE BLANK"
    Cancel = True
    Me.CDNumber.SetFocus
    End If


    If Len(Me.Location & vbNullString) = 0 Then
    MsgBox "LOCATION CANNOT BE BLANK"
    Cancel = True
    Me.Location.SetFocus
    End If


    If Len(Me.Manufacturer & vbNullString) = 0 Then
    MsgBox "MANUFACTURER CANNOT BE BLANK"
    Cancel = True
    Me.Manufacturer.SetFocus
    End If


    If Len(Me.Description & vbNullString) = 0 Then
    MsgBox "DESCRIPTION CANNOT BE BLANK"
    Cancel = True
    Me.Description.SetFocus
    End If


    If Len(Me.Model & vbNullString) = 0 Then
    MsgBox "MODEL CANNOT BE BLANK"
    Cancel = True
    Me.Model.SetFocus
    End If


    If Len(Me.SerialNumber & vbNullString) = 0 Then
    MsgBox "SERIAL NUMBER CANNOT BE BLANK"
    Cancel = True
    Me.SerialNumber.SetFocus
    End If


    If Len(Me.Cost & vbNullString) = 0 Then
    MsgBox "COST CANNOT BE BLANK"
    Cancel = True
    Me.Cost.SetFocus
    End If


    If Len(Me.AcquisitionDate & vbNullString) = 0 Then
    MsgBox "ACQUISITION DATE CANNOT BE BLANK"
    Cancel = True
    Me.AcquisitionDate.SetFocus
    End If



    I am pretty new to Access and especially writing code. I do appreciate the help!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I assume there's more, or that would generate a compile error. That's on the right track, but I'd tweak it depending on how you wanted it to work if there are more than one empty. At the simplest, I'd add

    Exit Sub

    to the end if each test, like:

    Code:
    If Len(Me.CDNumber & vbNullString) = 0 Then
       MsgBox "CD NUMBER CANNOT BE BLANK"
       Cancel = True
       Me.CDNumber.SetFocus
       Exit Sub
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    rdenn_58 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    6
    Thanks, Paul! This is working as advertised except the other fields on the form are not holding the data after selecting OK in the message box. All the fields are still blank as if the form refreshed. What needs to be added to keep the other fields data on the form?

    Here's an example of what's happening......I put in data in all fields, but left the Serial Number field blank. When I select a command SAVE button, the message box displays "SERIAL NUMBER CANNOT BE BLANK". Perfect so far! I click the message box OK button. The form fields all return to being blank and all the data has to be entered again. Hopefully you can point me in the direction to keep the previous data on the form prior to the message box.

    Thanks again for your help!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Can you post the db here? The Cancel = True should stop the update and leave the values on the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    rdenn_58 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    6
    Property 2013 Copy_Backup1.zip

    Here it is, Paul. The form is AddAssetF. The database opens to the Main Menu and selecting Add CD gets you to that form.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I think the code in your save button is overriding things. Try with just this in there:

    DoCmd.GoToRecord , , acNewRec
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    rdenn_58 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    6
    Thanks, Paul! That took care of it. I commented out the unnecessary error messaging and closing/reopening the form commands. I think I looked at that button quite a few times and never saw the error messaging duplication or form reloading until now. It just needed another set of eyes on it......and expert eyes to boot.

    In making my first database, I have learned a lot. While creating this, I have Google'd many times when I got stuck. A lot of hits I got back, you had replied to so many others with easy to follow solutions. I used quite a few of your solutions you provided for others to resolve issues or better my own database. For those new to Access like myself, we appreciate your time and assistance. Thanks again for your help!!!!! I may put you on forum speed dial. Haa.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help, and thanks!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 09:04 AM
  2. Replies: 5
    Last Post: 02-27-2012, 02:05 PM
  3. Replies: 2
    Last Post: 03-16-2009, 12:19 PM
  4. Shared DB data retention issues
    By tnmz6i in forum Access
    Replies: 0
    Last Post: 02-03-2009, 11:08 AM
  5. can't enter data in some form fields
    By ashiers in forum Forms
    Replies: 1
    Last Post: 09-18-2008, 12:37 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