article,explains,create,your,Access,validatio
Quick Search for:  in language:    
article,explains,create,your,Access,validatio
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 31,327 lines
 Jobs: 372 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for SQL.
Unix Date Convertor Function
By George Graff on 10/23


Insert pipe delimited rows into multiple rows.
By Charles Toepfer on 10/21


Logfiles by PL/SQL
By Stephan Rechberger on 10/21


To display a name in a default language if the given one doesn't exist
By Serge Alard on 10/18


Order by column except a few values
By Serge Alard on 10/18


Introduction to PL/SQL (Series 3) Cursors
By David Nishimoto on 10/14


Sorting a String using T-SQL
By Gaurav Pugalia on 10/12


Protecting against TSQL virii, worms and time bombs
By Joseph Gama on 10/11


Click here to see a screenshot of this code!Get size in bytes of SP, View, Trigger, UDF or Rule
By Joseph Gama on 10/11

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Creating your own Access2000 validation routines

Print
Email
 

Submitted on: 8/26/2002 1:45:02 PM
By: David Nishimoto 
Level: Beginner
User Rating: By 1 Users
Compatibility:SQL Server 7.0, SQL Server 6.5 and earlier, Other

Users have accessed this article 908 times.
 
(About the author)
 
     This article explains how to create your own Access 2000 validation routines. Access2000 has ValidationRules you can create for each control. However, I found it easier to use Visual Basic techniques to validate my data.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
David Nishimoto
davepamn@relia.net
Article: Creating your own Access2000 validation routines

Overview: This article explains how to create your own Access 2000 validation routines.Access2000 has ValidationRules you can create for each control. However, I found it easier to use Visual Basic techniques to validate my data.

You may wonder why these validation routines were created in Access when the data types and bound controls could prevent most of the data errors. In truth, I uses these routines to validated using Active Server Pages by converting the validation routines to javascript. The javascript was converted to vbascript and run in this Access 2000 demonstration.

You may find creating your own validation routines more flexible and functional than the validation rules in Access.

Objectives: 1. You will be able to determine if user input is a valid number, date, in a list, within a number range, or a field value in a table.

Basic Setup:

1. One command button name "cmdValidate"
2. Five TextBoxes:
a. txtNumber
b. txtDate
c. txtList
d. txtRange
e. txtInTable

Code


Option Explicit
Option Compare Database


Purpose: The Validate button has been pressed by the user.
Each validation type is run.
The IsIntable validation function assumes
you have a table called processes with a field named processname.

Private Sub cmdValidate_Click()
Dim errorMessage As String
Dim List(3) As String
List(0) = "Hello"
List(1) = "World"
List(2) = "Utah"
Call IsaNumber(txtNumber, errorMessage)
Call IsaDate(txtDate, errorMessage)
Call IsaListItem(txtList, errorMessage, List)
Call IsInRange(txtRange, errorMessage, 3, 5)
Call IsInTable(txtInTable, errorMessage, "processes", "processname", "STRING")
msgbox errorMessage
End Sub

Purpose: Valids the user input is a number

Public Sub IsaNumber(objText As TextBox, errormsg As String)
On Error GoTo IsANumber_Error
objText.SetFocus
If IsNumeric(objText.Text) = False Then
errormsg = errormsg & objText.name & ":" & objText.Text & " is not numeric " & Chr(13) & Chr(10)
objText.BackColor = &HFF;&
Else
objText.BackColor = &HFFFFFF;
End If

Exit_IsaNumber: Exit Sub IsANumber_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsaNumber End Sub Purpose: The user input is a date Public Sub IsaDate(objText As TextBox, errormsg As String) On Error GoTo IsaDate_Error objText.SetFocus If IsDate(objText.Text) = False Then errormsg = errormsg & objText.name & ":" & objText.Text & " is not a date " & Chr(13) & Chr(10) objText.BackColor = &HFF;& Else objText.BackColor = &HFFFFFF; End If
Exit_IsaDate: Exit Sub IsaDate_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsaDate End Sub Purpose: A list of valid choices are checked against the users input. The text comparison is not case sensitive. Public Sub IsaListItem(objText As TextBox, errormsg As String, List() As String) On Error GoTo IsaListItem_Error Dim sValue Dim i Dim bFound objText.SetFocus sValue = objText.Value bFound = False For i = 0 To UBound(List) - 1 If ucase(List(i)) = ucase(sValue) Then bFound = True Exit For End If
Next If bFound = False Then errormsg = errormsg & objText.name & ":" & objText.Text & " is not a valid entry " & Chr(13) & Chr(10) objText.BackColor = &HFF;& Else objText.BackColor = &HFFFFFF; End If
Exit_IsaListItem: Exit Sub IsaListItem_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsaListItem End Sub Purpose: The user input is a numeric value within a certain upper and lower range. Public Sub IsInRange(objText As TextBox, errormsg As String, _ lowerlimit As Integer, upperlimit As Integer) On Error GoTo IsInRange_Error Dim sValue objText.SetFocus Call IsaNumber(objText, errormsg) If IsNull(objText.Value) Then sValue = 0 Else sValue = objText.Value End If
If sValue < lowerlimit Or sValue > upperlimit Then errormsg = errormsg & objText.name & ":" & objText.Text & " is not in range " & Chr(13) & Chr(10) objText.BackColor = &HFF;& Else objText.BackColor = &HFFFFFF; End If
Exit_IsInRange: Exit Sub IsInRange_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsInRange End Sub Purpose: The user input is a field value for an access 2000 table. Usually, a bound combo box is used to select a valid field value. However, you may have a need to check for valid database matching. Public Sub IsInTable(objText As TextBox, errormsg As String, tablename As String, _ fieldname As String, datetype As String) On Error GoTo IsInTable_Error Dim sValue Dim rs Dim sql Dim bFound objText.SetFocus sValue = objText If datetype = "STRING" Then sql = "select * from " & tablename & " where ucase(" & fieldname & ")=" & IsNVLString(UCase(sValue)) ElseIf datetype = "DATE" Then Call IsaDate(objText, errormsg) sql = "select * from " & tablename & " where " & fieldname & "=" & IsNVLDate(sValue) ElseIf datetype = "NUMERIC" Then Call IsaNumber(objText, errormsg) sql = "select * from " & tablename & " where " & fieldname & "=" & IsNVLNumber(sValue) End If
Set rs = CurrentDB().OpenRecordset(sql) bFound = False If Not rs.EOF Then bFound = True End If
rs.Close Set rs = Nothing If bFound = False Then errormsg = errormsg & objText.name & ":" & objText.Text & " is not in table " & Chr(13) & Chr(10) objText.BackColor = &HFF;& Else objText.BackColor = &HFFFFFF; End If
Exit_IsInTable: Exit Sub IsInTable_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsInTable End Sub Purpose: Returns a single quote enclosed string with embedded single quotes being converted into double single quotes. If the parameter is an empty string than return a null. Function IsNVLString(parameter) On Error GoTo IsNVLString_Error If IsNull(parameter) Or parameter = "" Then IsNVLString = "Null" GoTo Exit_IsNVLString End If
IsNVLString = "'" & FixApostrophy(parameter) & "'" Exit_IsNVLString: Exit Function IsNVLString_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsNVLString End Function Purpose: Return either a number or a null. Function IsNVLNumber(parameter) On Error GoTo IsNVLNumber_Error If IsNull(parameter) Or parameter = "" Then IsNVLNumber = "Null" GoTo Exit_IsNVLNumber End If
IsNVLString = parameter Exit_IsNVLNumber: Exit Function IsNVLNumber_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsNVLNumber End Function Purpose: Return a # enclosed string if the user data is a date type or null if the parameter is empty. Function IsNVLDate(parameter) On Error GoTo IsNVLDate_Error If IsNull(parameter) Or parameter = "" Then IsNVLDate = "Null" GoTo Exit_IsNVLDate End If
IsNVLDate = "#" & parameter & "#" Exit_IsNVLNumber: Exit Function IsNVLNumber_Error: #If gnDebug Then Stop Resume #End If msgbox Err.Description & ":" & Err.Number Resume Exit_IsNVLNumber End Function Purpose: Replace each single quote with two single quotes. Public Function FixApostrophy(ByVal sSQL As String) Dim sFront$, sBack$, nParamLen% Dim sPhrase As String Dim wLength As Integer Dim i As Integer On Error GoTo FixApostrophy_Error wLength = Len(sSQL) For i = 1 To wLength If Mid$(sSQL, i, 1) = "'" Then sPhrase = sPhrase + "''" Else sPhrase = sPhrase + Mid$(sSQL, i, 1) End If
Next FixApostrophy = sPhrase Exit_FixApostrophy: Exit Function FixApostrophy_Error: #If gnDebug Then Stop Resume #End If 'Standard error handling statement msgbox Err.Description & ":" & Err.Number Resume Exit_FixApostrophy End Function


Other 9 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/26/2002 3:56:02 PM:webJose
A comment on language selection: This has nothing to do with SQL Server or any other languages. This should be categorized under VB!! I could be working with a text file and still need data validation, for example.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/26/2002 3:58:02 PM:webJose
Also, you say it is SQL server, but the caption says Access 2K. My suggestion: Make this a VB resource, and get rid of specific database mentions, unless absolutely required.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.