MS Access Report - Modify RecordSource with VBA Using Parameter from Main Form












0














I didn't realize my initial post (which I deleted) was posting under a newbie account - apologies......



In any event, here's my issue:



I have a main form that lists various "Consultants." This main form also has parameters "Consultant Type" "Start Date" and "End Date" that can be used to filter the list.



What's needed is a Report, listing "Consultants" of a certain type. Bear in mind that this "type" filter can apply to any of 4 different type fields ("type1", "type2", "type3", "type4").



What I want to do is place the button that runs the report onto the main form, because the "Consultant Type" parameter is already there. I then want to generate the SQL based off that parameter (and a date parameter that I've already done), and use that recordset to populate the report.



The SQL works fine, but, the resulting data isn't being passed to the report, so I'm getting all records instead of just the ones I want. The recordsource of the report object is set to the background table in order to populate fields/textboxes.



Here's the code



On the Main Form



Private Sub Button_Click()
Dim s As String
If IsNull(Me.Type.Value) Then
MsgBox "Please Select a Type", vbCritical, "Report Error"
Else
s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"
DoCmd.OpenReport "RptList", acViewPreview, , , acDialog
End If


End Sub



Now, obviously, it all goes to pot when I open the report, because there's nothing in the DoCmd that says "Using this recordset." And that's my problem.



I'm trying to avoid creating a separate little dropdown form for the purpose of populating a piece of data that's right in front of me. Also, plugging the SQL directly into the report's recordsource using Forms![MainForm]![Type].Value for the "Type" variables didn't work. I suspect it might have something to do with the form not being active when the report button is clicked?



I thought QueryDef might work. But, I'm not grasping how to implement it, in order to know IF it will work. And I've been Googling in circles......



I don't know what I'm missing. HELP!










share|improve this question


















  • 2




    Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
    – C Perkins
    Nov 20 at 20:46










  • I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
    – JuniperSquared
    Nov 21 at 15:18


















0














I didn't realize my initial post (which I deleted) was posting under a newbie account - apologies......



In any event, here's my issue:



I have a main form that lists various "Consultants." This main form also has parameters "Consultant Type" "Start Date" and "End Date" that can be used to filter the list.



What's needed is a Report, listing "Consultants" of a certain type. Bear in mind that this "type" filter can apply to any of 4 different type fields ("type1", "type2", "type3", "type4").



What I want to do is place the button that runs the report onto the main form, because the "Consultant Type" parameter is already there. I then want to generate the SQL based off that parameter (and a date parameter that I've already done), and use that recordset to populate the report.



The SQL works fine, but, the resulting data isn't being passed to the report, so I'm getting all records instead of just the ones I want. The recordsource of the report object is set to the background table in order to populate fields/textboxes.



Here's the code



On the Main Form



Private Sub Button_Click()
Dim s As String
If IsNull(Me.Type.Value) Then
MsgBox "Please Select a Type", vbCritical, "Report Error"
Else
s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"
DoCmd.OpenReport "RptList", acViewPreview, , , acDialog
End If


End Sub



Now, obviously, it all goes to pot when I open the report, because there's nothing in the DoCmd that says "Using this recordset." And that's my problem.



I'm trying to avoid creating a separate little dropdown form for the purpose of populating a piece of data that's right in front of me. Also, plugging the SQL directly into the report's recordsource using Forms![MainForm]![Type].Value for the "Type" variables didn't work. I suspect it might have something to do with the form not being active when the report button is clicked?



I thought QueryDef might work. But, I'm not grasping how to implement it, in order to know IF it will work. And I've been Googling in circles......



I don't know what I'm missing. HELP!










share|improve this question


















  • 2




    Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
    – C Perkins
    Nov 20 at 20:46










  • I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
    – JuniperSquared
    Nov 21 at 15:18
















0












0








0







I didn't realize my initial post (which I deleted) was posting under a newbie account - apologies......



In any event, here's my issue:



I have a main form that lists various "Consultants." This main form also has parameters "Consultant Type" "Start Date" and "End Date" that can be used to filter the list.



What's needed is a Report, listing "Consultants" of a certain type. Bear in mind that this "type" filter can apply to any of 4 different type fields ("type1", "type2", "type3", "type4").



What I want to do is place the button that runs the report onto the main form, because the "Consultant Type" parameter is already there. I then want to generate the SQL based off that parameter (and a date parameter that I've already done), and use that recordset to populate the report.



The SQL works fine, but, the resulting data isn't being passed to the report, so I'm getting all records instead of just the ones I want. The recordsource of the report object is set to the background table in order to populate fields/textboxes.



Here's the code



On the Main Form



Private Sub Button_Click()
Dim s As String
If IsNull(Me.Type.Value) Then
MsgBox "Please Select a Type", vbCritical, "Report Error"
Else
s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"
DoCmd.OpenReport "RptList", acViewPreview, , , acDialog
End If


End Sub



Now, obviously, it all goes to pot when I open the report, because there's nothing in the DoCmd that says "Using this recordset." And that's my problem.



I'm trying to avoid creating a separate little dropdown form for the purpose of populating a piece of data that's right in front of me. Also, plugging the SQL directly into the report's recordsource using Forms![MainForm]![Type].Value for the "Type" variables didn't work. I suspect it might have something to do with the form not being active when the report button is clicked?



I thought QueryDef might work. But, I'm not grasping how to implement it, in order to know IF it will work. And I've been Googling in circles......



I don't know what I'm missing. HELP!










share|improve this question













I didn't realize my initial post (which I deleted) was posting under a newbie account - apologies......



In any event, here's my issue:



I have a main form that lists various "Consultants." This main form also has parameters "Consultant Type" "Start Date" and "End Date" that can be used to filter the list.



What's needed is a Report, listing "Consultants" of a certain type. Bear in mind that this "type" filter can apply to any of 4 different type fields ("type1", "type2", "type3", "type4").



What I want to do is place the button that runs the report onto the main form, because the "Consultant Type" parameter is already there. I then want to generate the SQL based off that parameter (and a date parameter that I've already done), and use that recordset to populate the report.



The SQL works fine, but, the resulting data isn't being passed to the report, so I'm getting all records instead of just the ones I want. The recordsource of the report object is set to the background table in order to populate fields/textboxes.



Here's the code



On the Main Form



Private Sub Button_Click()
Dim s As String
If IsNull(Me.Type.Value) Then
MsgBox "Please Select a Type", vbCritical, "Report Error"
Else
s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"
DoCmd.OpenReport "RptList", acViewPreview, , , acDialog
End If


End Sub



Now, obviously, it all goes to pot when I open the report, because there's nothing in the DoCmd that says "Using this recordset." And that's my problem.



I'm trying to avoid creating a separate little dropdown form for the purpose of populating a piece of data that's right in front of me. Also, plugging the SQL directly into the report's recordsource using Forms![MainForm]![Type].Value for the "Type" variables didn't work. I suspect it might have something to do with the form not being active when the report button is clicked?



I thought QueryDef might work. But, I'm not grasping how to implement it, in order to know IF it will work. And I've been Googling in circles......



I don't know what I'm missing. HELP!







ms-access access-vba ms-access-reports






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 20:11









JuniperSquared

807




807








  • 2




    Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
    – C Perkins
    Nov 20 at 20:46










  • I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
    – JuniperSquared
    Nov 21 at 15:18
















  • 2




    Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
    – C Perkins
    Nov 20 at 20:46










  • I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
    – JuniperSquared
    Nov 21 at 15:18










2




2




Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
– C Perkins
Nov 20 at 20:46




Look at the DoCmd.OpenReport online docs. There is a parameter for specifying a WHERE clause (without the "WHERE"). It takes a string value and you can just pass the same condition as you show in your code.
– C Perkins
Nov 20 at 20:46












I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
– JuniperSquared
Nov 21 at 15:18






I tried the Where condition. It didn't work. Where clause will look for 1 criteria, not the entire sql statement. In my attempted I tried Me.Recordsource = s. Blank report.
– JuniperSquared
Nov 21 at 15:18














1 Answer
1






active

oldest

votes


















0














AS C Perkins Suggested use a wherecondition in docmd.Openreport



Something like



    DoCmd.OpenReport "RptList", acViewPreview, , , acDialog, wherecondition:="consultant.typeone = "  & Me.Type.Value


Another way is you can create the object using Query def and used this to change your query at run time based on the user selected criteria.



try like this



save Report_Query in MsAccess and use this query as a recordsource for your report



Dim s as string

s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"

CurrentDb.QueryDefs("Report_Query").sql = s

DoCmd.OpenReport "RptList", acViewPreview, , , acDialog





share|improve this answer





















  • THIS is what I was looking for! Your 2nd one. I'll try that now!
    – JuniperSquared
    Nov 21 at 15:19










  • YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
    – JuniperSquared
    Nov 21 at 15:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400803%2fms-access-report-modify-recordsource-with-vba-using-parameter-from-main-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














AS C Perkins Suggested use a wherecondition in docmd.Openreport



Something like



    DoCmd.OpenReport "RptList", acViewPreview, , , acDialog, wherecondition:="consultant.typeone = "  & Me.Type.Value


Another way is you can create the object using Query def and used this to change your query at run time based on the user selected criteria.



try like this



save Report_Query in MsAccess and use this query as a recordsource for your report



Dim s as string

s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"

CurrentDb.QueryDefs("Report_Query").sql = s

DoCmd.OpenReport "RptList", acViewPreview, , , acDialog





share|improve this answer





















  • THIS is what I was looking for! Your 2nd one. I'll try that now!
    – JuniperSquared
    Nov 21 at 15:19










  • YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
    – JuniperSquared
    Nov 21 at 15:25
















0














AS C Perkins Suggested use a wherecondition in docmd.Openreport



Something like



    DoCmd.OpenReport "RptList", acViewPreview, , , acDialog, wherecondition:="consultant.typeone = "  & Me.Type.Value


Another way is you can create the object using Query def and used this to change your query at run time based on the user selected criteria.



try like this



save Report_Query in MsAccess and use this query as a recordsource for your report



Dim s as string

s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"

CurrentDb.QueryDefs("Report_Query").sql = s

DoCmd.OpenReport "RptList", acViewPreview, , , acDialog





share|improve this answer





















  • THIS is what I was looking for! Your 2nd one. I'll try that now!
    – JuniperSquared
    Nov 21 at 15:19










  • YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
    – JuniperSquared
    Nov 21 at 15:25














0












0








0






AS C Perkins Suggested use a wherecondition in docmd.Openreport



Something like



    DoCmd.OpenReport "RptList", acViewPreview, , , acDialog, wherecondition:="consultant.typeone = "  & Me.Type.Value


Another way is you can create the object using Query def and used this to change your query at run time based on the user selected criteria.



try like this



save Report_Query in MsAccess and use this query as a recordsource for your report



Dim s as string

s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"

CurrentDb.QueryDefs("Report_Query").sql = s

DoCmd.OpenReport "RptList", acViewPreview, , , acDialog





share|improve this answer












AS C Perkins Suggested use a wherecondition in docmd.Openreport



Something like



    DoCmd.OpenReport "RptList", acViewPreview, , , acDialog, wherecondition:="consultant.typeone = "  & Me.Type.Value


Another way is you can create the object using Query def and used this to change your query at run time based on the user selected criteria.



try like this



save Report_Query in MsAccess and use this query as a recordsource for your report



Dim s as string

s = "Select Consultant.* from Consultant "
s = s & "Where ((consultant.typeone = '" & Me.Type.Value & "') OR (Consultant.typetwo = '" & Me.Type.Value & "') OR (Consultant.typethree = '" & Me.Type.Value & "') OR (Consultant.typefour = '" & Me.Type.Value & "'))"
s = s & "And (((Consultant.[End date]) <= DateAdd(""d"", -730, Now()))) ORDER BY Consultant.Last_Name"

CurrentDb.QueryDefs("Report_Query").sql = s

DoCmd.OpenReport "RptList", acViewPreview, , , acDialog






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 7:42









Tarun. P

192210




192210












  • THIS is what I was looking for! Your 2nd one. I'll try that now!
    – JuniperSquared
    Nov 21 at 15:19










  • YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
    – JuniperSquared
    Nov 21 at 15:25


















  • THIS is what I was looking for! Your 2nd one. I'll try that now!
    – JuniperSquared
    Nov 21 at 15:19










  • YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
    – JuniperSquared
    Nov 21 at 15:25
















THIS is what I was looking for! Your 2nd one. I'll try that now!
– JuniperSquared
Nov 21 at 15:19




THIS is what I was looking for! Your 2nd one. I'll try that now!
– JuniperSquared
Nov 21 at 15:19












YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
– JuniperSquared
Nov 21 at 15:25




YES! QueryDefs was what I couldn't wrap my mind around! The Where condition wouldn't have worked, given the reasons in my comment, and, also, note that in my original post "Type" is an OR of 4 different fields. That, and the date code made the Where clause unfeasible. I needed the report to open the entire recordset. Thank You! QueryDefs was what I was looking for, and I just couldn't figure out how to implement it!
– JuniperSquared
Nov 21 at 15:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400803%2fms-access-report-modify-recordsource-with-vba-using-parameter-from-main-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python