if then else

G

Guest

Hi,

Can anyone explain to me why I would get an error at run time with the
following code:

Private Sub FindRecord_BeforeUpdate(Cancel As Integer)
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[Tag Number] = '" & Me![FindREcord] & "'"
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If
End Sub

The error showing up with the debugger is "and else statement without an if".

I don't know a lot about visual basic but it seems a standard bit of syntax
to me and I basically copied it from the visual basic help example so can't
understand why it doesn't work. Have also tried it without the if and just
gone straight to msgbox but I get a similar error message.

Thanks in advance.


Mabeline
 
A

Alex Dybenko

Hi,
should be:
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If

line:
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark

is considered as complete statement

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com
 
S

Stuart McCall

Mabeline said:
Hi,

Can anyone explain to me why I would get an error at run time with the
following code:

Private Sub FindRecord_BeforeUpdate(Cancel As Integer)
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[Tag Number] = '" & Me![FindREcord] & "'"
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If
End Sub

The error showing up with the debugger is "and else statement without an
if".

I don't know a lot about visual basic but it seems a standard bit of
syntax
to me and I basically copied it from the visual basic help example so
can't
understand why it doesn't work. Have also tried it without the if and just
gone straight to msgbox but I get a similar error message.

Thanks in advance.


Mabeline

Access will interpret this line:
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark

as a complete statement, so the ElseIf line has no starting If.

The code should be re-worked like this:

If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If

Also, because you are testing the same value (rs.NoMatch) on both sides of
the statement, you can use Else instead of ElseIf, thereby only making the
comparison once:

If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
Else
MsgBox "Must be a valid tag number"
Cancel = True
End If
 
G

Guest

Stuart,

Thank you very much. Such a small thing change to what I had but it makes a
hell of a difference.


Mabeline.

Stuart McCall said:
Mabeline said:
Hi,

Can anyone explain to me why I would get an error at run time with the
following code:

Private Sub FindRecord_BeforeUpdate(Cancel As Integer)
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[Tag Number] = '" & Me![FindREcord] & "'"
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If
End Sub

The error showing up with the debugger is "and else statement without an
if".

I don't know a lot about visual basic but it seems a standard bit of
syntax
to me and I basically copied it from the visual basic help example so
can't
understand why it doesn't work. Have also tried it without the if and just
gone straight to msgbox but I get a similar error message.

Thanks in advance.


Mabeline

Access will interpret this line:
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark

as a complete statement, so the ElseIf line has no starting If.

The code should be re-worked like this:

If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If

Also, because you are testing the same value (rs.NoMatch) on both sides of
the statement, you can use Else instead of ElseIf, thereby only making the
comparison once:

If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
Else
MsgBox "Must be a valid tag number"
Cancel = True
End If
 
G

Guest

Thanks Alex, this worked well. Such a small change but it makes a hell of a
difference.


Mabeline.

Alex Dybenko said:
Hi,
should be:
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If

line:
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark

is considered as complete statement

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com


Mabeline said:
Hi,

Can anyone explain to me why I would get an error at run time with the
following code:

Private Sub FindRecord_BeforeUpdate(Cancel As Integer)
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[Tag Number] = '" & Me![FindREcord] & "'"
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark
ElseIf rs.NoMatch Then
MsgBox "Must be a valid tag number"
Cancel = True
End If
End Sub

The error showing up with the debugger is "and else statement without an
if".

I don't know a lot about visual basic but it seems a standard bit of
syntax
to me and I basically copied it from the visual basic help example so
can't
understand why it doesn't work. Have also tried it without the if and just
gone straight to msgbox but I get a similar error message.

Thanks in advance.


Mabeline
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top