Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Stop code running.

    This is part of much larger code but I believe this is as much as is relevant:



    When order number does = 0 it continues to CPath anyway and then errors. I thought my if statement would stop the code from going any further but this is not the case.
    Code:
    If Me.Order_Number = Null Then
    Exit Sub
    Else
    
    
    APath = "\\server\finance\Invoices\"
    BPath = Me!Company_Name & ""
    CPath = Me!Order_Number
    Note: if I hover over Me.Order_Number in the code it does say null...

    anyone with an idea?

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    If isnull(Me.Order_Number) Then

    I didn't think any variable could =null

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Click image for larger version. 

Name:	Screenshot_3.png 
Views:	17 
Size:	1.8 KB 
ID:	28781

    I don't know mate. Also in post #1 I wrote =0 when I meant null. Ill have a go at your suggestion now. Thanks for the reply.

  4. #4
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    That's worked fine. Ill remember these things one day I promise.

  5. #5
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Spoke too soon. Just to highlight how useless I am in VBA:


    Code:
    If IsNull(Me.Order_Number) Then
      If IsNull(Me.R_Number) Then
      MsgBox "Please add an order number or R Number"
    Exit Sub
    End If
    End If
    The messege box does work as intended but the exit sub statement seems to be ignored. We are back to the original issue now of it trying to continue with the code.

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Send more code andy

  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,463
    Just put End after the msgbox, that should end the code.

  8. #8
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Ahh ok, Ill have a go. For some reason I thought it was exit sub.

  9. #9
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Code:
    Private Sub Command64_Click()
    
    Dim NPath As String
    Dim CPath As String
    Dim FPath As String
    Dim PPath As String
    Dim strReportName As String
    If IsNull(Me.Order_Number) Then
      If IsNull(Me.R_Number) Then
      MsgBox "Please add an order number or R Number"
    End
    End If
    End If
    APath = "\\server\finance\Invoices\"
    BPath = Me!Company_Name & ""
    CPath = Me!Order_Number
    Dpath = APath & BPath
    EPath = Dpath & CPath & ""
    strReportName = Me.Order_Number
    Dim answer As Integer
    
    answer = MsgBox("Are the PO details complete?", vbYesNo + vbQuestion)
    If answer = vbNo Then
    Exit Sub
    Else
     
     If Dir(Dpath, vbDirectory) = "" Then
            MkDir (Dpath)
        Else
            'do nothing for the "C:\Text Files" directory already exists
            'MsgBox FPath
        End If
     If Dir(EPath, vbDirectory) = "" Then
            MkDir (EPath)
        Else
            'do nothing for the "C:\Text Files" directory already exists
            'MsgBox FPath
        End If
    
    End If
     
    End Sub
    still reaching here:

    Code:
    CPath = Me!Order_Number
    I believe both exit and end was working. It's the way I've written it.

    Ill explain what I would like rather than what I thinks wrong. If order number is null then it should never reach the line :

    Code:
    APath = "\\server\finance\Invoices\"
    BPath = Me!Company_Name & ""
    CPath = Me!Order_Number
    then if R number is also null then the message box should trigger. but in any case order number is null it should not continue.

  10. #10
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Private Sub Command64_Click()

    Dim NPath As String
    Dim CPath As String
    Dim FPath As String
    Dim PPath As String
    Dim strReportName As String
    If IsNull(Me.Order_Number) Then exit

    If IsNull(Me.R_Number) Then
    MsgBox "Please add an order number or R Number"
    End If
    APath = "\\server\finance\Invoices\"
    BPath = Me!Company_Name & "\"
    CPath = Me!Order_Number
    Dpath = APath & BPath
    EPath = Dpath & CPath & "\" 'added something in case you need it
    strReportName = Me.Order_Number
    Dim answer As Integer
    Sugest you debug.print to see if the path comes through as OK.
    answer = MsgBox("Are the PO details complete?", vbYesNo + vbQuestion)
    If answer = vbNo Then
    Exit Sub
    Else

    If Dir(Dpath, vbDirectory) = "" Then
    MkDir (Dpath)
    Else
    'do nothing for the "C:\Text Files" directory already exists
    'MsgBox FPath
    End If
    If Dir(EPath, vbDirectory) = "" Then
    MkDir (EPath)
    Else
    'do nothing for the "C:\Text Files" directory already exists
    'MsgBox FPath
    End If

    End If

    End Sub

  11. #11
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    In the original code, if order is not null, the second test is not made:
    Code:
    If IsNull(Me.Order_Number) Then  
     If IsNull(Me.R_Number) Then
      MsgBox "Please add an order number or R Number"
      End
     End If
    End If
    In the suggested code, the same can be said because of the exit line. To ensure they are both evaluated, use OR

    Code:
    If IsNull(Me.Order_Number) OR IsNull(Me.R_Number) Then
      MsgBox "Please add an order number or R Number"
      Exit Sub (or function)
    End If
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by andy49 View Post
    If isnull(Me.Order_Number) Then

    I didn't think any variable could =null
    Correct. In fact , NULL is not equal to NULL!

    Try this
    Code:
    Sub nulltest()
        MsgBox "(1 = 1)  is   " & Nz((1 = 1), "Null")
        MsgBox "(1 = 2)  is   " & Nz((1 = 2), "Null")
        MsgBox "(1 = Null)  is  " & Nz((1 = Null), "Null")
        MsgBox "(Null = Null)  is  " & Nz((Null = Null), "Null")
    End Sub
    NULL is an unknown, so comparing anything to a NULL must result in unknown (ie NULL).
    This is why there is an IsNull() function.......

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

Similar Threads

  1. Stop executing code when Form opens
    By Joakim N in forum Programming
    Replies: 3
    Last Post: 01-04-2017, 06:09 AM
  2. Disabled Breakpoint continues to stop code
    By Glenn_Suggs in forum Access
    Replies: 3
    Last Post: 08-15-2014, 04:32 PM
  3. VBA code stop adding value when textbox exceed 50
    By Georgi in forum Programming
    Replies: 7
    Last Post: 09-27-2013, 02:32 PM
  4. If New Record Stop Code Exit Sub
    By burrina in forum Forms
    Replies: 1
    Last Post: 12-19-2012, 01:02 PM
  5. Navigation Buttons Stop My Code
    By millerdav99 in forum Programming
    Replies: 6
    Last Post: 03-18-2011, 11:13 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