Code:
Dim TblName As Long
Dim FMonth As Long
Dim FYear As Long
Dim FSort As Long
Dim TMonth As Long
Dim TYear As Long
Dim TSort As Long
'delete after the form is made with a date picker
FYear = 2010
FMonth = 3
TYear = 2010
TMonth = 9
TblName = 2
'delete after the form is made with a date picker
Dim TrendSQLSelect As String
Dim TrendSQLJoins As String
Dim TrendSQL As String
TrendSQLSelect = ""
TrendSQLJoins = ""
TrendSQL = ""
If TblName = 2 Then 'market
Dim QryToLoop As String
Dim LMonth As Long
Dim LYear As Long
Dim db As Database
Dim Rst As Recordset
QryToLoop = "SELECT CmbMonthsYears.Dates FROM CmbMonthsYears WHERE CmbMonthsYears.Dates>#" & FMonth & "/1/" & FYear & _
"# AND CmbMonthsYears.Dates<#" & TMonth & "/1/" & TYear & "#"
Set db = CurrentDb
Set Rst = db.OpenRecordset(QryToLoop)
Do Until Rst.EOF
LMonth = Left(Rst("Dates"), InStr(Rst("Dates"), "/1/") - 1)
LYear = Right(Rst("Dates"), 4)
'MsgBox LMonth & " - " & LYear
Dim dbs As Database
Dim qryDef As QueryDef
Dim QryMonth As String
Dim QryName As String
On Error Resume Next
Set dbs = CurrentDb()
QryName = LMonth & LYear
If QueryExists(LMonth & LYear) Then
dbs.QueryDefs.Delete QryName
End If
QryMonth = "SELECT MONTHLY.State, MONTHLY.Mkt, MONTHLY.Acute, MONTHLY.Ancil, MONTHLY.PCP, MONTHLY.Spec, MONTHLY.TotalPrac, MONTHLY.DAcuteI, " & _
"MONTHLY.DAcuteO, MONTHLY.DAncil, MONTHLY.DPrac FROM MONTHLY WHERE MONTHLY.CMonth=" & LMonth & " AND MONTHLY.CYear=" & LYear & " " & _
"AND MONTHLY.TableName=" & TblName & " ORDER BY MONTHLY.SortID, MONTHLY.State, MONTHLY.Mkt"
Set qryDef = dbs.CreateQueryDef(QryName, QryMonth)
If IsNull(TrendSQL) Or TrendSQL = "" Then
TrendSQLSelect = ", [" & QryName & "].Acute"
TrendSQLJoins = "LEFT JOIN " & QryName & " ON (AllMkts.Mkt = [" & QryName & "].Mkt) AND (AllMkts.State = [" & QryName & "].State) "
Else
TrendSQLSelect = TrendSQLSelect & ", [" & QryName & "].Acute"
TrendSQLJoins = TrendSQLJoins & " LEFT JOIN " & QryName & " ON (AllMkts.Mkt = [" & QryName & "].Mkt) AND (AllMkts.State = [" & QryName & "].State) "
End If
TrendSQL = "SELECT AllMkts.State, AllMkts.Mkt" & TrendSQLSelect & " FROM AllMkts " & TrendSQLJoins & "ORDER BY AllMkts.ID, AllMkts.State, AllMkts.Mkt;"
Rst.MoveNext
Loop
Dim db11 As Database
Dim qryDef11 As QueryDef
Set db11 = CurrentDb
If QueryExists("QTrends") Then
db11.QueryDefs.Delete "QTrends"
End If
[B][SIZE="5"]Set qryDef11 = db11.CreateQueryDef("QTrends", TrendSQL)[/SIZE][/B]
[SIZE="5"]msgbox trendsql[/SIZE]
ElseIf TblName = 4 Then 'MSA
'will write later
ElseIf TblName = 8 Then 'State
'will write later
End If
everything works fine except the large line
it's just not creating the query. everything runs perfect, i tested it with the MSGBOX to see the sql and it looks fine to me but the query doesn't get created
if i put the query-create-block inside the loop - it creates it, but only with the first go round of values
what am i doing wrong?
thank you