Results 1 to 8 of 8
  1. #1
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76

    Which is the best code to use to save a record and exit using a button?

    Hi,



    What is the best code to use in order to save the record and quit the form? I have a main menu so i will save,quit this form and go to main form

    I have this code:

    Private Sub testingsaveok_Click()
    If Me.Dirty Then
    Me.Dirty = False

    DoCmd.OpenForm "BorrowerSection", acNormal
    DoCmd.Close acForm, "AddNewBorrower", acSaveNo

    End If
    End Sub

    Is this the best way? My code actually gets a runtime error if i did not fill in one of the required fields in my form but other than that it saved the form if i entered all required information.

  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,891
    The record will commit to table when the form closes. No other code needed. You might want to handle the possibility of the neglected field with:

    If IsNull(Me.requiredfieldname) Then
    MsgBox "Must enter value in fieldname."
    Me.requiredfieldname.SetFocus
    Else
    'close
    ...
    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
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by June7 View Post
    The record will commit to table when the form closes. No other code needed. You might want to handle the possibility of the neglected field with:

    If IsNull(Me.requiredfieldname) Then
    MsgBox "Must enter value in fieldname."
    Me.requiredfieldname.SetFocus
    Else
    'close
    ...
    So my code is alright? i arldy set the required properties to yes on required fields so that validation is not needed i think. Actually, can the above code validation fields in my subforms?

  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,891
    I meant the Me.Dirty code was not needed.

    If you need to validate subform data, then refer to the subform like:

    Me.subformcontainername!fieldname
    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
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by June7 View Post
    I meant the Me.Dirty code was not needed.

    If you need to validate subform data, then refer to the subform like:

    Me.subformcontainername!fieldname

    Hmm but i read somewhere that the me.dirty code is to force a save.Alright if i try to validate the data of the subform, will the code look something like this?I think ill only validate the subforms since i arld have validation in main form property fields:


    Private Sub testingsaveok_Click()

    If IsNull(Me.subformcontainername!fieldname) Then
    MsgBox "Must enter value "
    Me.fieldname.SetFocus

    ElseIf IsNull(Me.subsubformcontainername!fieldname) Then
    MsgBox "Must enter value "
    Me.fieldname.SetFocus

    Else

    DoCmd.OpenForm "BorrowerSection", acNormal
    DoCmd.Close acForm, "AddNewBorrower", acSaveNo

    End If
    End Sub

    Is this how you do it for subsubform and subform validation? Im also not quite sure what to put inside this " Me.subformcontainername!fieldname ", can you give me an example?

  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,891
    That is an example with dummy aliases. You need to use your subform container name and fieldname.

    I always give subform container control a name different from the object it holds, like ctrInvoiceDetail.

    If you want to set focus to control in subform, need syntax that refers to the subform container. However, I have never tried to set focus to a control on subform. I think the syntax for that will be Me.containername.Form.controlname.
    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
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by June7 View Post
    That is an example with dummy aliases. You need to use your subform container name and fieldname.

    I always give subform container control a name different from the object it holds, like ctrInvoiceDetail.

    If you want to set focus to control in subform, need syntax that refers to the subform container. However, I have never tried to set focus to a control on subform. I think the syntax for that will be Me.containername.Form.controlname.
    Alright so besides the me.fieldname.setfocus, everything is ok? So for example if your form is bound and you used autoform to create the form , the subformcontainername would be the same as the fieldname right?
    But why would you give a different name ?Wont it be easier if the containername is the same as fieldname?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    Wizard would name the container the same as the subform it holds, not fieldname.

    If I use a control in code or in expressions, I always make sure has name different from items (form, field). Sometimes Access will be confused if it has to decide if you are referencing a control or the item. For instance, if a textbox has same name as a field and you use this name in a ControlSource expression, could get 'circular reference' error.
    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. do NOT save record on close (x button)
    By benjammin in forum Forms
    Replies: 12
    Last Post: 03-15-2013, 02:54 AM
  2. Replies: 3
    Last Post: 11-18-2011, 03:53 PM
  3. Emailing Last Record & exit handling
    By malamute20 in forum Programming
    Replies: 25
    Last Post: 08-24-2011, 05:14 AM
  4. Replies: 9
    Last Post: 08-06-2011, 02:10 PM
  5. Save to PDF Button?
    By SpeedyApocalypse in forum Forms
    Replies: 2
    Last Post: 04-10-2010, 06:06 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