Block If without End If????? (1 Viewer)

Firemansam

Registered User.
Local time
Today, 22:34
Joined
Mar 28, 2007
Messages
16
G'day All,

I was wondering whether some could help me get this to work. Iam basically trying to get a message only when the "M" drive is connected. It keeps coming up telling me "Block If without End If" that way I see it I have and End If statement after every If statement.

Any help would be appreciated.

Sub Test()
Dim fs, d, dc, s, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d In dc

'If d.DriveType = 3 Then
' n = d.volumeName
'End If

If d.driveletter = "A" Or "B" Or "C" Or "D" Or "E" Or "F" Or "G" Or "H" Then
MsgBox "This is not the right drive", vbCritical, "Failed"

Else

If d.driveletter = "M" Then
MsgBox "The M Drive was found", vbExclamation, "Success"


End If



'Next
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:34
Joined
Aug 30, 2003
Messages
36,124
I see 2 If's with only 1 End If, plus a For with no Next.
 

Firemansam

Registered User.
Local time
Today, 22:34
Joined
Mar 28, 2007
Messages
16
Slightly new to using VBA, so do I need to put and End if before I do and Else?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:34
Joined
Aug 30, 2003
Messages
36,124
It depends on the logic of the situation, but there must be an End If for every If. IOW, you could have:

Code:
If Whatever Then
  'something
End If

If Something else Then
  'something else
End If

or

Code:
If whatever Then
  If something else Then
    'whatever
  End If
Else
  'something here
End If

But you'll notice that each If has an End If somewhere (the exception would be a one-line If, but that's not the case here). As a guess, you may want:

Code:
If d.driveletter = "A" Or "B" Or "C" Or "D" Or "E" Or "F" Or "G" Or "H" Then
  MsgBox "This is not the right drive", vbCritical, "Failed"

ElseIf d.driveletter = "M" Then
  MsgBox "The M Drive was found", vbExclamation, "Success"


End If

But don't forget the For needs a Next.
 

brucey54

Registered User.
Local time
Today, 05:34
Joined
Jun 18, 2012
Messages
155
I retyped the code a new error message

"You can't disable a control while it has the focus"

Private Sub Form_Current()
If Me.NewRecord Then
Me.MealDate = DMax("MealDate", "TblDietPlan")
Me.MealDate.Enabled = False
Else
Me.MealDate.Enabled = True
End If
End Sub
 

pr2-eugin

Super Moderator
Local time
Today, 13:34
Joined
Nov 30, 2011
Messages
8,494
I retyped the code a new error message

"You can't disable a control while it has the focus"
Please updathe the appropriate thread --> Passing data from query to a txtbox on a Form
Code:
Private Sub Form_Current()
    If Me.NewRecord Then
        Me.MealDate = DMax("MealDate", "TblDietPlan")
        [COLOR=Blue][B]Me.someOtherControl.SetFocus[/B][/COLOR]
        Me.MealDate.Enabled = False
    Else
        Me.MealDate.Enabled = True
    End If
End Sub
 

Users who are viewing this thread

Top Bottom