Private Sub cmdNEXT_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim stDocName As String
Dim stLinkCriteria As String
Dim sql As String
Dim valp As String
Dim qtyp As String
Dim DATEFROM As Date
Dim DATETO As Date
Dim sqlstr As String
Dim inttill As Integer
Dim intarea As Integer
Dim strstore As String
Dim fromplu As Integer
Dim toplu As Integer
Dim grpval As Integer
Dim diffval As String
Dim qtyval As String
Dim sqlrange As String
Dim sql2 As String
Dim sqlselect As String
Dim sqlfrom As String
Dim qdp As DAO.QueryDef
Dim length As Integer
On Error GoTo COMPARISON_ERROR
grpval = Me.frmPERIOD.VALUE
rptval = Me.frmREPORTBY.VALUE
Select Case grpval
Case Is = 1
period = "DAY"
diffval = "d"
Case Is = 2
period = "WEEK"
diffval = "w"
Case Is = 3
period = "MONTH"
diffval = "m"
End Select
Select Case Me.frmREPORTBY.VALUE
Case Is = 1
qtyval = "HTRX_VALUE"
Case Is = 2
qtyval = "HTRX_QTY_1"
Case Else
MsgBox "Please choose a Report by Option"
Exit Sub
End Select
Set db = CurrentDb()
sqlstr = ""
'sets caption on form to period1
p = Right(Forms("frmCOMPARISON").Caption, 1)
' check to see if date from and to entered
' if no date entered exit procedure and focus on date text box
If Nz(Me.txtDATEFROM1, 0) = 0 Then
MsgBox "YOU MUST CHOOSE A START DATE", , "ENTER A DATE"
Me.txtDATEFROM1.SetFocus
Exit Sub
ElseIf Nz(Me.txtDATETO1, 0) = 0 Then
MsgBox "YOU MUST CHOOSE AN END DATE", , "ENTER A DATE"
Me.txtDATETO1.SetFocus
Exit Sub
End If
valp = "PERIOD" & p
DATETO = Me.txtDATETO1
DATEFROM = Me.txtDATEFROM1
'sets the where criteria on the sql command to either store,
'area or till
If Nz(Me!frmAREAFILTERS.Form.comboTILL, 0) <> 0 Then
inttill = [Forms]![frmCOMPARISON]![frmAREAFILTERS].[Form]![comboTILL]
sqlstr = sqlstr & "AND ((HTRXTBL.HTRX_TILL_NUMBER)= " & inttill & ")"
sqlrange = sqlrange & "((TILLTBL.TILL_NUMBER)= " & inttill & ")"
sqlselect = sqlselect & "TILLTBL.TILL_DESC, "
sqlfrom = sqlfrom & "TILLTBL, "
Else
If Nz(Me!frmAREAFILTERS.Form.comboAREA, 0) <> 0 Then
intarea = [Forms]![frmCOMPARISON]![frmAREAFILTERS].[Form]![comboAREA]
sqlstr = sqlstr & "AND ((HTRXTBL.HTRX_AREA_NUMBER)= " & intarea & ")"
sqlrange = sqlrange & "((AREATBL.AREA_NUMBER)= " & intarea & ")"
sqlselect = sqlselect & "AREATBL.AREA_DESC, "
sqlfrom = sqlfrom & "AREATBL, "
Else
If Nz(Me!frmAREAFILTERS.Form.comboSTORE, 0) <> 0 Then
strstore = [Forms]![frmCOMPARISON]![frmAREAFILTERS].[Form]![comboSTORE]
sqlstr = sqlstr & "AND ((HTRXTBL.HTRX_SYSC_NUMBER) LIKE " & strstore & ")"
sqlrange = sqlrange & "((SYSCTBL.SYSC_NUMBER) LIKE " & strstore & ")"
sqlselect = sqlselect & "SYSCTBL.SYSC_COMPANY, "
sqlfrom = sqlfrom & "SYSCTBL, "
End If
End If
End If
length = Len(sqlfrom)
sqlfrom = Left(sqlfrom, (length - 2))
sqlfrom = sqlfrom & " "
If Nz(Me!subfrmPLUFILTERS.Form.txtFROMPLU, 0) <> 0 Then
fromplu = [Forms]![frmCOMPARISON]![subfrmPLUFILTERS].[Form]![txtFROMPLU]
sqlstr = sqlstr & " AND ((HTRXTBL.HTRX_ITEM_NUMBER) >= " & fromplu & ")"
sqlselect = sqlselect & fromplu & " AS FROMPLU, "
End If
If Nz(Me!subfrmPLUFILTERS.Form.txtTOPLU, 0) <> 0 Then
toplu = [Forms]![frmCOMPARISON]![subfrmPLUFILTERS].[Form]![txtTOPLU]
sqlstr = sqlstr & " AND ((HTRXTBL.HTRX_ITEM_NUMBER) <= " & toplu & ")"
sqlselect = sqlselect & toplu & " AS TOPLU, "
End If
'creates the sqlcommand for the relevant period and saves to a query
sql = "SELECT " & _
"(DateDiff('" & diffval & "', " & "CDate('" & DATEFROM & "')" & ",(HTRXTBL.HTRX_TRX_DATE))) AS " & period & " , " & _
"(Round(Sum(HTRXTBL." & qtyval & "),2)) AS " & valp & " " & _
"FROM ITEMTBL, HTRXTBL " & _
"WHERE ITEMTBL.ITEM_NUMBER = HTRXTBL.HTRX_ITEM_NUMBER " & _
"AND (HTRXTBL.HTRX_REC_TYPE= 'ITMSALE') " & _
"AND HTRXTBL.HTRX_TRX_DATE BETWEEN CDate('" & DATEFROM & "') AND CDate('" & DATETO & "')" & _
sqlstr & _
"GROUP BY " & "((DateDiff('" & diffval & "', " & "CDate('" & DATEFROM & "')" & ",(HTRXTBL.HTRX_TRX_DATE))))"
sqlselect = sqlselect & "#" & Format(DATEFROM, "dd/mm/yyyy") & "#" & " AS FROMDATE, " & "#" & Format(DATETO, "dd/mm/yyyy") & "#" & " AS TODATE "
'MsgBox sqlselect
'MsgBox sqlfrom
'MsgBox sqlrange
sql2 = "SELECT " & _
sqlselect & _
"FROM " & _
sqlfrom & _
"WHERE " & _
sqlrange
'MsgBox sql2
Set qdf = db.CreateQueryDef("qryCOMPARISON" & p)
Set qdp = db.CreateQueryDef("qryPERIOD" & p)
'Create qrycomparison
qdf.sql = sql
qdp.sql = sql2
p = Right(Forms("frmCOMPARISON").Caption, 1) + 1
'RENAME CAPTION OF FORM FOR EACH NEW PERIOD
If p <= 9 Then
Forms("frmCOMPARISON").Caption = "SALES COMPARISON PERIOD" & p
Else
MsgBox "You have entered the maximum amount of periods", vbOKOnly
GoTo COMPARISON_EXIT
End If
'Re-open comparison for next period
stDocName = "frmCOMPARISON"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Call reset_filters
COMPARISON_EXIT:
Exit Sub
COMPARISON_ERROR:
MsgBox "Error " & Err.Number & ": " & Err.DESCRIPTION, , "NEXT PERIOD ERROR"
Resume COMPARISON_EXIT
End Sub