**************************************************
*-- Class: buildfilter (d:\tipsbook\ch11\ch11.vcx)
*-- ParentClass: form
*-- BaseClass: form
*-- Time Stamp: 04/09/04 11:59:03 AM
*
DEFINE CLASS buildfilter AS form
DataSession = 2
Height = 242
Width = 630
DoCreate = .T.
BufferMode = 2
AutoCenter = .T.
Caption = "Build Filter Condition"
Closable = .F.
WindowType = 1
AlwaysOnTop = .T.
*-- Alias used to build the filter condition(s)
calias = ""
*-- Holds the filter condition as it is being built
cfilter = ""
Name = "frmBuildFilter"
*-- Array to hold the field captions if the alias is part of a dbc and has captions set . Otherwise, holds the field names
DIMENSION afieldnames[1,1]
ADD OBJECT cbofieldnames AS combobox WITH ;
BoundColumn = 2, ;
RowSourceType = 5, ;
RowSource = "Thisform.aFieldNames", ;
Height = 24, ;
Left = 7, ;
TabIndex = 1, ;
Top = 26, ;
Width = 257, ;
Name = "cboFieldNames"
ADD OBJECT cboconditions AS combobox WITH ;
RowSourceType = 1, ;
RowSource = "", ;
Height = 24, ;
Left = 268, ;
TabIndex = 2, ;
Top = 26, ;
Width = 100, ;
Name = "cboConditions"
ADD OBJECT label1 AS label WITH ;
FontBold = .T., ;
Caption = "Field", ;
Height = 17, ;
Left = 9, ;
Top = 51, ;
Width = 60, ;
TabIndex = 8, ;
Name = "Label1"
ADD OBJECT label2 AS label WITH ;
FontBold = .T., ;
Caption = "Condition", ;
Height = 17, ;
Left = 270, ;
Top = 51, ;
Width = 72, ;
TabIndex = 9, ;
Name = "Label2"
ADD OBJECT label3 AS label WITH ;
FontBold = .T., ;
Caption = "Value", ;
Height = 17, ;
Left = 375, ;
Top = 50, ;
Width = 40, ;
TabIndex = 10, ;
Name = "Label3"
ADD OBJECT cmdok AS commandbutton WITH ;
Top = 209, ;
Left = 317, ;
Height = 27, ;
Width = 84, ;
Caption = "OK", ;
TabIndex = 6, ;
Name = "cmdOK"
ADD OBJECT cmdcancel AS commandbutton WITH ;
Top = 209, ;
Left = 406, ;
Height = 27, ;
Width = 84, ;
Cancel = .T., ;
Caption = "Cancel", ;
TabIndex = 7, ;
Name = "CmdCancel"
ADD OBJECT cmdclearall AS commandbutton WITH ;
Top = 209, ;
Left = 228, ;
Height = 27, ;
Width = 84, ;
Caption = "\<Clear All", ;
TabIndex = 5, ;
Name = "cmdClearAll"
ADD OBJECT cmdaddcondition AS commandbutton WITH ;
Top = 209, ;
Left = 139, ;
Height = 27, ;
Width = 84, ;
Caption = "\<Add Condition", ;
TabIndex = 4, ;
Name = "cmdAddCondition"
ADD OBJECT edtfilter AS editbox WITH ;
Height = 84, ;
Left = 7, ;
ReadOnly = .T., ;
TabIndex = 11, ;
Top = 84, ;
Width = 619, ;
ControlSource = "Thisform.cFilter", ;
Name = "edtFilter"
ADD OBJECT label4 AS label WITH ;
FontBold = .T., ;
Caption = "Filter Condition", ;
Height = 17, ;
Left = 9, ;
Top = 169, ;
Width = 90, ;
TabIndex = 12, ;
Name = "Label4"
ADD OBJECT txtvalues AS textbox WITH ;
Format = "!", ;
Height = 23, ;
Left = 376, ;
TabIndex = 3, ;
Top = 26, ;
Width = 250, ;
Name = "txtValues"
*-- Builds the filter condition
PROCEDURE buildfilter
LOCAL lcCondition
WITH Thisform
IF TYPE(.cAlias+'.'+ALLTRIM(.cboFieldNames.Value)) == 'C'
lcCondition = 'UPPER('+ALLTRIM(.cboFieldNames.Value) + ') ' + ALLTRIM(Thisform.cboConditions.Value) + ' '
ELSE
lcCondition = ALLTRIM(.cboFieldNames.Value) + ' ' + ALLTRIM(Thisform.cboConditions.Value) + ' '
ENDIF
*** Add the quotation marks if the field type is character
IF TYPE(.cAlias+'.'+ALLTRIM(.cboFieldNames.Value)) == 'C'
lcCondition = lcCondition + CHR(34) + UPPER(ALLTRIM(.txtValueS.Value)) + CHR(34)
ELSE
lcCondition = lcCondition + ALLTRIM(.txtValues.Value)
ENDIF
*** If there are multiple conditions and them together
.cFilter = IIF(EMPTY(.cFilter), lcCondition, .cFilter + ' AND ' + lcCondition)
ENDWITH
Thisform.edtFilter.Refresh()
ENDPROC
*-- Create array of field names (or, hopefully, captions) to use in dropdown list
PROCEDURE setform
LOCAL lnFieldCnt, laFields[1], lnCnt, lcCaption, lnArrayLen
WITH Thisform
*** Make sure alias is available
IF !USED( .cAlias )
USE ( .cAlias ) IN 0
ENDIF
*** Get all the field names in the passed alias
lnFieldCnt = AFIELDS( laFields, .cAlias )
*** Don't include memo fields in the field list
lnArrayLen = lnFieldCnt
lnCnt = 1
DO WHILE lnCnt <= lnArrayLen
IF lnCnt > lnFieldCnt
EXIT
ENDIF
IF TYPE( .cAlias + "." + laFields[ lnCnt, 1 ] ) = "M"
=ADEL( laFields,lnCnt )
lnFieldCnt = lnFieldCnt - 1
ELSE
lnCnt = lnCnt + 1
ENDIF
ENDDO
*** Create a two-dimensional array of captions (if available) and field names
DIMENSION .aFieldNames[ lnFieldCnt,2 ]
FOR lnCnt = 1 TO lnFieldCnt
lcCaption = ""
IF !EMPTY( DBC() ) AND ( INDBC( .cAlias, 'TABLE' ) OR INDBC( .cAlias, 'VIEW' ) )
lcCaption = PADR( DBGetProp( .cAlias + "." + laFields[ lnCnt, 1 ], 'FIELD', 'CAPTION' ), 40 )
ENDIF
IF EMPTY( lcCaption )
lcCaption = PADR( laFields[ lnCnt, 1 ], 40 )
ENDIF
.aFieldNames[ lnCnt, 1 ] = lcCaption
.aFieldNames[ lnCnt, 2 ] = PADR( laFields[ lnCnt, 1 ], 40 )
ENDFOR
.cboFieldNames.Requery()
.cboFieldNames.ListIndex = 1
ENDWITH
ENDPROC
PROCEDURE Init
LPARAMETERS tcALias
WITH ThisForm
*** Save alias so it can be used by the entire form
.cAlias = tcAlias
.SetForm()
ENDWITH
ENDPROC
PROCEDURE Unload
RETURN ThisForm.cFilter
ENDPROC
PROCEDURE Load
DoSets()
ENDPROC
PROCEDURE cboconditions.Init
WITH This
.AddItem( " = " )
.AddItem( " > " )
.AddItem( " < " )
.AddItem( " # " )
ENDWITH
ENDPROC
PROCEDURE cmdok.Click
Thisform.Release()
ENDPROC
PROCEDURE cmdcancel.Click
Thisform.cFilter = ""
Thisform.Release()
ENDPROC
PROCEDURE cmdclearall.Click
Thisform.cFilter = ""
Thisform.EdtFilter.Refresh()
ENDPROC
PROCEDURE cmdaddcondition.Click
Thisform.BuildFilter()
ENDPROC
ENDDEFINE
*
*-- EndDefine: buildfilter
**************************************************