OpenForm in acHidden, then Save does not work - vba

I'm trying to open form, change some properties, save, then close the form; all hidden from user.
For some reason, when I open form with acHidden mode, it throws me Error 29068 cannot complete this operation. You must stop the code and try again.
Here is the code :
DoCmd.OpenForm "frmProsContractorList", acDesign, , , , acHidden
DoCmd.Save acForm, "frmProsContractorList"
DoCmd.Close acForm, "frmProsContractorList"
If I use acWindowNormal instead of acHidden, no errors.
If I take out Save method, no errors.
It seems like OpenForm with acHidden and Save method does not work.
Can someone verify if above code works, and how to resolve?
WORK-AROUND : below works
DoCmd.OpenForm "frmProsContractorList", acDesign, , , , acHidden
DoCmd.Close acForm, "frmProsContractorList", acSaveYes
http://msdn.microsoft.com/en-us/library/ff192860(v=office.14).aspx

You can not edit a form's properties and then save the form. You can, however, edit some of a form's properties without opening the form. If you explain in full detail what you're trying to do, I can edit this answer to tell you how it's achieved. Until then, the answer is, "It can't be done the way you're doing it."
EDIT: You can simply set the field's Enabled property to True or False on the form's Load event.
i.e.
MyField.Enabled = False

Related

Get report name from DataSource of subform ms access vba

I have a form (say form1) to navigate to another form (form2) which has a subform that opens reports based on the DataSource put into by selecting the Text box on form1.
The code looks like this
Private Sub txt_DeliveryItemsSummary_Click()
DoCmd.OpenForm "frm_viewReports", acNormal
Forms!frm_ViewReports!subrpt_ReportArea.SourceObject = "Report.rpt_DeliveryItemsSummary"
End Sub
And on form2 I have a print button to print the report
The code looks like this
DoCmd.OpenReport subrpt_ReportArea.SourceObject, acViewNormal, , , acWindowNormal
But this code gives following error
Runtime Error 2103
The report name 'Report.rpt_DeliveryItemsSummary' you entered in either the property sheet or macro is misspelled or refers to a report that doesn't exist.
Any help how can I fix this issue.
Thanks a lot in advance.
Just figured it out myself. I knew i need to skip a piece of text but how! There I was getting trouble.
Here is the working code
DoCmd.OpenReport Right(subrpt_ReportArea.SourceObject, Len(subrpt_ReportArea.SourceObject) - 7), , acViewNormal, , , acWindowNormal
Thanks #Gustav for reaching out again.

Close an Access form and use next form in stack

I have a family history database. I occasionally need to add a father or mother to a record "on the fly". I do not want to close the database, add a record, then open it again. Accordingly, I added a button btnQuickAdd which opens a small form (frmQuickAdd) to add in the father/mother's name, date of birth, and gender. The frmQuickAdd has a "Save" button which closes the frmQuickAdd and then requeries the main form using code
DoCmd.Close acForm, "frmQuickAdd", acSaveYes
Me.Requery
However, using me.requery just results in error 2467 - the object is not open or does not exist. The main form clearly is still open. What am I doing wrong please?
As it is, you are trying the requery the frmQuickAdd.
I presume your frmQuickAdd is loaded through a button on the main form. If that's the case, then the .Requery needs to happen on the next line where the form is loaded.
Private Sub btnQuickAdd_Click()
'open form
DoCmd.OpenForm "frmQuickAdd", acNormal, , , acFormPropertySettings, acDialog
'requery
Me.Requery
End Sub
Also, you can avoid the hard-coded form name when closing by using the form's Name property:
DoCmd.Close acForm, Me.Name, acSaveYes
Edit
The above snippet will always requery the main form when the edit-form is loaded. If this behavior is not wanted, you can requery the main form when the save button is clicked using the form's name.
DoCmd.Close acForm, Me.Name, acSaveYes
Forms.MainForm.Requery
Note, MainForm must be loaded or you'll get an error.

Why form object doesn't load RecordSource attribute in VBA for Ms Access

I need to read programmatically the RecordSource attribute of a form, to know what table or query is the form depending on.
After some research, it seems that the form object needs to be actually open in a window (or hidden, but open) so that certain attributes can be accessed. I've put together the following code snippet:
Application.DoCmd.OpenForm "Form1", view:=acDesign, windowMode:=acWindowNormal
Dim frm as Access.Form
Set frm = Forms("Form1").Form
Debug.print frm.Name 'works ok
Debug.print frm.RecordSource 'prints an empty string
Application.DoCmd.Close AcForm, "Form1"
I can see that the form is correctly open, but the RecordSource attribute is empty, eventhough the form is fed from a table.
What could be going on? I'm using .accdb Ms Access database files. And have compacted the database, just in case the db were corrupted, but the issue remains.
Edit: The form is very minimal, and I realize that actually has no RecordSource at all; it only has a subform which, since it is indeed fed with a table, does have a non-blank RecordSource.
So VBA was right returning a blank RecordSource for the parent form. The script posted was correct after all. Thanks for your inputs!!
Any hints or directions would be greatly appreciated. Thank you in advance!!
Most likely, the RecordSource is assigned in code when the form is loaded, either as a table name or as SQL:
Me.RecordSource = "Select * From SomeTable Order By SomeField"
In design view you can't get RecordSource programmatically. You can see from Property window. You have to open it as normal form, may be you want it to open as hidden. You can specify WindowMode to open form hiddenly by DoCmd.OpenForm "Form1", acNormal, , , , acHidden. Try below codes.
Private Sub CmdTest_Click()
Dim frm As Access.Form
DoCmd.OpenForm "Form1", acNormal, , , , acHidden
Set frm = Forms("Form1")
Debug.Print frm.Name
Debug.Print frm.RecordSource
DoCmd.Close acForm, "Form1"
End Sub

Unable to refer form in another form's procedure

How do I refer a form when writing a procedure that is related to another form?
When I try to refer a form(not the current form) it returns null.in the image, the highlighted script in the project database window is where the code is written in.The form Forms("All Patient Info") is Null.
Image
This code is working. But the userform you are refering to must be opened, otherwise it won't be found. So add some routine like this:
If Not CurrentProject.AllForms("All Patient Info").IsLoaded Then
DoCmd.OpenForm "All Patient Info", acNormal
End If
Forms("All Patient Info").txtTest.Caption = "LEL"
If you don't want the form to be visible while you are doing something, add
Application.Echo False
before you open the form. When you are done modifying the form, close it with
DoCmd.Close acForm, "All Patient Info"
And Enable Echo again:
Application.Echo True

MS Access - "phantom" process created by canceling report

I have encountered a problem in Access 2010 for Windows:
Launch a report using doCmd.OpenReport
Set "Cancel = True" in an event associated with the report
Close Access
Access continues to run as a "phantom" process and must be killed in Task Manager. Access cannot open another database until this process is killed. The task initially shows some CPU utilization but quiets down to 0%.
I discovered this while using doCmd.OpenReport to launch a report. The report opens a form in the Report_Open event. The form prompts the user for report parameters and allows them to press "OK" to display the report or press "Cancel". The On Click event for "Cancel" sets "Cancel = True".
It appears that OpenReport() is not responding gracefully to the cancel. This seems like a frequently used technique so I hesitate to call it a bug and am wondering if I am doing something wrong. Actually... the more I explain this, it does sound like a bug. At this point, I am hoping somebody has a workaround or that I am missing something obvious.
This is just a simplified example I created to illustrate the problem:
form
Private Sub Form_Open(Cancel As Integer)
On Error GoTo errHandler
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
Exit Sub
errHandler:
Select Case Err.Number
Case 2501 ' Cancelled by user, or by NoData event.
MsgBox "Report cancelled, or no matching data.", vbInformation, "Information"
Case Else
MsgBox "Error " & Err & ": " & Error$, vbInformation, "Form_Open()"
End Select
Resume Next
End Sub
report
Private Sub Report_Open(Cancel As Integer)
Cancel = True
End Sub
The reason for the problem in this case is acDialog:
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
I think you will find:
DoCmd.OpenReport "Test Report", acViewPreview
Works without problems.
Edit re Comment
You should not need to cancel the report. For the most part, it is better to avoid errors than to trap them, so check for data and get parameters before opening the form. Move the form from the report open event to step of its own and use DLookUp or a query to check for data before launching the form.
I'm not in a position to test this problem, so I'll just offer a generic solution: Remove the data/recordset/recordsource from the report definition.
You can apply the recordsource to the report after you have determined the parameters. If the report is canceled, it is canceled without ever having a data connection.