Using VBA to set title of report to static value (1 Viewer)

tmyers

Well-known member
Local time
Yesterday, 19:15
Joined
Sep 8, 2020
Messages
1,090
I have some code that uses a "dummy" query to generate a report based from a crosstab. So when the report is generated, it just has the name of the crosstab query as its title.

Why is the correct method to change that? Every report generated will have the same value ("Counts Comparison"). I am pretty sure it deals with the
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:15
Joined
Oct 29, 2018
Messages
21,454
Are you talking about the Report Title? Or, are you saying you "generate" a report using code, as in, creating a new report in design using code?
 

tmyers

Well-known member
Local time
Yesterday, 19:15
Joined
Sep 8, 2020
Messages
1,090
I am referring to the report title. The code I am using to generate the report from scratch, but cant figure out how to properly set the value for the title. The code that generates the report is as follows for your reference:
Code:
Private Sub Command38_Click()
    Dim sql As String
    sql = "PARAMETERS [Forms]![JobQuote]![JobID] Short; " & _
            "TRANSFORM First(ContractorCountQry.Count) AS FirstOfCount " & _
            "SELECT ContractorCountQry.TypeName, Sum(ContractorCountQry.Count) AS [Total Of Count] " & _
            "FROM ContractorCountQry " & _
            "GROUP BY ContractorCountQry.TypeName " & _
            "PIVOT ContractorCountQry.Contractor;"
  
    CreateAutoReport sql
  
  
End Sub

Public Sub CreateAutoReport(strSQL As String)
Dim rptReport As Access.Report
Dim strCaption As String
Dim rpt As Access.Report


     CurrentDb.QueryDefs("qryDummy").sql = strSQL

     ' Open dummy query to invoke NewObjectAutoReport command on it
     ' Put the report created to design view to make properties editable
     With DoCmd
         .OpenQuery "ContractorCountQry_Crosstab", acViewNormal
         .RunCommand acCmdNewObjectAutoReport
         .Close acQuery, "ContractorCountQry_Crosstab"
         .RunCommand acCmdDesignView
     End With

     ' Get reference to just created report
     For Each rpt In Reports
         If rpt.Name Like "ContractorCountQry_Crosstab*" Then Set rptReport = rpt
     Next

     With rptReport

      

         ' Create timestamp on footer
         CreateReportControl .Name, acLabel, _
             acPageFooter, , Now(), 0, 0

         ' Create page numbering on footer
         With CreateReportControl(.Name, acTextBox, _
             acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", _
             .Width - 1000, 0)
             .SizeToFit
         End With

         ' Detach the report from dummy query
         '.RecordSource = strSQL

         ' Set the report caption to autogenerated unique string
         strCaption = GetUniqueReportName
         If strCaption <> "" Then .Caption = strCaption

     End With

     DoCmd.RunCommand acCmdPrintPreview

     Set rptReport = Nothing

End Sub


Public Function GetUniqueReportName() As String
Dim intCounter As Integer
Dim blnIsUnique As Boolean

     For intCounter = 1 To 256
         GetUniqueReportName = "rptAutoReport_" & Format(intCounter, "0000")
         blnIsUnique = True
         For Each rpt In CurrentProject.AllReports
             If rpt.Name = GetUniqueReportName Then blnIsUnique = False
         Next
         If blnIsUnique Then Exit Function
     Next

     GetUniqueReportName = ""

End Function
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:15
Joined
Oct 29, 2018
Messages
21,454
I am referring to the report title. The code I am using to generate the report from scratch, but cant figure out how to properly set the value for the title. The code that generates the report is as follows for your reference:
Code:
Private Sub Command38_Click()
    Dim sql As String
    sql = "PARAMETERS [Forms]![JobQuote]![JobID] Short; " & _
            "TRANSFORM First(ContractorCountQry.Count) AS FirstOfCount " & _
            "SELECT ContractorCountQry.TypeName, Sum(ContractorCountQry.Count) AS [Total Of Count] " & _
            "FROM ContractorCountQry " & _
            "GROUP BY ContractorCountQry.TypeName " & _
            "PIVOT ContractorCountQry.Contractor;"
 
    CreateAutoReport sql
 
 
End Sub

Public Sub CreateAutoReport(strSQL As String)
Dim rptReport As Access.Report
Dim strCaption As String
Dim rpt As Access.Report


     CurrentDb.QueryDefs("qryDummy").sql = strSQL

     ' Open dummy query to invoke NewObjectAutoReport command on it
     ' Put the report created to design view to make properties editable
     With DoCmd
         .OpenQuery "ContractorCountQry_Crosstab", acViewNormal
         .RunCommand acCmdNewObjectAutoReport
         .Close acQuery, "ContractorCountQry_Crosstab"
         .RunCommand acCmdDesignView
     End With

     ' Get reference to just created report
     For Each rpt In Reports
         If rpt.Name Like "ContractorCountQry_Crosstab*" Then Set rptReport = rpt
     Next

     With rptReport

     

         ' Create timestamp on footer
         CreateReportControl .Name, acLabel, _
             acPageFooter, , Now(), 0, 0

         ' Create page numbering on footer
         With CreateReportControl(.Name, acTextBox, _
             acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", _
             .Width - 1000, 0)
             .SizeToFit
         End With

         ' Detach the report from dummy query
         '.RecordSource = strSQL

         ' Set the report caption to autogenerated unique string
         strCaption = GetUniqueReportName
         If strCaption <> "" Then .Caption = strCaption

     End With

     DoCmd.RunCommand acCmdPrintPreview

     Set rptReport = Nothing

End Sub


Public Function GetUniqueReportName() As String
Dim intCounter As Integer
Dim blnIsUnique As Boolean

     For intCounter = 1 To 256
         GetUniqueReportName = "rptAutoReport_" & Format(intCounter, "0000")
         blnIsUnique = True
         For Each rpt In CurrentProject.AllReports
             If rpt.Name = GetUniqueReportName Then blnIsUnique = False
         Next
         If blnIsUnique Then Exit Function
     Next

     GetUniqueReportName = ""

End Function
I see. You are creating a new report using code. This is not normally needed and usually avoided because you won't be able to use the runtime or compile your project to ACCDE.

In any case, according to your code, you are specifying the report's title using the GetUniqueReportName() function. So, if you don't want the report to use that title, what do you want to use instead?
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:15
Joined
Sep 21, 2011
Messages
14,238
The caption is just that. Try changing it in the report properties and see what happens.
A report created with the wizard normally has a title control of Auto_title0 ?
 

tmyers

Well-known member
Local time
Yesterday, 19:15
Joined
Sep 8, 2020
Messages
1,090
The caption is just that. Try changing it in the report properties and see what happens.
A report created with the wizard normally has a title control of Auto_title0 ?
That is correct. It is Auto_Header0, but when I tried something a long the lines of me.Auto_Header0.caption = "blah" it didnt work.
I see. You are creating a new report using code. This is not normally needed and usually avoided because you won't be able to use the runtime or compile your project to ACCDE.

In any case, according to your code, you are specifying the report's title using the GetUniqueReportName() function. So, if you don't want the report to use that title, what do you want to use instead?
I don't fully understand what you said. What do you mean in regards to runtime/compile to ACCDE?

As far as the GetUniqueReportName, it is just naming the report, not setting its title. Eventually once I get this working fully, while I know their are probably better ways, when the user generates the report, they can review it then choose to print. Once they close it, the report will be deleted. Since these are autogenerated, if they arent deleted, they will pile up.

This code while tweaked by me, was written by a really good friend of mine who is a developer (who mainly works in C#), but helps from time to time on my project.
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:15
Joined
Sep 21, 2011
Messages
14,238
That is correct. It is Auto_Header0, but when I tried something a long the lines of me.Auto_Header0.caption = "blah" it didnt work.

I don't fully understand what you said. What do you mean in regards to runtime/compile to ACCDE?

As far as the GetUniqueReportName, it is just naming the report, not setting its title. Eventually once I get this working fully, while I know their are probably better ways, when the user generates the report, they can review it then choose to print. Once they close it, the report will be deleted. Since these are autogenerated, if they arent deleted, they will pile up.

This code while tweaked by me, was written by a really good friend of mine who is a developer (who mainly works in C#), but helps from time to time on my project.
I do not think you can use Me as the code is not in the actual report.?
Use the . prefix as you did with the Caption ?

Not sure why you cannot create the report as normal and then just supply the recordsource if needed.? If you use the same query, you would not even need that.?
 

tmyers

Well-known member
Local time
Yesterday, 19:15
Joined
Sep 8, 2020
Messages
1,090
I do not think you can use Me as the code is not in the actual report.?
Use the . prefix as you did with the Caption ?

Not sure why you cannot create the report as normal and then just supply the recordsource if needed.? If you use the same query, you would not even need that.?
Ill be honest. I don't know. I currently have two versions of this I am playing with (probably bad practice). One version is the one you assisted me with this morning, and the other is this as my friend had just sent it to me, so I wanted to see how he did it.

At this point, I have tried I think 5 different methods to try to get this report to work how I need it to, but every iteration has a problem I can't seem to remedy. What I am trying to accomplish is far beyond my ability with Access and VBA currently, but I can't/wont just hand it to someone to do 100% for me. If I don't know how it functions/works, I can't maintain it if it were to break in the future.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:15
Joined
Oct 29, 2018
Messages
21,454
As far as the GetUniqueReportName, it is just naming the report, not setting its title.
Actually, the way I see it, that function is used to set the title of your new report with these lines in your code.
Code:
         ' Set the report caption to autogenerated unique string
         strCaption = GetUniqueReportName
         If strCaption <> "" Then .Caption = strCaption
So, if you're not getting the correct title, please answer my previous question: "What did you want the title to be?"
 

tmyers

Well-known member
Local time
Yesterday, 19:15
Joined
Sep 8, 2020
Messages
1,090
@theDBguy with Gas's input I was incorrectly trying to use "Me". Im too used to the code being in the object itself. Once he pointed that out, I was able to correct it.

As far the GetUniqueReportName function, the way me friend described it to me it was naming the report rptAutoreport_0001 and letting the title be set like it normally would through the wizard, in this case, what it is based off of (ContractorCountQry_Crosstab).

I apologize for not being clear. This aspect of my project has gotten me quite frustrated since I have failed so many times.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:15
Joined
Oct 29, 2018
Messages
21,454
@theDBguy with Gas's input I was incorrectly trying to use "Me". Im too used to the code being in the object itself. Once he pointed that out, I was able to correct it.

As far the GetUniqueReportName function, the way me friend described it to me it was naming the report rptAutoreport_0001 and letting the title be set like it normally would through the wizard, in this case, what it is based off of (ContractorCountQry_Crosstab).

I apologize for not being clear. This aspect of my project has gotten me quite frustrated since I have failed so many times.
Hi. Glad to hear you got it sorted out. Cheers!
 

Users who are viewing this thread

Top Bottom