Select query based on another table

davidb88

Board Regular
Joined
Sep 23, 2013
Messages
76
Hi -

I am trying to insert a select query into my database where the date of the records in my tbl1 are equal to the date stored in tblmodeldate. Usually I know you would do an inner join on the two fields and it would work properly. The only issue with that is that I am not only looking to bring in the records for which the date matches the date stored in tblmodeldate but also for records from the prior month. Therefore I don't think an inner join would work, correct? Is there a way to do a select query that has criteria that is based on another table without using a join so I can have two criteria? Any suggestions would be appreciated!

Thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Just as a quick follow-up I did a couple other searches and found a good approach to accomplish this would may be through assigning the current month and the previous months as variables. I have come up with the following code, but still cannot get it to work properly. I have verified that the variables are getting assigned properly through using the debug.print function, but still nothing is coming through on the table that it is creating. When I take out the date criteria, the table gets populated so I know that the date criteria is the issue with this set of code, just can't seem to figure out what about it is incorrect. Any suggestions?
Code:
Sub CalculatePD()
Dim modeldate As Date
Dim priormodeldate As Date
Dim strSQL As String
DoCmd.setwarnings False


'Assign modeldate variable to the stored value in tblmodeldate
modeldate = DLookup("modeldate", "tblmodeldate")
'Assign priormodeldate to the month prior to stored value in tblmodeldate
priormodeldate = DateSerial(Year(modeldate), Month(modeldate), 0)


strSQL = "SELECT tblLHFIedit.Month, tblLHFIedit.[Loan Type], tblLHFIedit.[FICO Band] INTO tbl1stfico1 " _
        & "FROM tblLHFIedit " _
        & "WHERE (((tblLHFIedit.[Month])= " & modeldate & ") OR ((tblLHFIedit.[Month])= " & priormodeldate & ") " _
        & "AND ((tblLHFIedit.[FICO Band])='1') AND ((tblLHFIedit.[Loan Type])='1st Mtg'));"
DoCmd.RunSQL (strSQL)
 
Upvote 0
I answered my own question. I was missing #'s. For anyone in the future that is experiencing a similar issue, this is the final code that works for me:
Code:
Sub CalculatePD()
Dim modeldate As Date
Dim priormodeldate As Date
Dim strSQL As String
DoCmd.setwarnings False


'Assign modeldate variable to the stored value in tblmodeldate
modeldate = DLookup("modeldate", "tblmodeldate")
'Assign priormodeldate to the month prior to stored value in tblmodeldate
priormodeldate = DateSerial(Year(modeldate), Month(modeldate), 0)


strSQL = "SELECT tblLHFIedit.Month, tblLHFIedit.[Loan Type], tblLHFIedit.[FICO Band] INTO tbl1stfico1 " _
        & "FROM tblLHFIedit " _
        & "WHERE (((tblLHFIedit.[Month])= #" & modeldate & "#) OR ((tblLHFIedit.[Month])= #" & priormodeldate & "#) " _
        & "AND ((tblLHFIedit.[FICO Band])='1') AND ((tblLHFIedit.[Loan Type])='1st Mtg'));"
DoCmd.RunSQL (strSQL)
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,092
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top