1
votes

I am creating a report via vba. Basically when I am trying to change the recordsource value of a report to a new query, the "Enter Parameter Value" dialog box pops up for the old query. Where I have to manually enter the values even though they are in the query already.

Dim rpt As Report
Set rpt = CreateReport
With rpt
    .Width = 10000
   .RecordSource = info_string 'this is the first query
    .Caption = title
End With

a code which prints all "info_string" query to the report

With rpt
    .RecordSource = filter_information 'this is the second query
End With

a code which runs and is supposed to print all the results of the second query to the rpt report

this is where the issues comes up. It's like i cannot reassign a new query to the recordsource[![i am being asked to asign values for the fields of the first query(info_site)][1]][1]. When i try to use rpt.requery, I get an error that the command is not valid

DoCmd.OpenReport rpt.Name, acViewPreview
rs.Close
Set rs = Nothing
Set rpt = Nothing
Set db = Nothing
End Sub

NB: the queries are not the issue because when i run them separately, they work perfectly. The issue is when i try to reassign the report's recordsource to a new value.

'I can print the different queries on the report separately but I cannot do them together. [1]: https://i.stack.imgur.com/WKzho.png

this is the full code if anyone is interested

Public Sub Report_Generator_Function(info_string As String, filter_information As String)
Dim str As String
Dim rs As Recordset
Dim rs_filter As Recordset
Dim fld As Field
Dim lngtop As Long
Dim lngleft As Long
Dim title As String
Dim report_label As Label
Dim txtbox As TextBox
Dim col As Long
Dim rpt As Report
Dim rect As Rectangle

col = RGB(104, 138, 38)

title = "ACTIVITY REPORT"
lngleft = 0
lngtop = 0
Set rpt = CreateReport

With rpt
    .Width = 10000
    .RecordSource = info_string
    .Caption = title
End With

Set db = CurrentDb
Set rs = db.OpenRecordset(info_string)

Set report_label = CreateReportControl(rpt.Name, acLabel, acPageHeader, , "Customer Information", 0, 0)
report_label.FontBold = True
report_label.FontSize = 20
report_label.ForeColor = RGB(255, 255, 255)

report_label.SizeToFit
rpt.Section(3).BackColor = col
rpt.Section("Detail").Height = 5500



For Each fld In rs.Fields
    Set txtbox = CreateReportControl(rpt.Name, acTextBox, acDetail, , fld.Name, lngleft + 1900, lngtop)
    txtbox.SizeToFit
    txtbox.TextAlign = 1
    txtbox.Width = 5000
    txtbox.BorderStyle = 0
    Set report_label = CreateReportControl(rpt.Name, acLabel, acDetail, txtbox.Name, fld.Name, lngleft, lngtop, 1400, txtbox.Height)
    report_label.SizeToFit
    txtbox.TextAlign = 1
    txtbox.Width = 5000
    lngtop = lngtop + txtbox.Height + 25
Next

Set rect = CreateReportControl(rpt.Name, acRectangle, acDetail, "filters", 0, 0, 3500, 0)
With rect
    .Width = 2000
    .Height = 500
    .BackColor = col
End With
    Set report_label = CreateReportControl(rpt.Name, acLabel, acDetail, "Random", "FILTERS", 100, lngtop + 870, 3500, 0)
    report_label.SizeToFit
    report_label.ForeColor = RGB(255, 255, 255)
    report_label.FontSize = 13
    report_label.FontWeight = 700
    txtbox.TextAlign = 0
    txtbox.Width = 5000



With rpt
    .RecordSource = filter_information
End With

Set rs_filter = db.OpenRecordset(filter_information)
For Each fld In rs_filter.Fields
    Set txtbox = CreateReportControl(rpt.Name, acTextBox, acDetail, , fld.Name, lngleft + 1900, lngtop)
    txtbox.SizeToFit
    txtbox.TextAlign = 1
    txtbox.Width = 5000
    txtbox.BorderStyle = 0
    Set report_label = CreateReportControl(rpt.Name, acLabel, acDetail, txtbox.Name, fld.Name, lngleft, lngtop, 1400, txtbox.Height)
    report_label.SizeToFit
    txtbox.TextAlign = 1
    txtbox.Width = 5000
    lngtop = lngtop + txtbox.Height + 25
Next





Set report_label = CreateReportControl(rpt.Name, acLabel, acPageFooter, , Now(), 0, 0)
'Create a numbering at the footer
Set txtbox = CreateReportControl(rpt.Name, acTextBox, acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", rpt.Width - 1000, 0)
txtbox.SizeToFit

DoCmd.OpenReport rpt.Name, acViewPreview
rs.Close
Set rs = Nothing
Set rpt = Nothing
Set db = Nothing

End Sub
1
What is so different about these queries that you must change report RecordSource? I have done this only once. I pass SQL statement with OpenArgs and then in report Open event: Me.RecordSource = Me.OpenArgsJune7
Why even build report with code?June7
this is because it is a procedural report. the reported is generated from a form. So the user selects the company and site and then its information displayed on the form. There is an option for the user to generate a report so it can be printed. So the values keeps on changing. So the code is meant too create a report and populate the report with the values.user19068
What is a 'procedural' report? What do you mean by 'generated from a form'? Options are selected on form, such as filter criteria? I tried testing your code. Do you have Option Explicit in module header? Variable db is not declared. Have you step-debugged? Where are info_string and filter_information variables populated? I get 'member not found' error on fld.Name. I followed code in this article and it works bytes.com/topic/access/insights/…June7
When I set a report with textboxes bound to fields then remove the RecordSource, report opens with #Name? errors in textboxes, no prompts. So, not the entire reason for prompts. However, rest of my comment applies. I don't know your data so I can't advise exactly how to resolve. Maybe should be a report/subreport arrangement. Maybe should be a query joining these two datasets.June7

1 Answers

0
votes

At first glance, it seems that you are creating a report that takes a query as the source of the record and add the fields corresponding to that query. Then you change the source of the report and add the fields of the new query. The problem is that the report can only have one record source, and the fields added from the first query are preserved but do not exist in the second. That is why these fields are considered as parameters and, since the system do not know their value, you are asked to tell them.