Results 1 to 3 of 3
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    VBA gone wrong with an if

    I wanted on close button



    Code:
    If Me.frmSubBookingsControl.Form!StatusID = 5 And IsNull(Me.frmSubBookingsControl.Form!CancellationDate) Or IsNull(Me.frmSubBookingsControl.Form!CancellationContact) Or IsNull(Me.frmSubBookingsControl.Form!CancellationReasonID) Then
    MsgBox "You require a date in the Cancellation Date field as well as a reason and a the name of the person who asked to cancel the booking."
    
    
    ElseIf Me.frmSubBookingsControl.Form!StatusID <> 5 And Not IsNull(Me.frmSubBookingsControl.Form!CancellationDate) Then
    MsgBox "If this booking is not cancelled - Please clear the Cancellation Date by returning the status to cancelled and then deleting the Cancellation Date before you can close this form"
    
    
    ElseIf Me.frmSubBookingsControl.Form!StatusID = 5 And Not IsNull(Me.frmSubBookingsControl.Form!CancellationDate) Then
    DoCmd.Close
    
    
    
    
    End If
    The idea is that if the user switches from cancelled (5) to uncancelled but doesn't clear the date field then it won't let you close the form - however it stops at the second if

    please help! (maybe I require more sleep)

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Am I permitted to say that's horrible? No offence meant but your code has little or no structure and is a nightmare to comprehend. If you wish to keep your present logic (and I don't recommend you do) then I'm not sure how VBA will interpret the logical concatenation in the first If statement.

    If --- And --- Or --- Or ---

    You must include parentheses to make your logic clear.

    Better though is to structure your logic. There are two If statements beginning If Me.frmSubBookingsControl.Form!StatusID = 5

    Why don't you structure your logic as follows:

    If Me.frmSubBookingsControl.Form!StatusID = 5 then
    If .....

    Else

    End If
    Else

    End If

    I can't fill in the gaps because I can't interpret your first statement. Get some sleep!

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    lol thanks

    Yes I do appreciate the honest answer. I got some sleep and solved it all - All working thanks.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. What's wrong!
    By khalid in forum Programming
    Replies: 15
    Last Post: 06-27-2011, 06:38 AM
  2. What is wrong with this IFF?
    By bburton in forum Reports
    Replies: 2
    Last Post: 03-16-2011, 10:42 AM
  3. Replies: 6
    Last Post: 10-12-2010, 09:35 AM
  4. What's wrong with this query?
    By jsoldi in forum Queries
    Replies: 2
    Last Post: 10-11-2010, 07:45 AM
  5. What am I doing wrong?
    By brandon in forum Access
    Replies: 2
    Last Post: 08-03-2008, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums