Programmatic changes to form won't save - ms-access

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

Related

How to close a spawned VBA Access user form via a close button w/o using DoCmd

I have a main form named Calling. It has a button named Travel. That Travel button does:
Private Sub btnTravel_Click()
On Error GoTo btnTravel_Click_Err
DoCmd.OpenForm "Travel", acNormal, "", "", acFormEdit
'If you can find a cleaner way to open a form I will be thankful.
btnTravel_Click_Exit:
Exit Sub
btnTravel_Click_Err:
MsgBox Error$
Resume btnTravel_Click_Exit
End Sub
The Travel info form performs correctly. That Travel form has a Close button with the code:
Private Sub bntClose_Click()
Unload Me
End Sub
When pressed, the Close code generates "Run-time error '361': Can't load or unload this object.
Your help is much appreciated.
You do not need the commas with the empty strings, nor do you need the acFormEdit as when you open the form you will be able to edit and add new records anyway.
If you leave this argument blank the form will open in the data mode set by the forms AllowEdits, AllowDeletions, AllowAdditions, and DataEntry permissions (in the form properties).
DoCmd.OpenForm "Travel", acNormal
As for the next sub routine, I would use docmd.close instead of unload.
Private Sub bntClose_Click()
Me.Undo
DoCmd.Close acForm, "Travel", acSaveNo
End Sub
The me.undo is optional, if you don't want to save, and if you want to save the form change the acSaveNo to acSaveYes.
EDIT:
I have just re-read your question and noticed in the title you want to do this without docmd.
I have had a think about this and docmd is the standard way of closing forms in access using VBA. I am not sure if you have inherited the unload from using VB, but I would stick to docmd.close when using access.
"Unolad me" just doesn't work in Access Form objects.
Access wants you to use DoCmd.Open/DoCmd.Close but you are clever than that and can use Access Form Object as an object. Access names the Form Classes prefixed the name you give them with "Form_". You create a Form named "YourForm", Access create the Class "Form_YourForm". Use this class as a real object:
'Declare a typed variable
Dim f As Form_YourForm
'Create the object
Set f = New Form_YourForm 'This triggers the Open event
'Use the object
f.SetFocus
f.Resize
'... And eventually, dispose the object
Set f = Nothing
'Remember <<Unload f>> won't work, neither you can use DoCmd.Close
Also, you can use Form_YourForm directly as an object variable because VBA Access just create "implicitly" this object out of the class Form_YourForm when you first use it or when you use DoCmd.Open. (Yes, it's a little bit confusing, but Access was create for users that didn't have necessarily programmer skills).
However, you'll get a different instance of the Form_YourForm Class each time you use a variable object typed as any of the Form Class that exist in your project. This means you can open as many instances of a form as you want ...or while it fits your computer's memory. You can't acomplish it using DoCmd.Open.
The main "disadvantages" are that you have to handle the form object from another module. You can still use the "Forms" collection but since you don't know the given key you can't reach your form instance easily. Also, you can't close the form from its own code, but only disposing the typed variable (Set f=nothing).

Access 2010 Form won't save record

I have a form with some fields on it. Form was created from the table in question.
I've tried to use the button wizard to create a save button, I've tried to use the following:
Private Sub saveRecord_Click()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Nothing works, any method I try to save the info doesn't work. I've made sure the form is bound, as far as I can tell it is, it was created off that table.
I have this line when the form loads to make sure the form is on a new record:
Me.Recordset.AddNew
From my limited understanding of the language, setting the form to a new record and then doing a save command should work?
RunCommand acCmdSaveRecord will only do a save if there is something to save. Simply adding a record to the form's recordset doesn't give you anything to save.
Make this change to saveRecord_Click() to confirm whether that is the explanation:
Private Sub saveRecord_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "nothing to save"
End If
End Sub
Some other suggestions ...
Since you want to go to the new record when the form loads, use GoToRecord instead of Me.Recordset.AddNew:
Private Sub Form_Load()
DoCmd.GoToRecord Record:=acNewRec
End Sub
The version of saveRecord_Click() I suggested earlier was only to explore the problem. But for routine use, it doesn't make sense to allow the user to click a saveRecord button and then tell them there is nothing to save. It is better to only make the button clickable when there is something to save.
So you can disable the command button in the form's On Current event and enable it in the On Dirty event (which means there is something to save).
Private Sub Form_Current()
Me.saveRecord.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me.saveRecord.Enabled = True
End Sub
Also, as the last step of saveRecord_Click(), you may wish to SetFocus on another control and disable the command button.

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.