DoCmd.Close acForm, "Name", acSavePrompt not working - ms-access

I have some VBA code, like this:
Private Sub Command150_Click()
DoCmd.OpenForm "frm_requirements_reference", , , , acFormAdd
DoCmd.GoToRecord , , acNewRec
Forms!frm_requirements_reference!fk_requirement.Value = Me!txt_form_requirement_id.Value
Forms!frm_requirements_reference!Requirement_Name.Value = Me!Requirement_Name.Value
Forms!frm_requirements_reference!Combo7.SetFocus
DoCmd.Close acForm, "Formular_Requirements", acSavePrompt
End Sub
In general, I am the most interested about that last line of code. It just closes the form "Formular_Requirements", however no prompt on save YES / NO comes. Don't you know, where that problem could be?
Thank you for help,
Vaclav

I guess you are using MS Access.
If I understand your problem well, saving data is automatically handled by the currency manager.
For example, if you edit a row, and
Click "Next Record"
or
The corresponding table fields lose focus
The record edited is automatically saved.

Related

New Record Command working in admin mode but not normal mode when If statement present

I am at my wit's end. This seems like it should be the most straightforward thing in the world. I have a button on a form that should start a new record when pressed. Here is the code in the button
Private Sub NewLocProLink_Click()
mbResult = MsgBox("Are you sure you would like to add a new provider to
location relationship?", vbYesNo, "New location to provider relationship")
If mbResult = vbYes Then
DoCmd.GoToRecord , "", acNewRec
ElseIf mbResult = vbNo Then
MsgBox ("No new record")
End If
End Sub
This code does not work. When I click "Yes" it just seems like nothing is happening. However, when I click "No" I do receive my test MsgBox that says "No new record". So I know that the code enters my If statement.
Now here is the weird part. When I access the DB in admin mode (by holding down shift when I enter) and test the button out, it works. But then when I close the DB and go back in without holding down shift and push the button, it does not work.
If I edit the button's code to only have the command to go to a new record without the confirmation message box like this:
Private Sub NewLocProLink_Click(
DoCmd.GoToRecord , "", acNewRec
End Sub
Then it works fine in both modes. I would really like to have a confirmation box before, though. Just FYI, I can't think of why this would matter but the button is on a subform. But the subform is where I want to create a new record, so that is where the button belongs. Any ideas would be appreciated.
One other piece of information that I'm not sure why it would have an impact, but maybe someone knows that it might, is that all my tables are using an ODBC connection to link to tables in SQL Server 2014.
The problem is focus. If you pop up a message box, the message box has focus. Since you're not specifying which form to navigate, it fails.
A simple solution is to navigate using the forms recordset instead:
Private Sub NewLocProLink_Click()
mbResult = MsgBox("Are you sure you would like to add a new provider to
location relationship?", vbYesNo, "New location to provider relationship")
If mbResult = vbYes Then
Me.Recordset.AddNew
ElseIf mbResult = vbNo Then
MsgBox ("No new record")
End If
End Sub
Since the record you are trying to access is on a sub-form, declare the the object as part of the DoCmd.GoToRecord , "[Object Name]", acNewRec.
This may solve your problem.
EDIT:
I should clarify what I mean here:
(Assuming your forms are "Form1" and sub-form is "Form2")
Forms!Form1!Form2.SetFocus
DoCmd.GoToRecord , "", acNewRec

Making "DoCmd.GoToRecord" function work on a subform

I have been using the function DoCmd.GoToRecord , , acNewRec successfully for creating and moving to a new record within a subform (with a table as the source). However, when I try to do the same from the parent form, this does not work. I have tried different approaches, including:
Me.sbfrm_subform.Controls("ctrName").SetFocus
DoCmd.GoToRecord , , acNewRec
which only sets the focus on the control (ctrName), but fails to add and go to a new record, or
DoCmd.GoToRecord acDataForm, Me.sbfrm_subform.Form.Name, acLast
Which returns the runtime error 2489, "The object 'sbfrm_subform is nt open."
Try splitting the operations:
Me.[sbfrm_subform].SetFocus
DoCmd.GoToRecord, , acNewRec
Alternatively, you can try creating a public Sub in the subform, and since it becomes a method of the form you can use that.
Using this on recent versions of Access, you can even try playing directly with the form's recordset instead, like Me.Recordset.Movenext.
Try placing the code into the subform and then call it from the Parent:
Sub Form Code:
Sub GoToNewRecord()
DoCmd.GoToRecord , , acNewRec
End Sub
Parent Form Code:
Me.sbfrm_subform.GoToNewRecord
As iDevlop noted, you can use the Recordset object of the subform to move to a new record. However, you don't need to create a public sub in the subform. You do it all from the main form:
Me.[subform control name].SetFocus
Form_[subform form name].Recordset.AddNew
In order to use the Form_[form name] syntax, the form has to have a VBA code module. If the form doesn't have one, and for some reason you're opposed to creating an empty one, then you can use the Forms!MyForm.SubformControl.Form syntax instead. But Form_[Form Name] is simpler.
I did the following event procedure in the main form "On Current" :
Private Sub Form_Current()
Me.SubformName.SetFocus
Me.SubformName.Requery
RunCommand acCmdRecordsGoToLast
DoCmd.GoToRecord , , acNewRec
Scan.SetFocus
End Sub
The DoCmd is for the main form to start a new record. Everything before that is to set the subform to the last record AND requery it so that the data is fresh.
This is how I resolved my issue...
main form name is FRM_Trader_WorkSheet
Subform name is Frm_Trader_Worksheet_Sub
On the open event of my main form I coded as follow;
Private Sub Form_Open(Cancel As Integer)
Me.Frm_Trader_Worksheet_Sub.SetFocus
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acNext
End Sub
Since I am not doing any data entry on my main form, now my main form opens up with the focus on a new record in my sub form. I now can go back to previous records if need be but am ready to enter new data when the main form is loaded.
That being said, you can achieve the same results by simply setting the property of the subform under the Data tab "Data Entry = YES. The only difference is that you will no longer have access to the previous records...
The answer is to use the one of the following lines:
DoCmd.RunCommand acRecordsGotoNew
DoCmd.RunCommand acRecordsGotoNext
DoCmd.RunCommand acRecordsGotoPrevious
DoCmd.RunCommand acRecordsGotoFirst
DoCmd.RunCommand acRecordsGotoLast
Depending on what you want to do. This is functionally the same as clicking on one of the navigation buttons if they are left visible.
If you're calling one of these from the parent form, you may first have to set the focus onto the subform with the following line
Me.subform_name.SetFocus
Unfortunately, there doesn't appear to be a command that navigates to an absolute position in the recordset, which would complete the set of possibilites.
What worked for me is:
In the form that shows the record, I set focus to the field I wish and set the recordset. After setting the recordset I just use the AddNew function of the recordset.
Public Sub GoToNewRecord()
Me.<myTextbox>.SetFocus
Dim rcClone as Recordset
Set rcClone = Me.Recordset
rcClone.AddNew
End Sub
Private Sub anycotrl()
Me.yoursubformname_subform.SetFocus
DoCmd.GoToControl ("[anycontrolyouneed]")
DoCmd.GoToRecord , , acLast
End Sub
I found a similar problem (I think). Using a linked form the intention was to click on a new row on the linked form in Datasheet view and have this clear the records the linked form is synced with in a subform within the parent form. Using DoCmd.GoToRecord , , acNewRec worked but only once. Found solution was to setfocus on the parent form first and then set focus on the subform. I recall finding this before somewhere - always setfocus on the parent first then the sub. ...... Carpe Dium ....

Openargs when form is already open

Is it possible to change the Openargs values once the form is already open in Access? It works only the first time the form is open. It fails of the form is open already.
EDIT:
I have this code in the onActivate event of the form
If Not IsNull(Me.OpenArgs) Then
Me.Recordset.FindFirst ("Id =" & Me.OpenArgs)
If Me.Recordset.NoMatch Then
MsgBox "ISOS not found"
End If
End If
Me.OpenArgs contains the result of a search in another form that uses this command DoCmd.OpenForm "<Form_Name>", acNormal, , , , acWindowNormal, Forms!Lookup_Form!Id to open up the main form.
I've had to solve this same problem recently for a project I'm working on, so I thought I'd offer up the solution I came up with.
The easiest thing I found to do in this situation is to call DoCmd.Close right before the DoCmd.OpenForm. This will close the current instance of the form and open a new one, which will force the Form_Load event to fire again with the new arguments.
DoCmd.Close acForm, "Form2", acSaveNo
DoCmd.OpenForm "Form2", acNormal, , , , , {arg1}|{arg2}
The two assumptions here are:
1. That the state of "Form2" is always being initialized by code in the Form_Load event.
2. There is no state data in "Form2" that I'm trying to retain when passing new arguments.
How about running your code from the search form, like so:
Dim frm As Form
If Not CurrentProject.AllForms("Form1").IsLoaded Then
DoCmd.OpenForm "Form1"
End If
Set frm = Forms!Form1
frm.Recordset.FindFirst ("Id =" & Me.ID)
If frm.Recordset.NoMatch Then
MsgBox "ISOS not found"
End If

Passing Form OpenArgs errors as NULL?

I am trying to open a form as a dialog and also pass a string in the OpenArgs property. No matter what I try, I get an Invalid use of null error.
The form is not open. It's not open in design mode either. Can anyone shed some light on this for me?
Here's the calling line:
DoCmd.OpenForm strTmpForm, acNormal, , , , acDialog, "Hi"
Here's the Form Open Sub on the target form
Private Sub Form_Open(Cancel As Integer)
MsgBox Me.OpenArgs
End Sub
Are you sure the form isn't open and hidden? It seems likely, since the usual dialog pattern usually involves hiding the dialog form to allow control to resume in the calling method.
To be certain:
If SysCmd(acSysCmdGetObjectState, acForm, strTmpForm) <> 0 Then
DoCmd.Close acForm, strTmpForm
End If
If that's not the problem then I'd ask if you can post the entire calling method rather than the calling line.

Microsoft Access 2000 Switch Views at Runtime

I'm creating a form programatically that has a ComboBox that is populated with a list of options. On selecting an option, I'd like the form to be populated with various controls.
At the moment I'm achieving it by:
FormName = Screen.ActiveForm.Name
DoCmd.Close acForm, FormName, acSaveYes
DoCmd.OpenForm FormName, acDesign
' Do Work to create controls
DoCmd.Close acForm, FormName, acSaveYes
DoCmd.OpenForm FormName, acNormal
The problem is, I'm going to have many temporary forms saved in my database. So as I see it there are two options for me,
Switch to design view without having to save the form
Be able to delete the form when I'm done with it
I've tried putting
DoCmd.DeleteObject acForm, FormName
in the OnClose, and OnUnload triggers, but it results in a "You can't delete the object 'Form1' while it is open" error
Any suggestions?
Do your options result in a limited number of controls on the form? In other words, would it be possible to have all the controls you need created ahead of time, just not Visible? Then all you would need to do is show and position what is needed without having to re-create everything from scratch.