Syntax (1 Viewer)

mike60smart

Registered User.
Local time
Today, 08:52
Joined
Aug 6, 2017
Messages
1,910
Hi Everyone

I had the following in an OnClick Event for a Subform
Code:
Private Sub cmdMask_Click()

10        On Error GoTo cmdMask_Click_Error
       Dim x

20        x = InputBoxDK("Password Required", "Password Required")
30        If x = "" Then End
40        If x = "SCtb0825" Then
50        DoCmd.OpenForm "Administration", acNormal
60        MsgBox "Welcome " & Environ("UserName") & "!", vbExclamation

70      End
80        End If

        
90        On Error GoTo 0
100       Exit Sub

cmdMask_Click_Error:

110       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdMask_Click, line " & Erl & "."

End Sub


I then attempted to add a requirement for a Password with the following Code.

This now gives me the error shown below.
Any help in pointing out where I am wrong appreciated.

Code:
Private Sub cmdDisable_Click()
       Dim x

10        x = InputBoxDK("Password Required", "Password Required")
20            If x = "" Then End
30        If x = "SCtb0825" Then
            Dim ctrl As Control
40          For Each ctrl In Detail.Controls
50        If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox) Then
60          ctrl.Enabled = False
70        End
80          End If

90    Next


End Sub
 

Attachments

  • Error.PNG
    Error.PNG
    25.8 KB · Views: 64

Gasman

Enthusiastic Amateur
Local time
Today, 08:52
Joined
Sep 21, 2011
Messages
14,311
Try indenting Mike.
Then these silly errors are easier to spot
:(
You have 2 ifs, one endif and one end!!!!!
Probably not even in the correct order as there is a loop in there as well.?
 

mike60smart

Registered User.
Local time
Today, 08:52
Joined
Aug 6, 2017
Messages
1,910
Try indenting Mike.
Then these silly errors are easier to spot
:(
You have 2 ifs, one endif and one end!!!!!
Probably not even in the correct order as there is a loop in there as well.?
Hi Gasman
This is what I can't get my head around.

I have tried all combinations that I can think of where to put End If's with no success
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:52
Joined
Sep 21, 2011
Messages
14,311
Well you have to apply logic in what you want to do. :(
Why would you want to disable all controls when a particular password is supplied?
Take it one step at a time, one logic step at a time.
And indent the code correctly. That always helps. More so than line numbers.
 

mike60smart

Registered User.
Local time
Today, 08:52
Joined
Aug 6, 2017
Messages
1,910
Hi Gasman

OK can you point me in the right direction in regards to Indenting the following

Code:
Dim x
    Dim ctrl As Control
        
            x = InputBoxDK("Password Required", "Password Required")
            If x = "" Then End
            
            If x = "SCtb0825" Then
                For Each ctrl In Detail.Controls
                If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox) Then
                ctrl.Enabled = False
    End
    End If

    Next
 

bastanu

AWF VIP
Local time
Today, 00:52
Joined
Apr 13, 2010
Messages
1,402
Mike,
Here it is with proper indentation, see how easy it is to spot the missing pieces:
Code:
Private Sub cmdDisable_Click()

Dim x
Dim ctrl As Control

x = InputBoxDK("Password Required", "Password Required")

If x = "" Then Exit Sub ' there is no End  :)      

If x = "SCtb0825" Then  
    For Each ctrl In Me.Detail.Controls
        If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox) Then
            ctrl.Enabled = False
            'End -see above occurence
        End If
    Next ctrl 'for clarity
End If 'was missing

End Sub

Here is some more info about End and why should be used carefully:

Cheers,
 
Last edited:

ebs17

Well-known member
Local time
Today, 09:52
Joined
Feb 7, 2020
Messages
1,947
This is what I can't get my head around.
Care is taken when writing code. If block: First write completely with blank lines, then fill in the lines in between:
Code:
If x=1 Then   
   'step 2
Else   
   ' step 3
End If
Same with loop blocks:
Code:
For I=0 To y   
   'step2
Next
So if you write the End If or the Next right away, you don't run the risk of forgetting it. You also know how to indent.
The visible indentations you see look like they were generated by the random number generator and are not helpful.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:52
Joined
Feb 28, 2001
Messages
27,191
A BRIEF discussion on code blocks...

Any time you use a code element that "expects" another specific code element, you have potentially created a code block. So for example,

Code:
PUBLIC SUB BLOCKHEAD( Z AS INTEGER, X AS DOUBLE, Y AS DOUBLE )   'only issued at code block level 0 i.e. same level as module declarations
Z= 1      'in code-block #1 at level 1
IF X>Y THEN     'create new block & level
    'in code-block #2 at level 2
ELSE          'does not create new level but DOES create new block
    'in code-block #3 at level 2
    IF ( X - Y ) <> 0 THEN       'create new block and level
        Z = 0   'in code-block #4 at level 3
    END IF     'terminates prior level 3 block
END IF       'terminates prior level 2 block
END SUB    'terminates prior level 1 block
'now sitting at level 0 but not in a block at the moment.

You will get used to the idea that certain pairings of syntax elements are just like parentheses - not for code or statement fragments, but for small contiguous groups of code statements. The error "Block If without End If" signifies that your block-forming statements are not balanced and that the imbalance was noted in a multi-line IF / END IF pairing.

Watch out for these pairings of syntax elements:

IF / END IF (where ELSE and ELSEIF can occur between the IF and END IF); DO / WHILE ; DO / UNTIL; FOR index=/ NEXT index; FOR EACH X / NEXT X; SUB / END SUB;

and there are a few more. The rule is simple: These syntax elements come in BALANCED PAIRS. You can use EXIT FOR, EXIT DO, EXIT SUB, and various other tricks that act like a GOTO to escape from the balanced logic, but if you don't provide the syntax balance, the compiler will tell you things like "X without Y" (see my list above for most likely X and Y elements). If you INTERTWINE the elements (as you did by placing a "plain" END before an END IF, you "cut off" a block before it sees its "normal" block-ending element. And THAT is how you get an "X without Y" error.
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:52
Joined
Sep 21, 2011
Messages
14,311
Mike,
If you are too lazy to indent your code yourself, then try Smart Indenter.

Great for when I have to copy code like yours to see if it works in a db of mine. Also helps to find errors as I have already mentioned. :(


Should also come in handy when you take on all these databases from other members? I dare say their indentation leaves a lot to be desired?
 

mike60smart

Registered User.
Local time
Today, 08:52
Joined
Aug 6, 2017
Messages
1,910
Mike,
Here it is with proper indentation, see how easy it is to spot the missing pieces:
Code:
Private Sub cmdDisable_Click()

Dim x
Dim ctrl As Control

x = InputBoxDK("Password Required", "Password Required")

If x = "" Then Exit Sub ' there is no End  :)     

If x = "SCtb0825" Then 
    For Each ctrl In Me.Detail.Controls
        If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox) Then
            ctrl.Enabled = False
            'End -see above occurence
        End If
    Next ctrl 'for clarity
End If 'was missing

End Sub
Hi Vlad

Many thanks for the clarification.

I will try to take it in.
A quick read that may help you...
Thanks I will take a look
 

Users who are viewing this thread

Top Bottom