Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / October 2007

Tip: Looking for answers? Try searching our database.

if then else

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mabeline - 17 Oct 2007 07:39 GMT
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
Alex Dybenko - 17 Oct 2007 07:58 GMT
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

Signature

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

> Hi,
>
[quoted text clipped - 25 lines]
>
> Mabeline
Mabeline - 17 Oct 2007 20:39 GMT
Thanks Alex, this worked well. Such a small change but it makes a hell of a
difference.

Mabeline.

> Hi,
> should be:
[quoted text clipped - 39 lines]
> >
> > Mabeline
Stuart McCall - 17 Oct 2007 08:16 GMT
> Hi,
>
[quoted text clipped - 25 lines]
>
> 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
Mabeline - 17 Oct 2007 20:38 GMT
Stuart,

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

Mabeline.

> > Hi,
> >
[quoted text clipped - 51 lines]
>     Cancel = True
> End If
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.