473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Insert a Carriage Return when coding

46
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub btnAddNew_Click()
  3. 'add new record
  4.  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
  5.  '    MsgBox "All Fields are required before moving on."
  6. '    Me.Combo1.SetFocus
  7. ' Else '
  8.     Set rstLabData = CurrentDb.OpenRecordset("LaboratoryData_Table", dbOpenTable)
  9.     rstLabData.Edit
  10.     AddNewLabData
  11.     rstLabData.Update
  12.     ClearForm
  13.  'End If
  14. End Sub
  15.  
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.
May 18 '09 #1
3 2153
Stewart Ross
2,545 Expert Mod 2GB
To continue a line, use a space followed by an underscore before you move on to the next line, like this:

Expand|Select|Wrap|Line Numbers
  1. somevar = somelongphrase * _
  2.           anotherlongphrase + _
  3.           yetanother
In your case, you could split lines wherever suits, like this:
Expand|Select|Wrap|Line Numbers
  1. If (IsNull(Me.text1.Value)) Or (IsNull(Me.text3.Value)) _
  2.    Or (IsNull(Me.text02.Value))  Or (IsNull(Me.text4.Value)) _ 
  3.    Or (IsNull(Me.text5.Value)) Or (IsNull(Me.text6.Value)) _ 
  4. ...
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:

Expand|Select|Wrap|Line Numbers
  1. With me
  2. If IsNull(.text1) Or IsNull(.text3) Or IsNull(.text02) _
  3.  Or IsNull(.text4) Or IsNull(.text5) Or IsNull(.text6) _
  4. ...
  5. End With
-Stewart
May 18 '09 #2
NeoPa
32,556 Expert Mod 16PB
As usual, Stewart's answer is perfect.

I would write a complicated, and very long, If statement like this as :
Expand|Select|Wrap|Line Numbers
  1.     With Me
  2.         If IsNull(.text1) _
  3.         Or IsNull(.text3) _
  4.         Or IsNull(.text02) _
  5.         Or IsNull(.text4) _
  6.         Or IsNull(.text5) _
  7.         Or IsNull(.text6) _
  8.         Or IsNull(.text7) _
  9.         Or IsNull(.text8) _
  10.         Or IsNull(.text9) _
  11.         Or IsNull(.text10) _
  12.         Or IsNull(.text11) _
  13.         Or IsNull(.text12) _
  14.         Or IsNull(.text013) _
  15.         Or IsNull(.text14) _
  16.         Or IsNull(.text15) _
  17.         Or IsNull(.text16) Then
  18.             ...
  19.         End If
  20.         ...
  21.     End With
Notice how it's now easier to see where the TextBox names are inconsistent.
May 18 '09 #3
missinglinq
3,532 Expert 2GB
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)>
May 18 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lauren Quantrell | last post by:
I have created the following trigger: CREATE TRIGGER ON OutputTable FOR INSERT AS Declare @filename nvarchar(35) Declare @filecontents nvarchar(2000) Declare @strcmdshell varchar(150)
2
by: David Cho | last post by:
I am using this expression \d+(,\s*\d+)* to allow only numbers and commas. But if there are carriage returns mixed, it is not validated. Is tehre a way to ignore all carriage returns? I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.