Solved Exercises for if elseif else ... (1 Viewer)

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
Hello experts
I need some help.

Can someone who has a time prepare me some 5 difficult problems or more or some good example in which I will have to practice:




Example something like that


Sub ShouldIGoOutToday()
'Set the weather to good or bad
Good_Weather = False
'Set the Rain coat to ready or not
Rain_Coat_Ready = True
'Set the type of bad weather
Bad_Weather = "Some Rain"
If Good_Weather = True Then
'if weather is good then go out
MsgBox "Go Out"
Else
'if weather is not good
If Bad_Weather = "Some Rain" Then
'If the bad weather is some rain
If Rain_Coat_Ready = True Then
'If the bad weather is some rain and the rain coat is ready
MsgBox "Go Out"
Else
'if the bad weather is some reain and the rain coat is not ready
MsgBox "Stay Home"
End If
ElseIf Bad_Weather = "Blizzard" Then
'if the bad weather is blizzard
MsgBox " Stay Home"
End If
End If
End Sub

Sub ShouldIGoOutTodayEx2()
'Set the weather to good or bad
Good_Weather = False
'Set the Rain coat to ready or not
Rain_Coat_Ready = True
'Set the type of bad weather
Bad_Weather = "Some Rain"
If Good_Weather = True Then
'if weather is good then go out
MsgBox "Go Out"
Else
'if weather is not good
If Bad_Weather = "Some Rain" And Rain_Coat_Ready = True Then
'If the bad weather is some rain AND the rain coat is ready then Go Out
MsgBox "Go Out"
ElseIf Bad_Weather = "Blizzard" Or Rain_Coat_Ready = False Then
'if the bad weather is blizzard or the rain coat is not ready then Stay Home
MsgBox " Stay Home"
End If
End If
End Sub


Or If someone has a good articles so I can read and study.
Or if someone can explain me...or where I can search for more

Is that the correct order?
1. if
2. else if
3. else
4. end if
5. else
6. end if


I know that is a basic but I just have a feeling that I need to practice that more.
thank you very much
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:41
Joined
Feb 19, 2013
Messages
16,614
Don’t have examples but you can Google to find plenty of tutorials- here is an example
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
Understanding code will be so much easier for you if you properly indent it so you can see which parts are subordinate to other parts.
 

Steve R.

Retired
Local time
Today, 13:41
Joined
Jul 5, 2006
Messages
4,687
With complex "if" operations, I prefer using the "case statement". The "case statement" is easier to interpret when compared to a complex "if statement". Additionally, the "case statement" has the "case else" option that is beneficial to throw an error message should there be a logical test that was missed or improperly constructed.
 

Minty

AWF VIP
Local time
Today, 18:41
Joined
Jul 26, 2013
Messages
10,371
+1 vote for using a case statement in place of ElseIf's. It's so much easier to read and debug.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:41
Joined
Feb 19, 2013
Messages
16,614
As an aside, in a query, using the switch function is the equivalent of a Case statement and better than using nested iif’s. Switch can be used in vba - but generally easier to use Case
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:41
Joined
Feb 28, 2001
Messages
27,188
In your 6-member list in post #1 of this thread, item 4 precludes the use of the later elements.

An IF statement creates an IF block. Oddly enough, so does an ELSE or ELSEIF. A block is also created by FOR / NEXT or DO / LOOP or any of the other looping methods. An ELSE, ELSEIF, or END IF terminates the previous IF-type block. Limiting ourselves to IF-blocks, you have a general form

Code:
IF <Y/N expression>
    THEN
        TRUE-case... a block of things to do if your expression evaluates to TRUE
    ELSE
        FALSE-case... a block of things to do if your expression evaluates to FALSE
END IF

You cannot put a label in either the TRUE case or FALSE case and jump into the block, though there are many ways to get out of the block. You can omit the ELSE and the FALSE-case block if needed. You can also omit the TRUE-case block but cannot omit the keyword THEN.

Adding ELSEIF into the mix is limited in certain ways...

Code:
IF <Y/N expression #1>
    THEN
        TRUE-case block #1
    ELSEIF <Y/N expression #2>
        TRUE-case block #2
    ELSE
        FALSE-case block #3
END IF

If you do this, there is only ONE ELSE and it is only executed if neither of expression #1 and #2 are true. Again, it is forbidden to jump into any of those blocks, though you can put a fully contained IF-block inside. Then you have to treat IF and END IF as parentheses that need to be matched.

Code:
IF <Y/N expression #1>
    THEN
        TRUE-case block #1
        IF <Y/N expression #2>
            THEN
                TRUE-case block #2
            ELSE
                FALSE-case block #3
       END IF
       now back in TRUE-case block #1 again!!!!
       ELSE
            FALSE-case block #4
END IF

I believe that IF-nesting is limited in depth but it is rare to run into that limit. This case is different than the ELSEIF case. If you want to think of it this way, in the ELSEIF case you get to FALSE-case block #3 if and only if Y/N expression #1 AND Y/N expression #2 are FALSE. In the nested IF case, you get to TRUE-case block #2 if and only if Y/N expression #1 AND Y/N expression #2 are TRUE.

As a matter of my opinion, if you have an urge to write ONE ELSEIF, it might be OK. If you have an urge to write TWO OR MORE ELSEIF statements in the same overall code sequence and related to the same initial IF statement, use a CASE statement instead. ELSEIF "ladders" are harder to read.
 

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
Hello experts
ohhh I see. So you reccomend to use case statments.
So that mean that in your coding you use 80% case statments and 20% elseif?
I was trying to read some code with elseif statments and I get lost, so I start thinking that maybe I am not good in that. :unsure:
Becouse of that I start thikining what is better to use.
Thank you very much for your opinion.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
No. You will always use more If-then-else than case statements. The point is that certain conditions that you might code as IF's are much simpler to understand if you use Case Statements. Here's an example. The code is part of a procedure that creates SQL statements and this case is used to generate the correct data type name using the internal number.
Code:
            Select Case fld.Type
                       
                Case 4  'Long Integer or Autonumber field
                    If fld.Attributes = 17 Then
                        strSQL = strSQL & "[" & fld.Name & "] INT IDENTITY, "
                    Else
                        strSQL = strSQL & "[" & fld.Name & "] INT " & notnull & ", "
                    End If
                Case 10  'Text field
                    strSQL = strSQL & "[" & fld.Name & "] VARCHAR(" & fld.Size & ") " & notnull & ", "
                Case 12  'Memo field
                    strSQL = strSQL & "[" & fld.Name & "] NTEXT " & notnull & ", "
                Case 2  'Byte field
                    strSQL = strSQL & "[" & fld.Name & "] TINYINT " & notnull & ", "
                Case 3  'Integer field
                    strSQL = strSQL & "[" & fld.Name & "] SMALLINT " & notnull & ", "
                Case 6  'Single-precision field
                    strSQL = strSQL & "[" & fld.Name & "] REAL " & notnull & ", "
                Case 7  'Double-precision field
                    strSQL = strSQL & "[" & fld.Name & "] FLOAT " & notnull & ", "
                Case 15  'ReplicationID field
                    strSQL = strSQL & "[" & fld.Name & "] UNIQUEIDENTIFIER " & notnull & ", "
                Case 8  'Date/Time field
                    strSQL = strSQL & "[" & fld.Name & "] DATETIME " & notnull & ", "
                Case 5  'Currency field
                    strSQL = strSQL & "[" & fld.Name & "] MONEY " & notnull & ", "
                Case 1  'Yes/No field
                    strSQL = strSQL & "[" & fld.Name & "] SMALLINT " & notnull & ", "
                Case 11  'OleObject field
                    strSQL = strSQL & "[" & fld.Name & "] IMAGE " & notnull & ", "
            End Select
Here's a case statement using the same initial value but instead of using the hard coded numbers, it uses the constant names and since the action is shorter, it uses a compressed format:
Code:
  Select Case lType
    Case dbBigInt: GetType = "BigInt"           '16
    Case dbBinary: GetType = "Binary"           '9
    Case dbBoolean: GetType = "Boolean"         '1
    Case dbByte: GetType = "Byte"               '2
    Case dbChar: GetType = "Char"               '18
    Case dbCurrency: GetType = "Currency"       '5
    Case dbDate: GetType = "Date"               '8
    Case dbDecimal: GetType = "Decimal"         '20
    Case dbDouble: GetType = "Double"           '7
    Case dbFloat: GetType = "Float"             '21
    Case dbGUID: GetType = "GUID"               '15
    Case dbInteger: GetType = "Integer"         '3
    Case dbLong: GetType = "Long"               '4
    Case dbLongBinary: GetType = "LongBinary"   '11
    Case dbMemo: GetType = "Memo"               '12
    Case dbNumeric: GetType = "Numeric"         '19
    Case dbSingle: GetType = "Single"           '6
    Case dbText: GetType = "Text"               '10
    Case dbTime: GetType = "Time"               '22
    Case dbTimeStamp: GetType = "TimeStamp"     '23
    Case dbVarBinary: GetType = "VarBinary"     '17
    Case Else: GetType = "Undefined - " & lType
  End Select

The one place you should ALWAYS use a case is for your error trapping. Here's an example:
Code:
Exit_Create_tblQueryFields:
    db.Close
    Exit Sub

Err_Create_tblQueryFields:
    Select Case Err.Number
        Case 3043, 3055
            MsgBox "Please select a valid database. Error #" & Err.Number, vbOKOnly
        Case 91   ' db was not opened so it cannot be closed.
            Exit Sub
        Case Else
            MsgBox Err.Number & "-" & Err.Description
    End Select
    Resume Exit_Create_tblQueryFields
 

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
Hello Pat,
yes sorry if I say wrong. I was thinkinig that is better to use select case then if -elseif-elseif-statments?
I know that if -then -else is almost everywhere.
Can I ask another question?

If you have to check 8 statments how would you start?
1. if
2. elseif
3.elseif
4.elseif
5. elseif
6.elseif
7.elseif
8.elseif
9. end if?
I dont know but this elseif is making me probelms when reading all my disaster...


I have to check 8 statments for some combo box field. Is correct to use the code on click event or on before update of the field?
For now I have the code on click event.

How do I know on which event to put the code? I know that must be some rules ...from you experts I learn that for validation rules is best to use before update event on the form...
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
Personally, I never use the ElseIf under any conditions. I use the If-Then-Else which I find much easier to read and more flexible since it allows logic on both the true and false paths which ElseIf does not.

If you are checking the same field, then use a Case statement. Do not use an ElseIF. Look at the Case statements I posted.
Code:
Select Case somefield
    Case 1
        do something
    Case 2
        do something
    Case Else
        do someting
End Select
 

plog

Banishment Pending
Local time
Today, 12:41
Joined
May 11, 2011
Messages
11,646
Agree with Pat.

If you have to check 8 statments how would you start?

95% of the time I would use a Select Case because although its 8 statements, its most likely just one variable tested against 8 values.

A=1
A=2
A=3
...
A=8

Sometimes though, the tests aren't that simple or complete:

A=1
B="Blue"
A=72 AND C<1/1/2022
B="Red"
A=9 AND B="Green" AND C>1/1/2022

I would make each of those an If statement (without an else).
 

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
I will write what is my problem.

I have to check 8 different conditions. I have very very complicate database. I need some example from you experts so I will know and learn how to do that in my form.
Before reading all my disaster of coding...(I know that I need to practice more if elseif statments) I will explain what I want to achieve.


Translation:
1. Redna Analiza = Regular analysis
2. Namen Uporabe = Purpose of testing (this is combo box, with 4 options), Regular analysis-Release analyis-Testing analysis,Changing status.
3. Sproščanje = Release analyis
4. TestaAnaliza = Testing analysis
5. SpreminjanjeStatusa = Changing status
6. Status = status of the record, it can be entered or approved. If it is empty this mean that user did not yet signed the record.
7. StatusKolone = StatusItem in this case is important status release
8. (3) Sproscena = (3) Released
9. SproscanjeUstrezno = ReleasedSucceful, options YES, NO and N/A.
10. DA = YES
11. NE = NO
12. N/A = N/A

I have two tables.
1. table (tblKolone2) and her subform frmVnosKolone
(In this table user enter specific information about some items)
2. table (tblUporabaKolon) and her subform frmKoloneUporabaSUB
(In this table user enter specific information about usage of the item, status item, date/time of usage, notes...)


Before user will start performing anaylsis on the item he can make analysis on item that is not released (before) yet and then he can and will start making analsis (after) releasing the item, that mean the the item is released and he can make analysis on released item.
So i was thinking that way. Before releasing the item he can try to use 4 options ( Regular analysis-Release analyis-Testing analysis,Changing status.) and then he can use the relased item again with 4 options ( Regular analysis-Release analyis-Testing analysis,Changing status.)
I need to block the next
1. if item is released he can not perform again the relased analysis
2. if the item is not released he can not do regualar analysis
3. if the item is not released he can try ( Regular analysis-Release analyis-Testing analysis,Changing status.)
4. if item is relased he can try ( Regular analysis-Release analyis-Testing analysis,Changing status.)

For approving that each record has the correct data I am using signatures. With signatures I mean that I have some sign off form on which analyst enter his name and his password and he sign the record. Then approver login, check if all the data is correct in the specific record and he also enter his name and his password and he approve the record. When the record is approved all the data is locked. Nobody can change data anymore. The record gets his status to approved. If record is not approved is empty field, if is only signed by analyst the record has status entered.


This mean that when analyst will do some measurments he will sign each record.
Record must have 2 signatures. One from the analyst and second for the approver. First line of signature is entered, second line is approved.
Every record in table tbl2, tblUporabaKolon must be approved before user will perform regualar analysis.

In the beggining I say that I have two forms.
When analyst make measurments he use second form frmUporabaKolonSUB and the connected table is tblUporabaKolon.

But before analyst can perform regular analyis he must make one special analysis called Release anaylsis. With that release analysis he will confirm that the selected item is good and prepared for doing the regular analysis then on daily basis.

When analyst will make relase analysis the status of the item will change to status release in my case (3) Sproscena.
Only if the item will have the status (3) Sproscena analyst will be alloved to go to the regualar analysis.

But before he can go to the regualar analyis he has to have some signatures that will confirm that all data is correct and the items is prepared to be used.

With form one, frm VnosKolone and connected table table tblKolone2 analyst and approver confirm that all the data entered for that item is correct, that the item contains valid certificate of anaylsis. This form also must be signed and also must have 2 signatures. From the analyst and from the approver.

So I need to achieve that both forms frmVnosKolone and specific record ID from the tblUporabaKolon on which release analysis was made must have signatures approved.

If first form is not approved and the release analysis is approved analyst can not go to the regualar analis. He must signed the record from tblUporabaKolon. that mean that status signature approved from tblKolone2 and the status signature approved must both be signed with approved.

if the form frmVNosKolone with his specific ID is signed and then if the release analysis with the specific ID is signed then analyst can go and make some regular analysis.

I start thinking how to check all that conditions and use Dcount to find what I need.
I hope can somebody read my disaster and tell me what I am doing wrong and how I have to make that correct.
thank you very much


Private Sub NamenUporabe_Click()
If Me.NamenUporabe = "TestnaAnaliza" Then
Me.SproscanjeUstrezno = "N/A"
ElseIf Me.NamenUporabe = "SpreminjanjeStatusa" Then
Me.SproscanjeUstrezno = "N/A"
Me.OznakaInstrumenta = "N/A"
Me.AnalitskiPostopek1 = "N/A"
Me.AnalitskiPostopek2 = "N/A"
Me.Analiza1 = "N/A"
Me.Analiza2 = "N/A"
Me.EmpChromChemMapa = "N/A"
Me.RefNaAnalizo = "N/A"
Me.StranStevilka = "N/A"
Me.RaztopinaIzpiranje = "N/A"
Me.CasIzpiranja = "N/A"
Me.SteviloInjiciranj = "0"
Me.AnalizaUstreza = "N/A"
End If

If Me.NamenUporabe = "RednaAnaliza" Then
Dim criteria1 As String
Dim criteria2 As String
Dim criteria3 As String
Dim criteria4 As String
criteria1 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID
criteria2 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID
criteria3 = "[status] = 'approved' AND [KID] = " & Me.KID
criteria4 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID


If DCount("*", "tblUporabaKolon", criteria1) = 0 Then
MsgBox "Sproscanje ni bilo izvedeno.Prosim izvedite analizo sproščanja.Ta zahteva ni mogoča in bo zavrnjena."
Me.Undo
DoCmd.Close acForm, "frmKoloneUporabaSUB"
End If

ElseIf Me.NamenUporabe = "Sproščanje" Then
If DCount("*", "tblUporabaKolon", criteria2) > 0 Then
MsgBox "Ponovno želite izvajati analizo sproščanja, čeprav je bilo Sproščanje že ustrezno. Ta zahteva ni mogoča in bo zavrnjena."
Me.Undo
DoCmd.Close acForm, "frmKoloneUporabaSUB"
End If
End If
End Sub

Private Sub NamenUporabe_BeforeUpdate(Cancel As Integer)

If Me.StanjeSproscanja = "(3) Sproscena" And Me.NamenUporabe = "TestnaAnaliza" Then
Me.StatusKolone = "(3) Sproscena"
Me.SproscanjeUstrezno = "DA"
Me.StatusKolone.Enabled = False
Me.SproscanjeUstrezno.Enabled = False
ElseIf Me.StanjeSproscanja = "(3) Sproscena" And Me.NamenUporabe = "SpreminjanjeStatusa" Then
Me.SproscanjeUstrezno = "DA"
Me.OznakaInstrumenta = "N/A"
Me.AnalitskiPostopek1 = "N/A"
Me.AnalitskiPostopek2 = "N/A"
Me.Analiza1 = "N/A"
Me.Analiza2 = "N/A"
Me.EmpChromChemMapa = "N/A"
Me.RefNaAnalizo = "N/A"
Me.StranStevilka = "N/A"
Me.RaztopinaIzpiranje = "N/A"
Me.CasIzpiranja = "N/A"
Me.SteviloInjiciranj = "0"
Me.AnalizaUstreza = "N/A"
ElseIf Me.StanjeSproscanja = "(3) Sproscena" And Me.NamenUporabe = "RednaAnaliza" Then

Dim criteria3 As String
Dim criteria4 As String
criteria3 = "[status] = 'approved' AND [KID] = " & Me.KID
criteria4 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID

If DCount("*", "tblKolone2", criteria3) > 0 And DCount("*", "tblUporabaKolon", criteria4) > 0 Then

Me.StatusKolone = "(3) Sproscena"
Me.SproscanjeUstrezno = "DA"
Me.StatusKolone.Enabled = False
Me.SproscanjeUstrezno.Enabled = False
MsgBox "Analiza Sproščanja je bila izvedena vendar ni podpisana.Prosim podpišite analizo Sproščanja.Vaša zahteva bo zavrnjena."
DoCmd.Close acForm, "frmKoloneUporabaSUB"
End If
End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
Whether the If's are nested or not depends on the relationship. Is one condition dependent on another? With a Case statement, the conditions are mutually exclusive. One and only ONE will be true. With a nested If, the conditions are dependent.

The vast majority of your conditional statements will be of the form:
Code:
If condition = True Then
   do a
Else
   do b
End If

More complex conditions will have nested conditions. People sometimes place arbitrary rules such as not more than three levels of nesting. You have to decide. At some point, the nested If gets too complicated and so instead of nesting the rest of the conditions on one of the paths, you could substitute calls to procedures or use functions to simplify the main line logic.
Code:
If condition = True Then
       do a
Else
    If condition2 = True Then
         do b
    Else
        do c
    End If
End If
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
You REALLY need to learn to properly indent your If's. They are much easier to understand when you make the dependency VISIBLE.
Code:
Private Sub NamenUporabe_Click()
If Me.NamenUporabe = "TestnaAnaliza" Then
    Me.SproscanjeUstrezno = "N/A"
Else
    If Me.NamenUporabe = "SpreminjanjeStatusa" Then
        Me.SproscanjeUstrezno = "N/A"
        Me.OznakaInstrumenta = "N/A"
        Me.AnalitskiPostopek1 = "N/A"
        Me.AnalitskiPostopek2 = "N/A"
        Me.Analiza1 = "N/A"
        Me.Analiza2 = "N/A"
        Me.EmpChromChemMapa = "N/A"
        Me.RefNaAnalizo = "N/A"
        Me.StranStevilka = "N/A"
        Me.RaztopinaIzpiranje = "N/A"
        Me.CasIzpiranja = "N/A"
        Me.SteviloInjiciranj = "0"
        Me.AnalizaUstreza = "N/A"
    End If
End If

If Me.NamenUporabe = "RednaAnaliza" Then
    Dim criteria1 As String
    Dim criteria2 As String
    Dim criteria3 As String
    Dim criteria4 As String
    criteria1 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID
    criteria2 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID
    criteria3 = "[status] = 'approved' AND [KID] = " & Me.KID
    criteria4 = "[SproscanjeUstrezno] = 'DA' AND [KID] = " & Me.KID
    If DCount("*", "tblUporabaKolon", criteria1) = 0 Then
        MsgBox "Sproscanje ni bilo izvedeno.Prosim izvedite analizo sprošcanja.Ta zahteva ni mogoca in bo zavrnjena."
        Me.Undo
        DoCmd.Close acForm, "frmKoloneUporabaSUB"
    End If
Else
    If Me.NamenUporabe = "Sprošcanje" Then
        If DCount("*", "tblUporabaKolon", criteria2) > 0 Then
            MsgBox "Ponovno želite izvajati analizo sprošcanja, ceprav je bilo Sprošcanje že ustrezno. Ta zahteva ni mogoca in bo zavrnjena."
            Me.Undo
            DoCmd.Close acForm, "frmKoloneUporabaSUB"
        End If
    End If
End If
End Sub

Your question is way too large. You are asking us to design the entire application.

Your validation code needs to go into the subform's BeforeUpdate event. You would use dLookup() to determine if some action already exists. If you are using a combo to select the action, you could technically put the code into the combo's BeforeUpdate Event but you will also need code in the Form's BeforeUpdate event to ensure an action was actually selected and perhaps to validate other things also. You might want to view my video on the form's BeforeUpdate event.
 

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
I am total amateur in ms access database. I learn a lot from this forum and a lot of internet.
Yes I know Pat that I Have to practice if then else this is basic and i am like stupid..

I am just confused with elseif. I was using if-then-else in another situations and on this problem I start thinking of that elseif amd select case and how to do that.

Yes I am sorry if I write so complex question. I just need some example how to learn and to check if what I make for now is going in correct way or not.
thank you for your reply and I will watch your video.
Thank you
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:41
Joined
Feb 28, 2001
Messages
27,188
You used the word "Translation" in your question. If this is a simple field that can have ONE value from of a list of values AND whatever this list translates for you is always and only single-valued (not "single word" because phrases are allowed in translation, but single-valued), then EITHER use a CASE statement OR create a JOIN to a table where the field in question links to a translation table - at which point you just pick up the translation directly with no other decisions.

In the case of translating a single field to a single value, I would NEVER use an ELSEIF ladder. I would RARELY use CASE. My preferred choice would always be a JOIN to a translation table. The only time I might use an ELSEIF would be with very limited logic where you are not looking at only one field in all cases. While ELSEIF is perfectly legal, that doesn't make it optimum syntax.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 19, 2002
Messages
43,275
You are not stupid. Stupid people don't use Access. Smart people use Access:)

ElseIf is a variation of the Else statement that originated prior to the Case statement. If you have something that can be implemented with a Case, in the OLDEN days, prior to the advent of the Case statement, the ElseIF was a more compact way of expressing the conditions because it didn't require indentation and allowed you to code something that was mutually exclusive rather than dependent in a more visually pleasing way.

Now that we have the Case statement, ElseIf has outlived its usefulness. Use the Case for mutually exclusive conditions. Use the nested If for dependent conditions. And always pay attention to indentation so that dependencies are visible.

I told you which event to use for the code. I have a strong preference for the FORM's BeforeUpdate event but in some conditions you can use the control's BeforeUpdate but you almost always need code in the Form's BeforeUPdate event anyway so why put code in two events when you can use one?

I told you how to determine if the action has already been employed. Please give it a shot and we'll help you to refine the code.

@The_Doc_Man I think the poster is using the action list to control which actions can be added to the child table at which times. I didn't get the impression that it was a translation. I think the translation was for our benefit so we knew what the words meant. But I absolutely agree with you regarding translations:)

I would probably use a Case statement to implement this logic although there might be some possibility of adding a grouping value to a table and using that to control actions rather than a case with individual actions all listed separately but many doing the same thing. The examples of Case statements I posted showed variations on how the Case works. Some people go years without discovering that one line can contain multiple conditions separated by comas which is the simplest variation. @lacampeona go look at my case for the error trapping to see this. When you want to do the same thing in multiple cases, this shows how to do that.
 
Last edited:

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
Yes I will put like you say. I will batlle with if statments again tomorrow and on for the weekend so I will came back to inform you what disaster i will create.


ohhh :oops: I am so embarased for my coding and my stupid questions.

You are all experts and me nothing.. :(

DocMan i will also try your suggestion: but how i have to create JOIN to a translation table?
hmm I never to that?do you have some example how to make that?

I will try both suggestions.

thank you
 

lacampeona

Registered User.
Local time
Today, 19:41
Joined
Dec 28, 2015
Messages
392
DocMan

Yes I make a list of words from my language so you can know what word mean sometthing.
I think the translation was for our benefit so we knew what the words meant.
 

Users who are viewing this thread

Top Bottom