Results 1 to 11 of 11
  1. #1
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9

    Record data on form to table only when hitting the "record button"

    Hi,



    I am new to Access and don't have any SQL experience. I have a form to input some information and save it to a table using a "Record" button. Is there an easy way to only add the information to the table by clicking the record button and not by, for example closing the form?

    Thanks

  2. #2
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    By default, Access will save any new Records (or any changes to existing Records) as soon as you "leave" the Record. This means that if you browse to a new Record on the Form, it will automatically save changes for you.

    If your Form only allows you to view a single Record at a time, or if you have special requirements, then you can create a button with the following command in it's On Click Event to force Access to save the Record immediately:

    Code:
    DoCmd.RunCommand acCmdSaveRecord

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I believe Rawb misunderstood your intent
    Quote Originally Posted by Jimmy Shoe View Post
    ...Is there an easy way to only add the information to the table by clicking the record button and not by...closing the form
    To circumvent the default behavior of saving a Record when the Record is moved off of or when closing the Form, you'd have to use an Unbound Forms, and this is not really something for the inexperienced developer to attempt. Several developers I know, experienced in Visual Basic database development and Access development, estimate that development, using Unbound Forms, by highly experienced developers, takes two to three times as long, using Unbound Forms, as it does when using Access and Bound Forms. That's because with Bound Forms, Access takes care of the 'heavy lifting,' but with Unbound Forms, the developer has to write code to take care of just about everything.

    If you insist on using Unbound Forms, you'd be far better off using a straight VB or C++ front end with a SQL Server or Oracle back end.

    • You can create an EXE file which gives total protection to your code/design
    • You can distribute the db to PCs without a copy of Access being on board
    • Your data security is far, far better than anything you can do in Access


    The question here is "Why?" Why do you not want Access to automatically save the Record by default? There's nothing that can be done using Unbound Forms that cannot be accomplished with Bound Forms, and usually accomplished with less work!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Whoops, you're right Linq, I completely missed what was being asked!

    However, there is another option: Create a temporary Table with the same Fields as your actual Table and bind the Form to it. Then make a Button that copies the record(s) from the temporary Table to the actual Table when clicked. This will only work if you don't want to be able to use the same Form to browse/update Records though - you would only be able to create new Records with the Form.

    You might also try setting up an On Close Event (and the On Current Event) in the Form to check if Me.Dirty = True and, if so, reset the Record. You should be able to do this by looping through the Fields and setting their .Value Properties to match their .OldValue Properties.

    Code:
    Dim objControl As Object
    
    If Me.Dirty = True Then
      On Error Resume Next ' Not all controls are for data entry, so they won't all have .Value or .OldValue!
    
      For Each objControl in Me.Controls
        objControl.Value = objControl.OldValue
      Next
    
      On Error GoTo 0 ' Turn error checking back on
    End If
    
    Set objControl = Nothing

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    A valid approach, given a valid reason! But such reasons are seldom presented, here. And given the Op's statement

    Quote Originally Posted by Jimmy Shoe View Post

    ...I am new to Access and don't have any SQL experience...
    this kind of thing is almost sure to be outside of Jimmy's skills set, requiring a lot of coding.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    Thanks to both of you. The form I have set up is used only to add new records and not edit/view but I am not sure how to set up a temporary table. I tried adding the code for the Me.Dirty = True on the two events but it doesn't seem to work. is there anything in the code I would need to change to suit my form?

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    There is no easy way to do this, no line or two of code that's going to work. We're still waiting to hear why you want to circumvent the normal behavior in an Access Bound Form, and make life more difficult for yourself in the process.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    To prevent incomplete records being added because a user decides to stop half way through and close the form.

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    So ask the user if they want to save the New Record or save the changes to an existing Record:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not (Me.NewRecord) Then
      If MsgBox("Would You Like To Save The Changes To This Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save Changes to Record ???") = vbNo Then
       Me.Undo
      End If
    Else
      If MsgBox("Would You Like To Save This New Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save This Record ???") = vbNo Then
       Me.Undo
      End If
    End If
    End Sub


    If you have specific Fields that have to be populated, to constitute a 'complete Record,' you can also use the Form_BeforeUpdate event to check that these are, in fact, populated, and if they're not, don't allow the Record to be saved.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    Jimmy Shoe is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    9
    Thanks Linq, that is a perfect solution!

  11. #11
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. how to change focus of "next record" button
    By Andyatwork in forum Forms
    Replies: 2
    Last Post: 09-05-2012, 04:34 AM
  2. Replies: 2
    Last Post: 02-21-2012, 01:35 PM
  3. Customize button "goto last record"
    By bertenbert in forum Forms
    Replies: 1
    Last Post: 12-30-2010, 08:38 AM
  4. Lock Record when "Add" button is pressed
    By jo15765 in forum Access
    Replies: 34
    Last Post: 11-28-2010, 08:50 PM
  5. Replies: 3
    Last Post: 02-23-2010, 06:32 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