manipulating variable and retrieving report - ms-access

I'm trying to work out a problem and need some help. I have 3 types of users (T8XXXXX, P9XXXXX and XXXXX) each of these users have different elements and as such has a different report. The top two if statements work, as far as opening the reports, one report is blank and the third fails all together. I'm not really sure how to solve this, and am hoping for some help from professionals. Any help is greatly appreciated!
Private Sub cmdPrintIndRep_Click()
Dim strUser As String
strUser = Forms.frmsupresults.subfrmUsrRes!UserName
If Left(strUser, 2) Like "t8" Then
DoCmd.OpenReport "FedInvest - ISSR Recertification Report", acViewPreview
ElseIf Left(strUser, 2) Like "p9" Then
DoCmd.OpenReport "Courts - ISSR Recertification Report", acViewPreview
Else Left(strUser, 2) not Like "p9" and not like "t8"" Then
DoCmd.OpenReport "FedInvest - ISSR Internal Users Recertification Report", acViewPreview
End If
End Sub

Disclaimer: I'm no professional but hopefully I can help.
I adjusted the definition of strUser by using a reference to referring to controls found here.
Assuming there are only 3 types of users, I simply removed the last if statement. If it is not the 1st type, nor the 2nd type, it must be the 3rd type of user.
Lastly, we should use = instead of Like in the conditions for the if statements.
Private Sub cmdPrintIndRep_Click()
Dim strUser As String
strUser = Forms!frmsupresults!subfrmUsrRes.Form!UserName
If Left(strUser, 2) = "t8" Or Left(strUser, 2) = "T8" Then
DoCmd.OpenReport "FedInvest - ISSR Recertification Report", acViewPreview
ElseIf Left(strUser, 2) = "p9" Or Left(strUser, 2) = "P9" Then
DoCmd.OpenReport "Courts - ISSR Recertification Report", acViewPreview
Else
DoCmd.OpenReport "FedInvest - ISSR Internal Users Recertification Report", _
acViewPreview
End If
End Sub
As far as your reports being blank, what is their Record Source?

Related

Error On click Button Docmd Report Error 3464

Hi Everyone How Are You I hope You Are Well.
I Am Working On A MS Access Application And Want Fetch Result From List Box With On Click Button
But I'm Facing A Error Data Type Mismatch In Criteria expression
Here is My Code On Click Button
Private Sub ViewBalance_Click()
'Dim listrpt As String
If IsNull(listrpt) Then
MsgBox "Please Select The Account Name", vbOKOnly, "Warning"
Exit Sub
End If
DoCmd.OpenReport "List All Parties Balance", acViewPreview, , "[Account ID] = """ & listrpt &
""""
Me.FilterOn = True
End Sub
And Here is My On Report Load Filter But This Filter Working Fine Above Code Give me Error
Report Load Code
Private Sub Report_Load()
DoCmd.Maximize
If MsgBox("Do You Want To Filter Available Balance Only?", vbYesNo + vbQuestion) = vbYes Then
Dim strFilter As String
strFilter = "[RQ]>0"
'verify that it looks good
Me.Filter = strFilter
Me.FilterOn = True
End If
End Sub
Report Name: List All Parties Balance
Query Name Where is Field Name: Account ID is: ListBalance
And listrpt Response Account ID = 60023
Please Anyone Give me A Support & Help Thanks in Advance

Access report - Recordsetclone as. Recordsource

I have a form, which has button for report. I want to set Report .Recordsource to whatever on screen is, so basically I need .RecordsetClone of form send to Report. Here is what I tried, but It doesn't work:
Me.Recordsource= Forms!Myform.RecordsetClone
I get an invalid argument on that. Any ideas how to solve this ?
EDIT:
I tried this too - this button is placed on form which has records and opens Report :
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
Reports![MyReport].RecordSource = Me.RecordSource
Reports![MyReport].Filter = Me.Filter
Reports![MyReport].FilterOn = True
End Sub
You can't do that, but you may get away with:
Me.RecordSource = Forms!Myform.RecordSource
though that will not include a filter applied to the form. However, the Filter can be copied the same way, and then:
Me.Filter = Forms!Myform.Filter
Me.FilterOn = True
while sorting must be specified in the report the usual way.
Proof of concept
Private Sub Report_Open(Cancel As Integer)
If MsgBox("Mod 2?", vbQuestion + vbYesNo, "Select") = vbYes Then
Me.RecordSource = "Select * From TestTable Where Id Mod 2 = 0"
End If
End Sub
Gustav, this is correct answer. I have opened another thread for that, but I wasn't aware of what is wrong and where. Sorry for crossposting. here is link to my thread:
Access Report - show current recordsource of another form
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Moderators can delete one of the threads, I can't do that.

VBA Access strange behavior with global variables

I have a form that calls a series of reports, one after the other. I also have a global variable that I use as a counter. Every time a page's PageFooterSection_Format is called, I want to increment the count. The problem is, the counter is counting higher that it should. Here is my code:
I have a module called genericFunctions:
Option Compare Database
Public pageCount As Integer
In my form, I have a loop that calls this:
'before loop
pageCount = 1
'start loop, which I left out for brevity
'run in previewView first so page footer format function is called
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
'then run this to open in report view, so onload event runs
DoCmd.OpenReport reportName, acViewReport, , , acHidden, !ID
'save report as a pdf
DoCmd.OutputTo acOutputReport, reportName, "PDF", rptPath
'close report
DoCmd.Close acReport, reportName
'I then have a method that "stitches" these individual reports into one PDF
now, in my report, I have this code:
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If (Report.CurrentView = 5) Then
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
End If
End Sub
I then place a breakpoint in the event. This event is hit 3 times, as it should. So, I would expect pgNumber text box to have 1,2,3 as their values but it has 2,4,6 instead. No where else in my code do I increment the pageCount variable. What's going on? Is this a scope issue?
PageFooterSection_Format runs both for DoCmd.OpenReport reportName, acViewPreview and for DoCmd.OutputTo acOutputReport, reportName, "PDF", because the latter is like a print command.
In both cases, Report.CurrentView = acViewReport (5). I'm not sure what to make of that - it feels like a bug. (Note: this is Access 2010)
Anyway, since DoCmd.OutputTo is like a printing (and thus like a print preview), you can simply omit this line:
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
At least that worked for me.
Note: Breakpoints in event procedures can be misleading as to what happens when. It is safer to use Debug.Print calls.
My test code was:
Sub ReportTesting()
Const reportName = "Bericht4"
'before loop
pageCount = 1
'run in previewView first so page footer format function is called
Debug.Print pageCount, "acViewPreview"
DoCmd.OpenReport reportName, acViewPreview, , , acHidden
'then run this to open in report view, so onload event runs
Debug.Print pageCount, "acViewReport"
DoCmd.OpenReport reportName, acViewReport, , , acHidden
'save report as a pdf
Debug.Print pageCount, "acOutputReport"
DoCmd.OutputTo acOutputReport, reportName, "PDF", "C:\test.pdf"
'close report
DoCmd.Close acReport, reportName
Debug.Print pageCount, "done"
End Sub
and
Private Sub Report_Open(Cancel As Integer)
Debug.Print "Report View: " & Me.CurrentView
End Sub
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If Me.CurrentView = acViewReport Then
Debug.Print "Footer - acViewReport"
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
Else
Debug.Print "Footer - other view" ' <-- this never happens
End If
End Sub
Output in Immediate window:
1 acViewPreview
Report View: 5
Footer - acViewReport
2 acViewReport
Report View: 6
2 acOutputReport
Footer - acViewReport
3 done

Access VBA Multiple Sorting Criteria

The Case: I have a form that has several fields of entry. A button contained within this form runs a query and generates a report based on the form entry fields.
I have created 3 comboboxes that will allow the user to sort the report on various criteria (ie. sort by Analyst name, then by Meeting Date, then by Ticker).
Combo Box 1 = Sort_By;
Combo Box 2 = Sort_By_2;
Combo Box 3 = Sort_By_3
The Code:
Private Sub Run_Query_Button_Click()
If Revisit_Check.Value = False Then
DoCmd.OpenQuery "Important Information Extracted"
DoCmd.Close
DoCmd.OpenReport "Important Information Extracted", acViewReport
DoCmd.SetOrderBy Sort_By Sort_By_2 Sort_By_3
Else
DoCmd.OpenQuery "Revisit"
DoCmd.Close
DoCmd.OpenReport "Revisit_Report", acViewReport
DoCmd.SetOrderBy Sort_By Sort_By_2 Sort_By_3
End If
End Sub
This code returns a syntax error. It does not sort on the three "sort by" criteria. If I use only on of the criteria in the following fashion:
Private Sub Run_Query_Button_Click()
If Revisit_Check.Value = False Then
DoCmd.OpenQuery "Important Information Extracted"
DoCmd.Close
DoCmd.OpenReport "Important Information Extracted", acViewReport
DoCmd.SetOrderBy Sort_By
The code runs properly and sorts on the given "sort by" value. If instead of using the form field comboboxes to sort, I use the actual field names, for instance:
Private Sub Run_Query_Button_Click()
If Revisit_Check.Value = False Then
DoCmd.OpenQuery "Important Information Extracted"
DoCmd.Close
DoCmd.OpenReport "Important Information Extracted", acViewReport
DoCmd.SetOrderBy "Analyst, Meeting Date, Ticker"
...
Everything works fine as well. Why, when I use the three sort criteria, do I get a syntax error? How can I sort on these three criteria?
Assuming Sort_By is the values you get in the comboboxes you are seeking you will need something more like this:
Private Sub Run_Query_Button_Click()
If Revisit_Check.Value = False Then
DoCmd.OpenQuery "Important Information Extracted"
DoCmd.Close
DoCmd.OpenReport "Important Information Extracted", acViewReport
DoCmd.SetOrderBy Sort_By & ", " & Sort_By_2 & ", " & Sort_By_3
Else
DoCmd.OpenQuery "Revisit"
DoCmd.Close
DoCmd.OpenReport "Revisit_Report", acViewReport
DoCmd.SetOrderBy Sort_By & ", " & Sort_By_2 & ", " & Sort_By_3
End If
End Sub
Notice the & ", " & to make it into the same kind of string as "Analyst, Meeting Date, Ticker" which you said had worked. Otherwise your sort you are sending in the first time would look like this: "AnalystMeeting DateTicker"

How to use a function in filter string in access report?

I have a function defined as this in VBA:
Function IsInWeek(RefDate As Date, checkDate As Date) As Boolean
StartDate = StartOfWeek(RefDate)
EndDte = EndOfWeek(RefDate)
If (checkDate >= StartDate And checkDate < Enddate) Then
IsInWeek = True
Else
IsInWeek = False
End If
End Function
I want to use it inside a filter clause of a report as follow:
strFilte = "IsInWeek(#" + Format(InDate, "dd/mm/yyyy") + "#, CalanderDate)"
DoCmd.OpenReport ReportName, acViewPreview,
With Reports(ReportName)
.Filter = strFilter
.FilterOn = True
End With
But it doesn't work. The report contains all records. What is the problem and how can I solve it? I am using access 2003.
Note: I can use between to implement function on filter string, but I am looking to find why the above technique doesn't work.
Edit 1
This is not working too:
strFilte = "IsInWeek(#" + Format(InDate, "dd/mm/yyyy") + "#, [CalanderDate])= True"
If Application.CurrentProject.AllReports(reportname).IsLoaded = True Then
DoCmd.Close acReport, reportname
End If
DoCmd.OpenReport reportname, acViewDesign, strFilter
DoCmd.OpenReport reportname, acViewPreview
The DoCmd.OpenReport method takes as it's last parameter an optional filter clause. So maybe:
DoCmd.OpenReport ReportName, acViewPreview, ,"IsInWeek(#" + Format(InDate, "dd/mm/yyyy") + "#, [CalanderDate]) = True"
As far as the reason the method you tried doesn't work, I remember Access being particularly fussy with Reports (as opposed to Forms) regarding the Filter property; not sure if you can do it on the fly like that. To achieve what you are trying to do without using DoCmd.OpenReport with a filter option, I think you have to open the report in Design mode, change the property, then change to Preview mode. I remember forms being more forgiving about this, however. That's just from memory, though.
EDIT: My syntax was incorrect, have added in an extra comma between the parameters; as per http://msdn.microsoft.com/en-us/library/office/aa220304%28v=office.11%29.aspx, FilterName is the third param, WhereCondition is the fourth. I was looking for WhereCondition.