Results 1 to 5 of 5
  1. #1
    conradqb is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    1

    If Statement Not Working

    Hi,



    I've got a if statement that doesn't work. The second part (the elseif statement) works fine, but even if I run the first part on its own, it doesn't want to work.

    Code:
    Private Sub cb_intercompany_code_AfterUpdate()
    
        If cb_customer_grp_code.Value = "INTERCO" And IsNull(cb_intercompany_code) = True Then
        
            MsgBox "You must complete an intercompany code when the customer grp code = INTERCO"
            Me.cb_intercompany_code.SetFocus
        
        ElseIf cb_customer_grp_code.Value <> "INTERCO" And IsNull(cb_intercompany_code) = False Then
        
            MsgBox "You cannot complete an intercompany code when the customer grp code is not INTERCO"
            Me.cb_intercompany_code.SetFocus
            Me.cb_intercompany_code = Null
        
        End If
    
    
    End Sub
    Any ideas as to why this might be?

  2. #2
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Get rid of .Value after cb_customer_grp_code ?

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Which line does it halt on when you debug?

    Is it this one?
    Me.cb_intercompany_code.SetFocus

    Maybe the cb_intercompany_code control is not available to receive focus.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What happens - error message, wrong results, nothing?

    Should probably use BeforeUpdate event for data validation.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    As a general rule, I'd recommend adding the prefix "Me!" to your Form Control references. If nothing else, it can help ease confusion when looking at individual code blocks by telling you, at a glance, if something is a Form Control or not.

    Also, IsNull() isn't guaranteed to return what you want (for example, the empty string "" does not evaluate to null). I'd recommend checking the length instead:
    Code:
    Len(Me!cb_intercompany_code & vbNullString) = 0
    It's a little clunkier-looking, but in my experience tends to work more reliably when dealing with user-controlled data.

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

Similar Threads

  1. if statement not working
    By redkyte in forum Programming
    Replies: 7
    Last Post: 04-08-2015, 05:09 PM
  2. IIF Statement isn't working
    By djclntn in forum Queries
    Replies: 9
    Last Post: 08-21-2013, 09:40 AM
  3. Replies: 5
    Last Post: 04-20-2012, 03:54 PM
  4. If statement not working
    By gkaro in forum Queries
    Replies: 10
    Last Post: 02-15-2012, 01:58 AM
  5. IIF Statement not working...
    By LanieB in forum Queries
    Replies: 6
    Last Post: 01-05-2012, 12:55 PM

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