473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to shorten codes that using if, else and end if?

180 100+
I want to shorten my codes coz i got a compile error: procedure too large. is there a way that these thing can be done in shortest code?

this are my sample codes: i have a hundred of this codes, i only pasted 12 to show you.

Expand|Select|Wrap|Line Numbers
  1. If Form_List.RecordSource = ("calvelo joseph Query") Then
  2. DoCmd.OpenReport "Clearance", acViewDesign, "calvelo joseph Query"
  3. Reports("Clearance").RecordSource = ("calvelo joseph Query")
  4. DoCmd.OpenReport "Clearance", acViewPreview, "calvelo joseph Query"
  5. Else
  6. If Form_List.RecordSource = ("carpio godofredo Query") Then
  7. DoCmd.OpenReport "Clearance", acViewDesign, "carpio godofredo Query"
  8. Reports("Clearance").RecordSource = ("carpio godofredo Query")
  9. DoCmd.OpenReport "Clearance", acViewPreview, "carpio godofredo Query"
  10. Else
  11. If Form_List.RecordSource = ("caseros roberto Query") Then
  12. DoCmd.OpenReport "Clearance", acViewDesign, "caseros roberto Query"
  13. Reports("Clearance").RecordSource = ("caseros roberto Query")
  14. DoCmd.OpenReport "Clearance", acViewPreview, "caseros roberto Query"
  15. Else
  16. If Form_List.RecordSource = ("celajes artemio Query") Then
  17. DoCmd.OpenReport "Clearance", acViewDesign, "celajes artemio Query"
  18. Reports("Clearance").RecordSource = ("celajes artemio Query")
  19. DoCmd.OpenReport "Clearance", acViewPreview, "celajes artemio Query"
  20. Else
  21. If Form_List.RecordSource = ("coloma lino Query") Then
  22. DoCmd.OpenReport "Clearance", acViewDesign, "coloma lino Query"
  23. Reports("Clearance").RecordSource = ("coloma lino Query")
  24. DoCmd.OpenReport "Clearance", acViewPreview, "coloma lino Query"
  25. Else
  26. If Form_List.RecordSource = ("dayao jose Query") Then
  27. DoCmd.OpenReport "Clearance", acViewDesign, "dayao jose Query"
  28. Reports("Clearance").RecordSource = ("dayao jose Query")
  29. DoCmd.OpenReport "Clearance", acViewPreview, "dayao jose Query"
  30. Else
  31. If Form_List.RecordSource = ("De Guzman josian Query") Then
  32. DoCmd.OpenReport "Clearance", acViewDesign, "De Guzman josian Query"
  33. Reports("Clearance").RecordSource = ("De Guzman josian Query")
  34. DoCmd.OpenReport "Clearance", acViewPreview, "De Guzman josian Query"
  35. Else
  36. If Form_List.RecordSource = ("De Guzman vernadette Query") Then
  37. DoCmd.OpenReport "Clearance", acViewDesign, "De Guzman vernadette Query"
  38. Reports("Clearance").RecordSource = ("De Guzman vernadette Query")
  39. DoCmd.OpenReport "Clearance", acViewPreview, "De Guzman vernadette Query"
  40. Else
  41. If Form_List.RecordSource = ("Dela Cruz helen Query") Then
  42. DoCmd.OpenReport "Clearance", acViewDesign, "Dela Cruz helen Query"
  43. Reports("Clearance").RecordSource = ("Dela Cruz helen Query")
  44. DoCmd.OpenReport "Clearance", acViewPreview, "Dela Cruz helen Query"
  45. Else
  46. If Form_List.RecordSource = ("donsol elvira Query") Then
  47. DoCmd.OpenReport "Clearance", acViewDesign, "donsol elvira Query"
  48. Reports("Clearance").RecordSource = ("donsol elvira Query")
  49. DoCmd.OpenReport "Clearance", acViewPreview, "donsol elvira Query"
  50. Else
  51. If Form_List.RecordSource = ("Dorosan jan christopher Query") Then
  52. DoCmd.OpenReport "Clearance", acViewDesign, "Dorosan jan christopher Query"
  53. Reports("Clearance").RecordSource = ("Dorosan jan christopher Query")
  54. DoCmd.OpenReport "Clearance", acViewPreview, "Dorosan jan christopher Query"
  55. end if
  56. end if
  57. end if
  58. end if
  59. end if
  60. end if
  61. end if
  62. end if
  63. end if
  64. end if
  65. end if
  66. end if
  67. end sub
Dec 21 '11 #1

✓ answered by TheSmileyCoder

Well there is alot to say about the code you have shown.

First of, from what I see, it seems your if/then/else statements in the shown code are superflous, you could simply write it as:

Expand|Select|Wrap|Line Numbers
  1. Dim strSource as String
  2. strSource=Form_List.RecordSource
  3. DoCmd.OpenReport "Clearance", acViewDesign, strSource
  4. Reports("Clearance").RecordSource = strSource
  5. DoCmd.OpenReport "Clearance", acViewPreview, strSource
  6.  
Secondly, if you have a limited number of statements you can use the elseIF instead of nested If's.
Expand|Select|Wrap|Line Numbers
  1. If X=0 then
  2.   Msgbox "Divide by zero"
  3. elseIf X=2 Then
  4.   Msgbox X & " is invalid"
  5. else
  6.   Msgbox X
  7. end if
Now the above is actually something you should have been able to find out yourself, by simply placing the cursor on the if keyword, and pressing F1, and you would get a nice explanation.

The third option, is the SELECT CASE statement, again, look at the help file for info, but here is an example:
Expand|Select|Wrap|Line Numbers
  1. Public Sub DoAction(strAction as string) 
  2.   Select Case strAction
  3.     Case "Open"
  4.       DoCmd.OpenForm "frm_Example"
  5.     Case "Close"
  6.       Docmd.Close acForm, "frm_Example"
  7.     case else
  8.       MsgBox "An invalid argument was passed to the DoAction command"
  9.   End Select
  10. End Sub

10 2630
TheSmileyCoder
2,322 Expert Mod 2GB
Well there is alot to say about the code you have shown.

First of, from what I see, it seems your if/then/else statements in the shown code are superflous, you could simply write it as:

Expand|Select|Wrap|Line Numbers
  1. Dim strSource as String
  2. strSource=Form_List.RecordSource
  3. DoCmd.OpenReport "Clearance", acViewDesign, strSource
  4. Reports("Clearance").RecordSource = strSource
  5. DoCmd.OpenReport "Clearance", acViewPreview, strSource
  6.  
Secondly, if you have a limited number of statements you can use the elseIF instead of nested If's.
Expand|Select|Wrap|Line Numbers
  1. If X=0 then
  2.   Msgbox "Divide by zero"
  3. elseIf X=2 Then
  4.   Msgbox X & " is invalid"
  5. else
  6.   Msgbox X
  7. end if
Now the above is actually something you should have been able to find out yourself, by simply placing the cursor on the if keyword, and pressing F1, and you would get a nice explanation.

The third option, is the SELECT CASE statement, again, look at the help file for info, but here is an example:
Expand|Select|Wrap|Line Numbers
  1. Public Sub DoAction(strAction as string) 
  2.   Select Case strAction
  3.     Case "Open"
  4.       DoCmd.OpenForm "frm_Example"
  5.     Case "Close"
  6.       Docmd.Close acForm, "frm_Example"
  7.     case else
  8.       MsgBox "An invalid argument was passed to the DoAction command"
  9.   End Select
  10. End Sub
Dec 21 '11 #2
Mihail
759 512MB
Of corse the first piece of code from Smiley can replace all your code.
But may I ask you why you first open your report in design view ?
This line is not necessary in order to view your report:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "Clearance", acViewDesign, strSource
So you can remove it making the code even shorter:
Expand|Select|Wrap|Line Numbers
  1. Dim strSource as String
  2. strSource=Form_List.RecordSource
  3. Reports("Clearance").RecordSource = strSource
  4. DoCmd.OpenReport "Clearance", acViewPreview, strSource
Dec 22 '11 #3
ADezii
8,834 Expert 8TB
I think that the Main point that we are missing here is that, in order to dynamically change the Record Source of a Report, we should:
  1. Open the Report in Design Mode and Hidden
  2. Modify the Record Source of the Report
  3. Close the Report, Saving the changes
  4. Open the Report with the modified Record Source
    Expand|Select|Wrap|Line Numbers
    1. Dim strReportName As String
    2.  
    3. strReportName = "rptEmployees"
    4.  
    5. With DoCmd
    6.   .OpenReport strReportName, acViewDesign, , , acHidden
    7.    Reports(strReportName).RecordSource = "qryEmployees"
    8.   .Close acReport, strReportName, acSaveYes
    9.   .OpenReport strReportName, acViewPreview, , , acWindowNormal
    10. End With
P.S. - A Report does not become a Member of the Reports Collection unless it is Opened in some manner.
Dec 22 '11 #4
eneyardi
180 100+
Thanks for your reply, The code below is where i used strSource, Is this the right application of your given codes TheSmileCoder? If it is correct, it's the same with my codes. If I'm wrong, how can i use your given codes?

Private Sub TxtClearance_Click()
Dim strSource As String
strSource = Form_List.RecordSource


If strSource = ("juan reynulfo Query") Then
DoCmd.OpenReport "Clearance", acViewDesign, "juan reynulfo Query"
Reports("Clearance").RecordSource = ("juan reynulfo Query")
DoCmd.OpenReport "Clearance", acViewPreview, "juan reynulfo Query"
Else
If strSource = ("lacsamana elizabeth Query") Then
DoCmd.OpenReport "Clearance", acViewDesign, "lacsamana elizabeth query"
Reports("Clearance").RecordSource = ("lacsamana elizabeth query")
DoCmd.OpenReport "Clearance", acViewPreview, "lacsamana elizabeth query"

End If
End If
End Sub
Dec 26 '11 #5
Mihail
759 512MB
No, eneyardi.
Smiley's code has NOT IFs statements.
You only need to add line 1 and 7 around Smiley's code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub TxtClearance_Click()
  2.     Dim strSource as String
  3.     strSource=Form_List.RecordSource
  4.     DoCmd.OpenReport "Clearance", acViewDesign, strSource
  5.     Reports("Clearance").RecordSource = strSource
  6.     DoCmd.OpenReport "Clearance", acViewPreview, strSource
  7. End Sub
The Smiley's five lines will do ALL your job. This five lines are EQUAL to yours 67 lines from your topic post.

Of course, Smiley assume, as a rule, that ALL your queries has the same name as the attached reports.
Example: Your query is named juan reynulfo Query and the attached report is also named juan reynulfo Query.
As long as you follow this "rule" you can add as many queries/reports as you wish and Smiley's five lines will do the job.
Dec 26 '11 #6
eneyardi
180 100+
How can i apply that five line of smiley without using if, if i want to call other query?
Dec 27 '11 #7
eneyardi
180 100+
This is what i need to do in my access program. I have main form name (home), in that form i have a combolist which row source contains 500 names of employees, i have another form which form name is (list). this form will show up when i click one of the name of employee in combolist and its recordsource is based on the name of employee i clicked. For example when i click juan reynulfo, the form list will show up and its recordsource will be juan reynulfo query. In form list there is a command button (print), when i click that print button, report will show up which name is (clearance) and its recordsource is juan reynulfo query. If i click other employee, how can i apply smiley codes replacing my codes without using if as you said?
Dec 27 '11 #8
eneyardi
180 100+
thanks alot guys, thank you smile, thank you mihail, thank you Adezii. it works!
Dec 27 '11 #9
Mihail
759 512MB
At last ! You understand the Smiley's logic.

More than that, lets see if this presumes are really:
I am almost sure that all your queries are identical. With a slight difference in the criteria: where you have different names.
And you wish to design as many queries as different names you have (500).

If this is the situation, what about if you wish to use your database for the US army's employes or for the China's peoples ?

To do all the job you need only one query. And to pass to the criteria row the correct name before you use the query.
More: If you use only one query is no more need to dynamically change the row source for your form and report. You can set up the row source at design time (to point to that query).

Let us know if this is your situation.

Good luck !
Dec 27 '11 #10
eneyardi
180 100+
yup, that's my situation, i don't know that theres a way to use only one query. I will post my next question later regarding that.
Once again thank you very much!
Dec 27 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
2
by: SpiderFly | last post by:
I have a popup window where a user can select from a list of address records. When passing the details back to the calling screen i set all the address fields using code like this: ...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
1
by: Alex P | last post by:
Hi, i would like to check for a given query (insert, update, delete) if there is a key or constraint violation which prevents the query from being executed. Is such a feature implemented in the...
1
by: scotspie501 | last post by:
i have perkins EST and looking for key genenrator for a product code can anyone help
1
nurikoAnna
by: nurikoAnna | last post by:
how to properly separate sql codes........ I am using &_ as a separator...because it will not suits the visual basic code form...it will skip to the next line that's why I am having trouble in it...
2
by: tms005 | last post by:
Anyone know how to compile Aix C++ Codes into Window platform? I have tried to compile my Aix (Version 4.2) C++ Codes using Bloodshed Dev C++ and Microsoft Visual C++ Compiler and I have copied...
2
by: newphpcoder | last post by:
I change my database from mysql to mssql, so I need to change the connection code in my php. I’m not familiar about the codes using mssql. Now cannot connect to my database. I used the following:...
0
by: phpuser123 | last post by:
When I am running the following codes using Eclipse and Netbeans 6.8. I want to see the available COM ports on my computer. When running in Eclipse it is returning me all available COm ports but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.