473,394 Members | 1,703 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,394 software developers and data experts.

Syntax Error in an IF statement

9
Help!

I am trying to do an If statement but keep getting a "syntax error (comma)" when I try to run it.

Expand|Select|Wrap|Line Numbers
  1.  Expr1: If([Sales_Amount]<21000000,"5 – 20M",If([Sales_Amount] Between 20999999 And 51000000,"21 – 50M",If([Sales_Amount] Between 50999999 And 101000000,"51 – 100M"," "))) 
I am using Access 2000.


In case it isn't clear what I'm trying to do I would like it to return a value - If the sales amount is less than 210000000 return the value of "5 - 20M", if not less than 21000000 then is sales amount between 20999999 and 5100000 then return the value "21 - 50M", and if not then is the value of the sales amount between 50999999 And 101000000 then return a value of "51 – 100M" else leave it blank.

Any assistance is greatly appreciated.
Feb 15 '07 #1
7 4149
I think you want iif not if..

Help!

I am trying to do an If statement but keep getting a "syntax error (comma)" when I try to run it.

Expand|Select|Wrap|Line Numbers
  1.  Expr1: If([Sales_Amount]<21000000,"5 – 20M",If([Sales_Amount] Between 20999999 And 51000000,"21 – 50M",If([Sales_Amount] Between 50999999 And 101000000,"51 – 100M"," "))) 
I am using Access 2000.


In case it isn't clear what I'm trying to do I would like it to return a value - If the sales amount is less than 210000000 return the value of "5 - 20M", if not less than 21000000 then is sales amount between 20999999 and 5100000 then return the value "21 - 50M", and if not then is the value of the sales amount between 50999999 And 101000000 then return a value of "51 – 100M" else leave it blank.

Any assistance is greatly appreciated.
Feb 15 '07 #2
ADezii
8,834 Expert 8TB
Help!

I am trying to do an If statement but keep getting a "syntax error (comma)" when I try to run it.

Expand|Select|Wrap|Line Numbers
  1.  Expr1: If([Sales_Amount]<21000000,"5 – 20M",If([Sales_Amount] Between 20999999 And 51000000,"21 – 50M",If([Sales_Amount] Between 50999999 And 101000000,"51 – 100M"," "))) 
I am using Access 2000.


In case it isn't clear what I'm trying to do I would like it to return a value - If the sales amount is less than 210000000 return the value of "5 - 20M", if not less than 21000000 then is sales amount between 20999999 and 5100000 then return the value "21 - 50M", and if not then is the value of the sales amount between 50999999 And 101000000 then return a value of "51 – 100M" else leave it blank.

Any assistance is greatly appreciated.
I feel as though this would be an easier solution:
Expand|Select|Wrap|Line Numbers
  1. Sales Range: fCalculateSalesRange([Sales])
Where fCalculateSalesRange() is:
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateSalesRange(MySales) As String
  2.    If MySales >= 50999999 And MySales <= 101000000 Then
  3.       fCalculateSalesAmount = "51 - 100M"
  4.    ElseIf MySales >= 20999999 And MySales <= 51000000 Then
  5.       fCalculateSalesAmount = "21 - 50M"
  6.    ElseIf MySales < 21000000 Then
  7.       fCalculateSalesAmount = "5 - 20M"
  8.    Else
  9.       fCalculateSalesAmount = vbNullString
  10.    End If
  11. End Function
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Sales                Sales Range
  2. $20,513,456.00        5 - 20M
  3. $30,000,000.00       21 - 50M
  4. $75,000,000.00       51 - 100M
  5. $21,000,000.00       21 - 50M
  6. $51,000,000.00       51 - 100M
  7. $1,230,000,000.00    
Any other questions, feel free to ask.
Feb 15 '07 #3
erajim
9
Reply to both of you in one.

Have never heard of IIF statement - I will look at that.

As to the other code that looks great, but where do I put the code. usually I would type the expression in the design view window or in the sql view. not sure how to put the VB script into the query. How do I do that?

Thanks both of you for your help!
Feb 15 '07 #4
erajim
9
Thanks Brisokol! Although the iif did not work for me it did get me to learn about switch function which did work for me! So thanks!
Feb 15 '07 #5
erajim
9
Thanks ADezii!

I did not get to use your code because while researching the iif function I learned about the switch function which did the trick! I would like to learn how to do it your way. However as I mentioned above, where do I put the code. usually I would type the expression in the design view window or in the sql view. not sure how to put the VB script into the query. How do I do that?

Thanks you for your help!
Jim
Feb 15 '07 #6
NeoPa
32,556 Expert Mod 16PB
Thanks ADezii!

I did not get to use your code because while researching the iif function I learned about the switch function which did the trick! I would like to learn how to do it your way. However as I mentioned above, where do I put the code. usually I would type the expression in the design view window or in the sql view. not sure how to put the VB script into the query. How do I do that?

Thanks you for your help!
Jim
Nice to reply to all assistance :)
To use code (VBA) you need to create a module (Alt-F11 from Access; Insert / Module).
Type the code in here then compile and close. This function will then be available to all code (including within SQL) in your project.
HTH.
Feb 15 '07 #7
NeoPa
32,556 Expert Mod 16PB
IIf() is a very important function in SQL.
Switch() is very useful (especially in this case) but one of my favourites is Choose().
Choose(index, choice-1[, choice-2, ... [, choice-n]])

The Choose function syntax has these parts:
Part Description
index Required. Numeric expression or field that results in a value between 1 and the number of available choices.
choice Required. Variant expression containing one of the possible choices.
Feb 15 '07 #8

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

Similar topics

3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
3
by: Nathan Sokalski | last post by:
When trying to submit data to an Access database using ASP.NET I recieve the following error: System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) +41...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
5
by: amitbadgi | last post by:
Hi guys, I am getting the following error in teh insert statement , I am converting this asp application to asp.net, here is teh error, Exception Details:...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
12
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select...
1
by: ajos | last post by:
hi evrybdy, the problem is:- i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem... when i enter values in the 2 text boxes and click enter...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.