Else without If

Scott-Atkins

New member
Local time
Today, 14:09
Joined
Apr 11, 2010
Messages
1
Hi All,

Im new to Vb6 and im just putting together something basic and for some reason reason i keep getting "else without if" error, what i want to do is when some one types in a name it will return there address etc, but it throws a "else without if" error. help will be much appreciated.

Private Sub Command5_Click()
Dim response As String
Dim Scott_Atkinon As String
Dim James_Ludlow As String
Dim Mark_webster As String
Dim Gareth_Hughes As String

If txt1.Text = ("Scott_Atkinson") Then
response = MsgBox("83 new hythe lane, Larkfield, ME20, Kent")

Else: txt1.Text = ("James_Ludlow")
response = MsgBox("27 Tonbridge road, ME17, Kent")

Else: txt1.Text = ("Mark_Webster")
response = MsgBox("47 Sunningdale Road, Maidstone, ME34, Kent")

Else: txt1.Text = ("Gareth_Hughes")
response = MsgBox("36 London Road, Maidstone, ME34, Kent")

End If

End Sub

The error is thrown on the 2nd Else statement. I have tried a few things i.e have all if statements with End if after,

If some one can help me it would be much appreciated.

Thanks in Advance
 
You mention VB6 but i guess its the same as in VBA , you cannot have multiple Else in an If Then Endif Block, all but the last will be ElseIf.

Brian
 
Don't understand your string variable names, but hey ho. You would be better doing a Select Case statement instead

Code:
Select Case X
   Case 1
    Do this
   Case 2
    Do this instead
   Case 3
    Or this
   Case Else
    Do nothing
End Select

Where X is the variable you are evaluating.
 
Hi All,


If txt1.Text = ("Scott_Atkinson") Then
response = MsgBox("83 new hythe lane, Larkfield, ME20, Kent")

Else: txt1.Text = ("James_Ludlow")
response = MsgBox("27 Tonbridge road, ME17, Kent")

Else: txt1.Text = ("Mark_Webster")
response = MsgBox("47 Sunningdale Road, Maidstone, ME34, Kent")

Else: txt1.Text = ("Gareth_Hughes")
response = MsgBox("36 London Road, Maidstone, ME34, Kent")

End If




Thanks in Advance

Might be a little late on this but seem to me your syntax is a little off. Try doing it like this.

Code:
If txt1.Text = ("Scott_Atkinson") Then
response = MsgBox("83 new hythe lane, Larkfield, ME20, Kent")
 
ElseIf txt1.Text = ("James_Ludlow") Then
response = MsgBox("27 Tonbridge road, ME17, Kent")
 
ElseIf txt1.Text = ("Mark_Webster") Then
response = MsgBox("47 Sunningdale Road, Maidstone, ME34, Kent")
 
Elseif txt1.Text = ("Gareth_Hughes") Then
response = MsgBox("36 London Road, Maidstone, ME34, Kent")

Else
 'Put something here just in case no one is found.
  MsgBox ("User not found")
 
End If
 

Users who are viewing this thread

Back
Top Bottom