Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then else problem

Status
Not open for further replies.

keepfoxing

Programmer
Dec 9, 2015
37
PH
this is my code to print list if conditions are met and my problem is, it always give me the message: "There are no enrollees for this year level1".
i knew how to construct if then else if there are few conditions but this one makes me confused.
can anybody help me reconstruct my if then else code to a better approach..
tnx in advance..
Code:
SELECT stud_table

IF EMPTY(THISFORM.cmb_yr.DisplayValue) OR EMPTY(THISFORM.cmb_major.DisplayValue)
	MESSAGEBOX("Please fill the fields required.",64,"Info")
	THISFORM.cmb_yr.SetFocus
	RETURN 
ELSE 
	LOCATE FOR major=ALLTRIM(thisform.cmb_major.DisplayValue) AND yr=ALLTRIM(thisform.cmb_yr.DisplayValue)
	IF FOUND()
		...PREVIEW FOR major=ALLTRIM(thisform.cmb_major.DisplayValue) AND yr=ALLTRIM(thisform.cmb_yr.DisplayValue)
ELSE 
MESSAGEBOX("There are no enrollees for this year level1",0,"No records found")
RETURN 
ENDIF
ENDIF  



*************************************************************
 IF thisform.cmb_yr.DisplayValue="ALL YEAR LEVEL" AND thisform.cmb_major.DisplayValue="ALL MAJORS" THEN 
		REPORT FORM LOCFILE("C:\System\Reports\masterlist.frx") TO PRINT PROMPT PREVIEW IN WINDOW wPreview
ELSE 
IF EMPTY(stud_table.id_no)
MESSAGEBOX("There are no enrollees yet.",0,"No records found")
RETURN 
ENDIF
ENDIF 
********************************************************
IF thisform.cmb_yr.DisplayValue="ALL YEAR LEVEL" AND thisform.cmb_major.DisplayValue <> "ALL MAJORS" THEN 
LOCATE FOR major=ALLTRIM(thisform.cmb_major.DisplayValue)
IF FOUND()

		REPORT FORM LOCFILE("C:\System\Reports\masterlist.frx") TO PRINT PROMPT PREVIEW FOR major=ALLTRIM(thisform.cmb_major.DisplayValue) IN WINDOW wPreview
 
ELSE 
IF !FOUND()
MESSAGEBOX("There are no enrollees for this year level2",0,"No records found")
RETURN 
ENDIF 
ENDIF
ENDIF 
********************************************************
IF thisform.cmb_major.DisplayValue="ALL MAJORS" AND thisform.cmb_yr.DisplayValue != "ALL YEAR LEVEL" THEN 
LOCATE FOR yr=ALLTRIM(thisform.cmb_yr.DisplayValue)
IF FOUND()

		REPORT FORM LOCFILE("C:\System\Reports\masterlist.frx") TO PRINT PROMPT PREVIEW FOR yr=ALLTRIM(thisform.cmb_yr.DisplayValue) IN WINDOW wPreview
 		
ELSE 
MESSAGEBOX("There are no enrollees for this year level3",0,"No records found")
RETURN 
ENDIF
ENDIF 
**************
 
You might use rightclick->Beautify in the code editor to get better indented code.

But your problem rather is about data (of this year) than the code structure. Just remember, the year is very young, if you don't yet have 2016 data for a sepcific "major" value, you don't have 2016 data for a specific "major" value. Select 2015 in the cmb_yr combobox an it would most probably find something.

Bye, Olaf.
 
One problematic thing can be seen right away without beautifying the code, you test for "ALL MAJORS" and "ALL YEAR LEVEL" last, so your first code already displayed some messages. If you want this to have precedence over other choices, of course this has to be checked first and all the rest of code either only comes in an ELSE branch or you RETURN from the first IF, so ohter code isn't executed any more.

Bye, Olaf.
 
Another problem might be trailing spaces. For example, in this code:

[tt]LOCATE FOR major=ALLTRIM(thisform.cmb_major.DisplayValue) ...[/tt]

you are correctly trimming the value from the combo box. But if Major is the name of a field, that would also need to be trimmed. That's because, if the value of a (character) field is shorter than the field itself, it will be filled with spaces.

For good measure, you should also allow for case sensitivity. In other words, modify the above code as follows:

[tt]LOCATE FOR ALLTRIM(UPPER(major)) = ALLTRIM(UPPER(thisform.cmb_major.DisplayValue))[/tt]

Similarly with the other tests within the code.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
i have modified it and i got an error on the last else of the code.
Code:
Select stud_table

If Empty(Thisform.cmb_yr.DisplayValue) Or Empty(Thisform.cmb_major.DisplayValue)
	Messagebox("Please fill the fields required.",64,"Info")
	Thisform.cmb_yr.SetFocus
	Return
Else
	Locate For major=Alltrim(Thisform.cmb_major.DisplayValue) And yr=Alltrim(Thisform.cmb_yr.DisplayValue)
	If Found()
		
		...Preview For major=Alltrim(Thisform.cmb_major.DisplayValue) And yr=Alltrim(Thisform.cmb_yr.DisplayValue)...
	Else
*************************************************************
		If Thisform.cmb_yr.DisplayValue="ALL YEAR LEVEL" And Thisform.cmb_major.DisplayValue="ALL MAJORS" Then
			
			Report Form Locfile("C:\System\Reports\masterlist.frx") To Print Prompt Preview In Window wPreview
		Else
********************************************************
			If Thisform.cmb_yr.DisplayValue="ALL YEAR LEVEL" And Thisform.cmb_major.DisplayValue <> "ALL MAJORS" Then
				Locate For major=Alltrim(Thisform.cmb_major.DisplayValue)
				If Found()
					
					...Preview For major=Alltrim(Thisform.cmb_major.DisplayValue)...
				Else
********************************************************
					If Thisform.cmb_major.DisplayValue="ALL MAJORS" And Thisform.cmb_yr.DisplayValue != "ALL YEAR LEVEL" Then
						Locate For yr=Alltrim(Thisform.cmb_yr.DisplayValue)
						If Found()
							
							...Preview For yr=Alltrim(Thisform.cmb_yr.DisplayValue)...	
						Else
							Messagebox("There are no enrollees for this year level3",0,"No records found")
							Return
						Endif
					Else
						Messagebox("There are no enrollees for this year level3",0,"No records found")
						Return
					Endif
				Endif
			Else
				Messagebox("There are no enrollees for this year level3",0,"No records found")
				Return
			Endif
			ENDIF 
	Else
		Messagebox("There are no enrollees for this year level3",0,"No records found")
		Return
	Endif
ENDIF 
**************
 
I'm guessing the error message is because you have two ENDIF before the last ELSE.

I'm guessing because you DID NOT SAY WHICH ERROR MESSAGE you got. That's VERY important information when you're asking about an error you're getting. Remember: we can't see through your eyes.
 
im sprry here is
the error message: "an if | else | endif statement is missing" and last else is highlighted.

even if i delete the endif on the above it still got the error..
 
im sprry here is
the error message: "an if | else | endif statement is missing" and last else is highlighted.

even if i delete the endif on the above it still got the error..
 
In the above code you have 6 IF statements and 6 ENDIFs, so the count is correct and they look like they are nested correctly, so I would look for something else.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
KeepFoxing, You have one too many 'else' (3rd, 8th) in the code.

You could replace first three 'else' with

return
endif

and your code will be more manageable for if/else/endif structure.

Code:
Select stud_table
If Empty(Thisform.cmb_yr.DisplayValue) Or Empty(Thisform.cmb_major.DisplayValue)
   Messagebox("Please fill the fields required.",64,"Info")
   Thisform.cmb_yr.SetFocus
   Return
endif

Locate For major=Alltrim(Thisform.cmb_major.DisplayValue) And yr=Alltrim(Thisform.cmb_yr.DisplayValue)
If Found()
   ...Preview For major=Alltrim(Thisform.cmb_major.DisplayValue) And yr=Alltrim(Thisform.cmb_yr.DisplayValue)...
   return
endif

*************************************************************
If Thisform.cmb_yr.DisplayValue="ALL YEAR LEVEL" And Thisform.cmb_major.DisplayValue="ALL MAJORS" Then
   Report Form Locfile("C:\System\Reports\masterlist.frx") To Print Prompt Preview In Window wPreview
   return
endif

Adjust and allign the balance of the code.
 
that worked.
but if the user choose to any yr and all majors on the 2 combo box, it says there is no record but actually it has..
that is the only instance that im getting into wrong..
 
Keepfoxing, simply set a breakpoint and see what happens, if you choose any year and all majors.
You'll see what happens and what you have to test first.

If you check the speecial cases last, you first get no FOUND() and report no record found, of course. All special cases have to be chekced first.
As you check major=Alltrim(Thisform.cmb_major.DisplayValue) And yr=Alltrim(Thisform.cmb_yr.DisplayValue) first, what do you think happens when you make some special value choice that actually does not exist in data? It's not found...

Also, in such a case of many IFs, think about using a CASE statement.

Bye, Olaf.

 
Hello,

Maybe use VALUE property instead of DISPLAYVALUE?

From the help file:

Visual FoxPro 9.0 SP2
DisplayValue Property

Specifies the contents of the first column of the selected item in a ListBox or ComboBox control. Available at design time and run time.

Use the DisplayValue property when a combo box or list box has more than one column, and the control's BoundColumn property is set to a value greater than 1.

and

Visual FoxPro 9.0 SP2
Value Property

Specifies the current state of a control. Available at design time and run time.

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top