Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2004, 12:29 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation Probs using VBA

I am attempting to validate user input on a bound form using VBA.

I need to check whether a user is entering the correct data type. Which is either a numeric value or text. I was wanting to know if VBA had functions I could use to do this? And where would it be best to place this test? I was thinking within the OnExit event?

I had tried writing a simple IF statement, but that didnt work, as when on running the form the code didnt not execute.

The IF statement was as follows

If Me.nametxtbox.Value = "" Then
   Me.nametxtbox.SetFocus
End If

The nametxtbox refers to the input box on the form. Is this correct or should I be referring to the actual database field.

Any ideas? as I am totally confused?


Gaz
__________________
Gaz
 
Old March 18th, 2004, 08:06 PM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you put it in the before update and set Cancel = True if it is invalid, the focus won't leave the control after your warning message. You could use another event and setfocus to the control and do a ctlname.Undo to clear the change. In general, the code for testing Number, Date and String are as follow, where I've allowed number or date entry but cancel (Before Update event only) if it is a text string.

    If Len(Me.nametxtbox & "") Then
        If IsNumeric(Me.nametxtbox) Then
            MsgBox "Is a number"
        ElseIf IsDate(Me.nametxtbox) Then
            MsgBox "Is a date"
        Else
            MsgBox "Is text"
            Cancel = True
        End If
    Else
        MsgBox "Is empty"
    End If

You could test for null rather than length of the control contents & "" but this works fine. The length test is for the case where a control had a value which has now been deleted though one is required.

Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]
 
Old March 19th, 2004, 06:54 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the suggestion. I have since got my code to kind of do what I wanted, but your suggestion is much clear. I am new to VBA, and although I understand the syntax, I am not aware of all the usefully functions. so thanks for the info

Gaz





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pong probs --- Help... ironchef BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 6 February 21st, 2009 04:12 PM
Need help in VBA excel on validation deepunair84296 Excel VBA 2 July 11th, 2008 04:23 PM
More session_start() probs LaVey Beginning PHP 13 March 4th, 2007 10:20 AM
Probs while running macro anukagni Excel VBA 2 February 2nd, 2006 03:29 AM
Probs with LookupDispatcherAction jophy BOOK: Professional Jakarta Struts 1 September 12th, 2004 06:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.