How convert SQL code to VBA code

I have the following SQL code in query designer.  The query becomes the record source for a report.  But I'd like to convert it to VBA code and use that VBA code as the record source for the report and don't know where to begin.



SELECT tblCategoriesAndSubCategories.OwnerReportSortOrder, tblCategoryInfo.Category, tblCategoryInfo.SubCategory, tblCategoryInfo.Description, tblCategoryInfo.JobNo
FROM tblCategoryInfo INNER JOIN tblCategoriesAndSubCategories ON (tblCategoryInfo.Category = tblCategoriesAndSubCategories.Category) AND (tblCategoryInfo.SubCategory = tblCategoriesAndSubCategories.SubCategory)
WHERE (((tblCategoryInfo.Description) Not Like "NA") AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True)) OR (((tblCategoryInfo.Description) Is Null) AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True))
ORDER BY tblCategoriesAndSubCategories.OwnerReportSortOrder;

Open in new window

SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
strSQL = "SELECT tblCategoriesAndSubCategories.OwnerReportSortOrder, tblCategoryInfo.Category, tblCategoryInfo.SubCategory, tblCategoryInfo.Description, tblCategoryInfo.JobNo "
strSQL = strSQL & "FROM tblCategoryInfo INNER JOIN tblCategoriesAndSubCategories ON (tblCategoryInfo.Category = tblCategoriesAndSubCategories.Category) AND (tblCategoryInfo.SubCategory = tblCategoriesAndSubCategories.SubCategory) "
strSQL = strSQL & "WHERE (((tblCategoryInfo.Description) Not Like """NA""") AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True)) OR (((tblCategoryInfo.Description) Is Null) AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True))"
strSQL = strSQL & "ORDER BY tblCategoriesAndSubCategories.OwnerReportSortOrder;"

Me.RecordSource = strSQL
0
SteveL13Author Commented:
When I copy/paste this in the code window, the 3rd line turns red.
0
Dale FyeOwner, Developing Solutions LLCCommented:
I've gotta ask, why?  What do you hope to achieve by getting rid of the saved query and using VBA for this purpose?
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

SteveL13Author Commented:
The issue is this...   The existing query is currently the record source for just one sub-report.  But on the main report there will be a total if 6 sub-reports which are either visible or not visible depending on a select report on a form that has a command button that opens the main report.  I just thought that the VBA code would be cleaner than having 6 queries.  And for now there are just 6 possibilities.  There may be more before I'm done.  I'd control the record source "selection" via if/then code surrounding the SQL code.  Make sense?
0
MacroShadowCommented:
Replace that line with this:
strSql = strSql & "WHERE (((tblCategoryInfo.Description) Not Like " & """NA""" & ") AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True)) OR (((tblCategoryInfo.Description) Is Null) AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob]) AND ((tblCategoryInfo.OwnerReport)=True)) "
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
MacroShadow...  I just posted another one that is more complicated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.