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

Adding code to Access for Specific Area Codes for a State

Hi All,

Can you see anything wrong with the below code? I'm trying to make sure that if the State in the record is VA, that the area code is either 703 or 804. When I test, no matter what I change the first 3 integers of the phone number to, I get the error msg box. Even if it's 703 or 804, I still get the error box. Any help would be appreciated. Thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     'Verify the correct area code for VA
  3.     Dim AreaCode As Integer
  4.     If Not IsNull([State]) And Not IsNull([Phone]) Then
  5.         AreaCode = Val(Left([Phone], 3))
  6.         Select Case [State]
  7.             Case "VA"
  8.                 If AreaCode <> "703" Or AreaCode <> "804" Then
  9.                     DoCmd.CancelEvent
  10.                     MsgBox "Area Code must be 703 or 804"
  11.                     Phone.SetFocus
  12.                 End If
  13.         End Select
  14.     End If
  15.  
  16. End Sub
Dec 2 '13 #1
5 1221
ADezii
8,834 Expert 8TB
Just a slight change in Logic should do the trick:
Expand|Select|Wrap|Line Numbers
  1. 'Verify the correct area code for VA
  2. Dim AreaCode As Integer
  3.  
  4. If Not IsNull([State]) And Not IsNull([Phone]) Then
  5.   AreaCode = Val(Left([Phone], 3))
  6. Else
  7.   Exit Sub
  8. End If
  9.  
  10. Select Case [State]
  11.   Case "VA"
  12.     Select Case AreaCode
  13.       Case 703, 804
  14.         'do nothing
  15.       Case Else
  16.         MsgBox "Area Code must be 703 or 804"
  17.           Me![Phone].SetFocus
  18.       End Select
  19.   Case Else
  20.     'States other than 'VA'
  21. End Select
P.S. - The Expression
Expand|Select|Wrap|Line Numbers
  1. If AreaCode <> "703" Or AreaCode <> "804" 
will never evaluate to TRUE even if AreaCode = 703 or 804 since it cannot be equal to both at the same time.
Dec 2 '13 #2
Seth Schrock
2,965 Expert 2GB
Your problem is in the line
Expand|Select|Wrap|Line Numbers
  1. If AreaCode <> "703" Or AreaCode <> "804" Then
Because you used the OR comparison, only one side has to be true for the If/Then to follow the true side. So if you enter 703, the first side is false but the second side is true, so your code will produce the message box. If you used the AND comparison, then both sides would have to return true for the true path to be taken. Thus, if you enter 703, then the first side returns false and makes the whole test return false.
Dec 2 '13 #3
Thank you ADezii, That worked perfectly. I don't know why the <> didn't work, but I'm glad you found a way to make it work. Thanks again.
Dec 2 '13 #4
Thank you Seth for the insight. That worked with my original code. I guess I need to be more careful with my comparison operators. Thanks for pointing that out.
Dec 2 '13 #5
ADezii
8,834 Expert 8TB
I don't know why the <> didn't work
It is simply a matter of Syntax.
Expand|Select|Wrap|Line Numbers
  1. '************** Code Intentionally omitted **************
  2. Select Case [State]
  3.   Case "VA"
  4.     If (AreaCode <> 703) And (AreaCode <> 804) Then
  5.       MsgBox "Area Code must be 703 or 804"
  6.         Me![Phone].SetFocus
  7.     End If
  8.   Case Else
  9.     'States other than 'VA'
  10. End Select
will work.
Dec 2 '13 #6

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

Similar topics

0
by: Brian Loesgen | last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25, 2003 at the Scripps Ranch Library. Scripps Ranch Library 10301 Scripps Lake Drive San Diego, CA 92131-1026 Please join us...
0
by: gmesmer | last post by:
Hello I have created an assembly to print a pdf document and I wrote some unit tests and everything works from within nuint and cruisecontrol.net. ( I am using C#) Next I have to put together a...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
4
by: MS News \(MS ILM\) | last post by:
Hello, How do I add some JavaScript into my Response and make the page scroll to some specific area on my page in the Browser when tired few thing it did not work at all Any help and sample...
3
by: Charlie | last post by:
Hi: After posting back, I would like to return to a specific area of page. For example, when my "add to cart" handler posts back, I would like returned page to scroll to where button was...
0
by: Ricky Kam | last post by:
I'm using MODI, after the document is OCR, is there a way to select a word in a specific area? I know it can be done if someone use the mouse and do the selection manually, but if I want to achieve...
6
by: Abel Chan | last post by:
Hi there, I am trying to use ASP.NET to populate list of files with hyper link from a specific directory. The following code works well but it populates the list to the top of the web page. I...
0
by: =?Utf-8?B?TWlrZSBNY0FsbGlzdGVy?= | last post by:
First, I'm not an AD Administrator. However, I have an important business need to be able to learn and pass along information about configuring .NET Code Access Security from Active Directory. ...
6
by: deknoopjes | last post by:
I want to display a specific area of an external website using iFrames. A year ago I created such code successfully, but lost this code due to a crashed computer. Googling for any solutions did not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.