Results 1 to 2 of 2
  1. #1
    slenish is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    2

    Problems with an If statement

    Hello,

    Im currently trying to implement an If statement for a number box. I want to make it so you have to enter in 5 numbers to the box. If you enter in more or less than 5 you get an error message back. I have already wrote some code for what I want to do but for some reason only half of it is working, and I cant figure out why?

    I would appercaite any help.

    Thanks



    Code:
    Dim number, zipCode As Integer
    Dim error As String
    number = 0
    zipCode = 5
     
    If number > zipCode Then
    error = " There are to many numbers. \n"
     
    Else
    If number < zipCode Then
    error = " You need to enter in 5 numbers. \n"
    End If
    End If
    At this point I can get it to error out when you enter in more than 5 but not less.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Two problems, neither of which is your problem, but both need to be addressed. First, you don't want to test the zip itself, you want to test the length of it, right? You want Len(NumberEntered). Second, because a zip can start with zero, you can't use a numeric data type for your variable, you need to use String (a numeric data type will drop the leading zero). Oh, and you would have had to use Long for the zip anyway, because Integer wouldn't hold the larger ones.

    The possible source of your problem is that number is actually declared as a Variant, not an Integer. You would have to have done this to get 2 Integers:

    Dim number As Integer, zipCode As Integer
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. If statement for image
    By turbo910 in forum Forms
    Replies: 1
    Last Post: 11-30-2009, 04:58 AM
  2. IIF statement
    By james1982 in forum Access
    Replies: 1
    Last Post: 07-20-2009, 09:38 AM
  3. how to use IF then statement
    By ronnie4 in forum Access
    Replies: 1
    Last Post: 03-17-2009, 11:32 PM
  4. If then statement in query
    By ronnie4 in forum Queries
    Replies: 1
    Last Post: 01-20-2009, 10:49 AM
  5. IF Statement with Minutes
    By sal_gxer in forum Queries
    Replies: 0
    Last Post: 02-12-2007, 08:39 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