Insert a Carriage Return when coding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teneesh
    New Member
    • Mar 2007
    • 46

    Insert a Carriage Return when coding

    Hi,

    I have a long if then statement, its so long that in the if/then portion, I need to move to the next line. However I do not know what is used to do so.
    Here's the example of my code - but how do I place it on multiple lines without getting an error?

    Code:
     
    Private Sub btnAddNew_Click()
    'add new record
    [B] If (IsNull(Me.text1.Value)) Or (IsNull(Me.text3.Value)) Or (IsNull(Me.text02.Value))  Or (IsNull(Me.text4.Value)) Or (IsNull(Me.text5.Value)) Or (IsNull(Me.text6.Value)) Or (IsNull(Me.text7.Value)) Or (IsNull(Me.text8.Value)) Or (IsNull(Me.text9.Value)) Or (IsNull(Me.text10.Value)) Or (IsNull(Me.text11.Value)) Or (IsNull(Me.text12.Value)) Or (IsNull(Me.text013.Value)) Or (IsNull(Me.text14.Value)) Or (IsNull(Me.text15.Value)) Or (IsNull(Me.text16.Value)) Then[/B]
     '    MsgBox "All Fields are required before moving on."
    '    Me.Combo1.SetFocus
    ' Else '
        Set rstLabData = CurrentDb.OpenRecordset("LaboratoryData_Table", dbOpenTable)
        rstLabData.Edit
        AddNewLabData
        rstLabData.Update
        ClearForm
     'End If
    End Sub
    The portion in Bold is one long line, because when I do a carriage return - VBA is looking for 'Then' in my if/then statement, preventing me from going that route.
    Last edited by Stewart Ross; May 18 '09, 07:17 PM. Reason: Removed spaces from code tags
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    To continue a line, use a space followed by an underscore before you move on to the next line, like this:

    Code:
    somevar = somelongphrase * _
              anotherlongphrase + _
              yetanother
    In your case, you could split lines wherever suits, like this:
    Code:
    If (IsNull(Me.text1.Value)) Or (IsNull(Me.text3.Value)) _
       Or (IsNull(Me.text02.Value))  Or (IsNull(Me.text4.Value)) _ 
       Or (IsNull(Me.text5.Value)) Or (IsNull(Me.text6.Value)) _ 
    ...
    By the way, you do not need to refer to .value explicitly - it is implied by default. This will in itself cut down the length of your lines, especially if you use the With qualifier to get rid of the repeated Me references. You can also lose the additional bracketing before and after the IsNull calls:

    Code:
    With me
    If IsNull(.text1) Or IsNull(.text3) Or IsNull(.text02) _
     Or IsNull(.text4) Or IsNull(.text5) Or IsNull(.text6) _
    ...
    End With
    -Stewart

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32663

      #3
      As usual, Stewart's answer is perfect.

      I would write a complicated, and very long, If statement like this as :
      Code:
          With Me
              If IsNull(.text1) _
              Or IsNull(.text3) _
              Or IsNull(.text02) _
              Or IsNull(.text4) _
              Or IsNull(.text5) _
              Or IsNull(.text6) _
              Or IsNull(.text7) _
              Or IsNull(.text8) _
              Or IsNull(.text9) _
              Or IsNull(.text10) _
              Or IsNull(.text11) _
              Or IsNull(.text12) _
              Or IsNull(.text013) _
              Or IsNull(.text14) _
              Or IsNull(.text15) _
              Or IsNull(.text16) Then
                  ...
              End If
              ...
          End With
      Notice how it's now easier to see where the TextBox names are inconsistent.

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Also note that, while you can use the underscore to do this inside of parentheses, as in a function, you cannot do it within quotation marks.

        Linq ;0)>

        Comment

        Working...