Openargs when form is already open - ms-access

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

Related

DoCmd.Close acForm, "Name", acSavePrompt not working

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.

MS Access New record in form

I am making a data entry MS Access based database. It has a main screen, where I enter the person ID, and once I click Create, it directs to a navigation form which displays 4 other forms (basically 4 pages of data entries for 1 person).
This form also displays the person_ID and date in the header. When I enter the person ID and click Create, I should be able to enter the details for that respective record. But when the form opens it redirects to the first record in the DB and not that particular record. Kindly suggest me the proper way to do it.
To open a form on a particular record you can use DoCmd.OpenForm method with a WhereCondition.
On the first form have a text box for the person_ID.
This will be referred to as me!person_ID in your code.
DoCmd.OpenForm "Form2", acNormal, , "person_ID = " & me!person_ID
That's it.
For something a little more advanced you can use the forms Form.OpenArgs property.
The code below does not allow the form to be opened unless an OpenArgs is supplied.
That may not be necessary but It prevents users from opening the form through a route I do not wish them to take.
Then the code behind the "Create" button will be something like:
DoCmd.OpenForm "Form2", acNormal, , , , acNormal, me!person_ID
On the form you wish to open on the specific record on ( Form2 ) use the following code in the "On Open" event:
Private Sub Form2_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) Then
MsgBox "Open from the first form, I won't work otherwise"
DoCmd.Close , "Form2"
Cancel = True
Exit Sub
End If
Me.Filter = "person_ID = """ & Me.OpenArgs & """"
Me.FilterOn = True
End Sub
The filter is now set in the second form's "On Open" event rather than being supplied by the WhereCondition on the first forms Create buttons "On Click" event.
As a design choice have you thought about using a single form and having a "Tab Control" with a tab in place of each of your forms. Instead of the Create button opening the next form it simply changes tab.
This is my first post on stackoverflow, did I do good?

Programmatic changes to form won't save

Does anyone have an idea what could prevent either of these commands from actually saving a form? Is there a setting I'm missing that could make the form "read-only"? I'm toggling the control.enabled property on the form on load, but I can't seen to make it stick once I've closed the form.
DoCmd.Close acForm, Me.Name, acSaveYes
DoCmd.Save acForm, Me.Name
Edit:
Some psuedo code to clarify.
This function runs on form load to enable/disable certain controls and works 100% as expected.
Private Sub Form_Load()
If IsNull(TempVars.Item(Me.Name)) Then
TempVars.Add Me.Name,1
If somecondition = true then
Me.Controls.Item("somecontrol").enabled = True
Else
Me.Controls.Item("somecontrol").enabled = False
End If
End If
End Sub
This click event closes the form and opens another form. The problem is, the control.enabled settings I set on form load do not save.
Private Sub button_OnClick()
DoCmd.OpenForm "someotherform", acnormal
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
The next time I open the first form, the control properties will not be set, and I need to set the conrol.enabled property again.
I've not been able to find any documentation, but it appears that you can not save design changes that are made in Normal mode. I ended up declaring a global collection type variable. I stored concatenated strings in this collection variable of the convention "Form.Name & Control.Name". If the form had never been opened, I ran the long check on which controls should be enabled, and stored them in this collection global. If the form had been opened at least once, I looped through the controls on the form checking to see if they were in my global collection of "authorized" controls. It feels really hackish, but it works pretty well.
The code you have (Close, then Save) fails on my testing because the Me.Name is empty after executing the Close. Although I don't know what other code you may have, I was able to get the Form to close once I switched the order.
I suggest you place the code in the 'Open' event as follows:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Save acForm, Me.Name
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
Do you want to change the enabled property on a condition? If you just need that control disabled IF the record ... then you'll want to move the enabling the form's Current event, which fires at every record navigation. Load and Open are too early and don't repeat at navigation.
Private Sub Form_Current()
If Me!X = True Then
Me.Controls("somecontrol").Enabled = False
Else
Me.Controls("somecontrol").Enabled = True
End If
End Sub

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.