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
Me.RecordSource = Me.OpenArgs
– June7fld.Name
. I followed code in this article and it works bytes.com/topic/access/insights/… – June7