Microsoft Access 2000 Switch Views at Runtime - ms-access

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.

Related

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

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

Access 2010: Which form control fires a macro?

I'm working on a legacy database. Specifically, changing a report. I have identified the queries/tables the report is based on. One of the tables has all the hallmarks of being a temprary table generated by a macro of which there are many dozens.
I've been able to identify the Append Query which generates the table and the macro that runs the query. Now I would like to find which form event fires that macro. Does it run everytime the report is generated or once a week or once a quarter or...
There is nothing in the macro behind the "button" that prints the report and no events are fired in the report.
I can iterate over every control in every form, but what property am I looking for?
Any pointers/key word guidance would be appreciated, thanks.
Some notes, it will be easier to find the relevant button when you have the name of the macro:
Sub FindMacros()
For Each f In CurrentProject.AllForms
DoCmd.OpenForm f.Name, acDesign
Set frm = Forms(f.Name)
For Each ctl In frm.Controls
If ctl.ControlType = acCommandButton Then
Debug.Print ctl.OnClick
End If
Next
DoCmd.Close acForm, f.Name, acSaveNo
Next
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.

Is there an easy way to find dependencies for a subform in Access?

For a given form in a database, is there a quick/easy way to find all other forms in an Access database use it as a subform?
Note: I am only concerned with main forms that have it defined using the property sheet, it is easy enough to do a code search for any form that dynamically sets it as a subform at runtime.
Right-click on the form in the database window and select "Object Dependencies" This should give you the list of forms that host it as a sub-form.
It is not too difficult to use VBA to check.
sfrmname="FormToFind"
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign
For Each ctl In Forms(frm.Name).Controls
If ctl.ControlType=acSubForm Then
If ctl.SourceObject = sfrmname Then
Debug.Print frm.Name
End If
End If
Next
DoCmd.Close acForm, frm.Name
Next
Or there abouts.