Get report name from DataSource of subform ms access vba - 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.

Related

VBA conditional formatting not visible in Print Preview

I am trying to apply conditional formatting to a report using VBA. This works if I open the report in Report View, but does not work if I open the report in Print Preview (unless I first open in Report View).
This issue does not apply to conditional formatting added via the wizard. That displays correctly in Print Preview.
I am using Office365 with Access Version 2208
My setup uses a form to allow a user to enter a date. This date is then passed to the report as an OpenArg:
Private Sub cmd_Ok_Click()
'DoCmd.OpenReport "Component Value", acViewPreview, , , , Me.txt_Date <- Does not show conditional formatting
DoCmd.OpenReport "Component Value", acViewReport, , , , Me.txt_Date
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Then the report adds a format condition in its OnLoad event
Dim strCondDate As String
Private Sub Report_Load()
If Not IsNull(Me.OpenArgs) Then
strCondDate = Me.OpenArgs
End If
If Not IsNull(strCondDate) Then
With Me.txt_Updated.FormatConditions
With .Add(acExpression, , "[Last Updated] < #" & strCondDate & "#")
.BackColor = vbYellow
End With
End With
End If
End Sub
I can breakpoint this code and see that it runs correctly when I open the report with acViewPreview, but the formatting is not visible.
Why does the format condition not appear in Print Preview (unless I first open the report in Report View)?
How can I get the formatting to be displayed?
Conclusion is Conditional Formatting rule cannot be programmatically added to report opened directly to Print or PrintPreview but can to ReportView. Fortunately, there is no need to use VBA. CF rule can grab OpenArgs content: Field Value Is less than CDate([OpenArgs]). Otherwise, have rule reference control on form that has user input.
Another alternative is to directly set Backcolor property in OnFormat or OnPrint event with conditional VBA structure. Drawback is these events do not execute in ReportView.

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

Pass Username through LoginForm, to GreetingForm, to NavigationForm

I have a problem with my database. When I Login the Username into the LoginForm the GreetingForm opens and the LoginForm closes. The username appears on the GreetingForm, but when I click on a button on the GreetingForm which than closes and Open the NavigationForm the username does not appear on the NavigationForm. Can you please help me fix this problem. Click on the link below to see more details.
Click on this link please
I would create a tempvars. These were introduced in access 2007 and unlike public variables, they will hold their value under the duress of a code error.
Tempvars are global , can be created in any module type and are supported in queries.
I would also change the textbox to a combobox. Then you can refer to the column number to select the initials.(column width 0cm)
How to use:
Option Compare Database
Option Explicit
Public G_Username As TempVars
Private Sub cmdLogin_Click()
If IsNull(cbo_username) Then
MsgBox "Please Select User"
Else
TempVars!G_Username = Me.cbo_username.column(1)
DoCmd.Close
If CurrentProject.AllForms("GreetingForm").IsLoaded = False Then
DoCmd.OpenForm "GreetingForm", acNormal, , , acFormEdit
End If
End If
End Sub
You can now refer to =G_Username in any form you require by creating a text box and entering =G_Username. Alternatively you should be able to do this in VBA like so:
Forms![GreetingForm]![txtLogin] = tempvars!G_Username
You can repeat this for initials, but refer to whichever column initials is in. Remember counting starts from column(0).

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

OpenForm in acHidden, then Save does not work

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