Link to home
Start Free TrialLog in
Avatar of colevalleygirl
colevalleygirlFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access docmd.close acform or docommand.runcommand accmdsaverecord in ribbon callback

I'm trying to close a form within a ribbon callback, and -- if necessary -- save any changes to the current record first; then open a new form in the same subform window.  The form being closed can be one of 12 forms, each with customised validation coded in VBA.

My ribbon callback code looks like:
         Dim strForm As String
     
         strForm = ParseWord(pControl.Tag, 3, ";") 'Get the third element of the ribbon control tag which identifies the form to open
         strForm = Right$(strForm, Len(strForm) - 17) 'Strip off the leading garbage
         DoCmd.RunCommand acCmdSaveRecord 'Save the current record
         DoCmd.Close acForm, Forms!frmmain!NavigationSubform.Name 'Close the current form
         Forms!frmmain!NavigationSubform.SourceObject = strForm 'Open the new form

Open in new window


If I only use docmd.close, the changes to the current record are discarded without informing the user if a mandatory field hasn't been completed.

If I use docmd.runcommand accmdsaverecord, the record is saved but none of the the error handling in relevant forms is triggered, so the error messages to the user are unfriendly to say the least. Is there any way to accomplish this using the appropriate form's error handling?

[For info, I'm moving to using a custom ribbon to replace a Navigation control that used to handle all this seamlessly].
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of colevalleygirl

ASKER

I want a consistent user experience -- within a form, if they navigate from field to field, or record to record, their changes are saved automatically and they get specific error messages if there's a problem with their data, so that's my target for what happens when they navigate from form to form.

I do at least have all the mandatory fields tagged as "Mandatory" already (it controls their formatting) so could relatively quickly code a generic validation routine like the one you outline, but would either have to have inconsistent error messages (less precise on leaving a form than when moving within it) or lose the precision within the form...

OK, I need to think about this some more.
Or you could use a function that does something like:

Public Function CheckValid(frm as form) as boolean

    if frm.Name = "frm_Main" Then
        if LEN(frm.txtField1 & "") = 0 then
            msgbox "Specific message here!"
            frm.txtField1.setfocus
        elseif len(frm.cboField5 & "") = 0 Then
            msgbox "some other specific message here!"
            frm.cboField5.setfocus
        else
             CheckValid = true
        endif
    elseif frm.Name = "frm_SomeOtherForm" Then
        if len(frm.txtField3 & "") = 0 then
             msgbox "control specific message!"
             frm.txtField3.setfocus
        else
             CheckValid = true
        end if
    end if

End Function
I have decided to test for a dirty form when switching to another form and prompt the user to save the record -- which then forces the data through the validation already written. Longer term, I will change the validation approach to adopt the solution proposed.