Results 1 to 6 of 6
  1. #1
    pervezjan is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    12

    Cursor in Forms after validation

    When I put some wrong value in the field the cursor not stay in the same filed its skip to next field. The code is as under:-

    Private Sub RollNo_BeforeUpdate(Cancel As Integer)
    If (RollNo.Value > 1) Or (RollNo.Value < 22) Then
    MsgBox "Roll No is Wrong, Please Type correct Roll No"
    Cancell = True


    End If
    End Sub

    The RollNo is numeric field. I need help from the expert. How to stop the cursor in the box if I put wrong number.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Cancel should have only one 'l'.

    Your code will not allow anything because of the OR operator. Look at the logic of that statement:

    Anything greater than 1 is not valid.

    Anything less than 22 is not valid.

    Every input will match one of those conditions.


    Instead of VBA code, consider the ValidationRule property - with a proper expression.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    pervezjan is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    12
    Thanks for the prompt reply. You are right I was wrong. But still I have the same problem that Message is appearing but cursor did not stop if I put wrong value i.e 24 or 0 etc.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    The code works for me. Did you change spelling of Cancel?

    ValidationRule property will accomplish what you want.

    ValidationText property will allow custom message.

    No code needed.

    What data do you want to allow?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    pervezjan is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    12
    Dear June7

    Thank you very much for correcting me. You solved my problem now its working fine. The big mistake you pointed was the spelling of Cancel

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Tip: Include the following line in every module header and it will help prevent errors with misspelled variables. It will require every variable to be declared and therefore if a variable is used that isn't declared, code will error during compile.

    Option Explicit
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Validation Rules in the Forms
    By JoeyHenry in forum Access
    Replies: 1
    Last Post: 02-04-2013, 05:07 PM
  2. Replies: 1
    Last Post: 12-09-2012, 07:54 PM
  3. Date Validation in Forms and subforms!
    By Arrrggggjhhhhh!!!! in forum Forms
    Replies: 3
    Last Post: 02-01-2012, 11:49 AM
  4. Placing Validation on Forms
    By Accession in forum Forms
    Replies: 3
    Last Post: 09-28-2011, 09:42 PM
  5. T-SQL Cursor within a cursor
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 6
    Last Post: 04-14-2011, 12:31 PM

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