Convert complicated SQL codes together to VBA to be used as record source for a report

I am using two queries like:

SELECT tblCategoriesAndSubCategories.ElevatorReportSortOrder, 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]![cboSelectJob2]) AND ((tblCategoryInfo.ElevatorReport)=True)) OR (((tblCategoryInfo.Description) Is Null) AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob2]) AND ((tblCategoryInfo.ElevatorReport)=True))
ORDER BY tblCategoriesAndSubCategories.ElevatorReportSortOrder;

Open in new window


And

SELECT tblCategoriesAndSubCategories.ElevatorReportSortOrder, 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]![cboSelectJob3]) AND ((tblCategoryInfo.ElevatorReport)=True)) OR (((tblCategoryInfo.Description) Is Null) AND ((tblCategoryInfo.JobNo)=[Forms]![frmReportSelector]![cboSelectJob3]) AND ((tblCategoryInfo.ElevatorReport)=True))
ORDER BY tblCategoriesAndSubCategories.ElevatorReportSortOrder;

Open in new window


and then am combining them together in a final query with:

SELECT qryElevatorReportDetailColumn1.ElevatorReportSortOrder AS SortOrder, qryElevatorReportDetailColumn1.SubCategory AS Job1SubCategory, qryElevatorReportDetailColumn1.Description AS Job1Description, qryElevatorReportDetailColumn1.JobNo AS Job1JobN, qryElevatorReportDetailColumn2.SubCategory AS Job2SubCategory, qryElevatorReportDetailColumn2.Description AS Job2Description, qryElevatorReportDetailColumn2.JobNo AS Job2JobN
FROM qryElevatorReportDetailColumn1 INNER JOIN qryElevatorReportDetailColumn2 ON qryElevatorReportDetailColumn1.SubCategory = qryElevatorReportDetailColumn2.SubCategory;

Open in new window


How can I convert all of this to VBA as the record source for the report?
SteveL13Asked:
Who is Participating?
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.

aikimarkCommented:
paste the combined query into the recordsource for the report
0
SteveL13Author Commented:
I probably didn't explain this well.  I want to convert all 3 SQL queries to VBA code.  If you refer to another topic I posted earlier today, How convert SQL code to VBA code, you will see how another expert helped me out.
0
aikimarkCommented:
These two languages solve two very different problems.  I don't see how or why you need a conversion.  I'll watch what other experts post.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PatHartmanCommented:
How can I convert all of this to VBA as the record source for the report?
The RecordSource for a form or report is NOT VBA.  It is a Table name, a querydef name, or an SQL String

Steve,  if the third query works when you run it with the QBE (open the QBE, close the dialog, switch to SQL View, paste the SQL string and run it), then just paste the string into the Form or Report's RecordSource.

However, saved querydefs (follow the steps above, save the query and give it a menaingful name) are slightly more efficient.  In that case, you would paste the query name into the RecordSource rather than the SQL string.

PS - you shouldn't have two questions on the same topic open and if you want people to look at a different question, please post a link.
0
[ fanpages ]IT Services ConsultantCommented:
0
PortletPaulfreelancerCommented:
Perhaps this will lead you to a solution where you don't need to come back here for each query:
http://rickincanada.blogspot.com.au/2012/08/convert-sql-to-vba.html

Also some on-line format tools provide formats for things like C#, this may make the job easier without much further effort. e.g.
http://www.dpriver.com/pp/sqlformat.htm
0
mbizupCommented:
From your previous post, it sounds like your concern is that you need to be able to add more queries, preferably easily.

An alternative to rewriting them all as SQL strings would be to keep each of the sub-queries as stored queries, and since you are selecting all fields, you can simplify your combined query like this, which might make it more manageable for you in adding more queries:

SELECT Query1.*, Query2.*
FROM Query1 LEFT JOIN Query2 ON Query1.SomeField = Query2.SomeField

Open in new window


Using VBA as the others have noted seems to complicate things without any real gain.  However if you are adamant about using VBA, keep your subqueries as-is, stored, and use VBA in your report's Open Event to combine them.  As noted below, you will run into complications with your form references:

Dim s as string
'Add to this as your database grows.
s= "SELECT Query1.*, Query2.* " _
   & "FROM (Query1 LEFT JOIN Query2 ON Query1.SomeField = Query2.SomeField)"
Me.Recordsource = s

Open in new window


With this method, the form references that you are using as criteria will probably cause a "Too Few Parameters" error, in which case you would have to use some other method of supplying the criteria.  TempVars are an option as a replacement for form references.  As an example, in your SQL, replace[Forms]![frmReportSelector]![cboSelectJob3] with:

TempVars!tvJob3

Open in new window


And in the click event of the button that runs the report:

Dim s as string
s = Me.cboSelectJob3
TempVars.add "tvJob3", s

Open in new window


(And do this for all form references in your queries
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.