944,129 Members | Top Members by Rank

Ad:
1

VBA Validation Module

by on Sep 20th, 2004
VBA Validation Module
Visual Basic 4 / 5 / 6 Code Snippet (Toggle Plain Text)
  1. Attribute VB_Name = "basValidation"
  2.  
  3. Option Explicit
  4. ' Name: basValidationFunctions
  5.  
  6. ' Purpose: each function will perform a primitive datatype
  7. ' validation and return a boolean value
  8.  
  9. ' Note: The variant data type is used for all parameters as
  10. ' the form sets the value property for a text/combo box
  11. ' to NULL if the user does not supply data. Variant is
  12. ' the only data type that stores the NULL value.
  13. '
  14. ' BCS 240/340 Validation Code Re - Instructor: Shane Bell
  15.  
  16.  
  17. Public Function RequiredInteger(ByVal varNumber As Variant) As Boolean
  18.  
  19. ' Validate a required integer field
  20.  
  21. ' Criteria: cannot be null, must be a whole number
  22.  
  23. ' establish error handler
  24.  
  25. ' On Error GoTo RequiredInteger_Err
  26.  
  27. ' Assume invalid data
  28.  
  29. RequiredInteger = False
  30.  
  31. ' Check criteria
  32.  
  33. If IsNumeric(varNumber) Then
  34. If CDbl(varNumber) = CLng(varNumber) Then
  35. RequiredInteger = True
  36. End If
  37. End If
  38.  
  39. 'RequiredInteger_Exit:
  40. '
  41. ' Exit Function
  42. '
  43. 'RequiredInteger_Err:
  44. '
  45. ' Call ErrorHandler("RequiredInteger", "basValidationFunctions")
  46. ' End
  47.  
  48. End Function
  49.  
  50. Public Function RequiredNumber(ByVal varNumber As Variant) As Boolean
  51.  
  52. ' Validate a required numeric field
  53.  
  54. ' Criteria: cannot be null, must be a number
  55.  
  56. ' establish error handler
  57.  
  58. ' On Error GoTo RequiredNumber_Err
  59.  
  60. ' Assume invalid data
  61.  
  62. RequiredNumber = False
  63.  
  64. ' Check criteria
  65.  
  66. If IsNumeric(varNumber) Then
  67. RequiredNumber = True
  68. End If
  69.  
  70. 'RequiredNumber_Exit:
  71. '
  72. ' Exit Function
  73. '
  74. 'RequiredNumber_Err:
  75. '
  76. ' Call ErrorHandler("RequiredNumber", "basValidationFunctions")
  77. ' End
  78.  
  79. End Function
  80.  
  81. Public Function RequiredDate(ByVal varDate As Variant) As Boolean
  82.  
  83. ' Validate a required date field
  84.  
  85. ' Criteria: cannot be null, must be a date
  86.  
  87. ' establish error handler
  88.  
  89. ' On Error GoTo RequiredDate_Err
  90.  
  91. ' Assume invalid data
  92.  
  93. RequiredDate = False
  94.  
  95. ' Check criteria
  96.  
  97. If Not IsNumeric(varDate) Then
  98. If IsDate(varDate) Then
  99. RequiredDate = True
  100. End If
  101. End If
  102. '
  103. 'RequiredDate_Exit:
  104. '
  105. ' Exit Function
  106. '
  107. 'RequiredDate_Err:
  108. '
  109. ' Call ErrorHandler("RequiredDate", "basValidationFunctions")
  110. ' End
  111.  
  112. End Function
  113.  
  114. Public Function RequiredText(ByVal varText As Variant) As Boolean
  115.  
  116. ' Validate a required text field
  117.  
  118. ' Criteria: cannot be null, cannot be a date, cannot be a number
  119.  
  120. ' establish error handler
  121.  
  122. ' On Error GoTo RequiredText_Err
  123.  
  124. ' Assume invalid data
  125.  
  126. RequiredText = False
  127.  
  128. ' Check criteria
  129.  
  130. If Not IsNull(varText) Then
  131. If Not IsDate(varText) Then
  132. If Not IsNumeric(varText) Then
  133. RequiredText = True
  134. End If
  135. End If
  136. End If
  137.  
  138. 'RequiredText_Exit:
  139. '
  140. ' Exit Function
  141. '
  142. 'RequiredText_Err:
  143. '
  144. ' Call ErrHandler("RequiredText", "basValidationFunctions")
  145. ' End
  146.  
  147. End Function
  148.  
  149. Public Function RequiredAlphaNumeric(ByVal varAlphaNumeric As Variant) As Boolean
  150.  
  151. ' Validate a required text field
  152.  
  153. ' Criteria: cannot be null, cannot be a date
  154.  
  155. ' establish error handler
  156.  
  157. ' On Error GoTo RequiredAlphaNumeric_Err
  158.  
  159. ' Assume invalid data
  160.  
  161. RequiredAlphaNumeric = False
  162.  
  163. ' Check criteria
  164.  
  165. If Not IsNull(varAlphaNumeric) Then
  166. If Not IsDate(varAlphaNumeric) Then
  167. RequiredAlphaNumeric = True
  168. End If
  169. End If
  170.  
  171. 'RequiredAlphaNumeric_Exit:
  172. '
  173. ' Exit Function
  174. '
  175. 'RequiredAlphaNumeric_Err:
  176. '
  177. ' Call ErrorHandler("RequiredAlphNumeric", "basValidationFunctions")
  178. ' End
  179.  
  180. End Function
  181.  
  182. Public Function NotRequiredInteger(ByVal varNumber As Variant) As Boolean
  183.  
  184. ' Validate a not required integer field
  185.  
  186. ' Criteria: must be a whole number or null
  187.  
  188. ' establish error handler
  189.  
  190. ' On Error GoTo NotRequiredInteger_Err
  191.  
  192. ' Assume invalid data
  193.  
  194. NotRequiredInteger = False
  195.  
  196. ' Check criteria
  197.  
  198. If IsNull(varNumber) Then
  199. NotRequiredInteger = True
  200. Else
  201. If IsNumeric(varNumber) Then
  202. If CDbl(varNumber) = CLng(varNumber) Then
  203. NotRequiredInteger = True
  204. End If
  205. End If
  206. End If
  207.  
  208. 'NotRequiredInteger_Exit:
  209. '
  210. ' Exit Function
  211. '
  212. 'NotRequiredInteger_Err:
  213. '
  214. ' Call ErrorHandler("NotRequiredInteger", "basValidationFunctions")
  215. ' End
  216.  
  217. End Function
  218.  
  219. Public Function NotRequiredNumber(ByVal varNumber As Variant) As Boolean
  220.  
  221.  
  222. ' Validate a not required numeric field
  223.  
  224. ' Criteria: must be a number or null
  225.  
  226. ' establish error handler
  227.  
  228. ' On Error GoTo NotRequiredNumber_Err
  229.  
  230. ' Assume invalid data
  231.  
  232. NotRequiredNumber = False
  233.  
  234. ' Check criteria
  235.  
  236. If IsNull(varNumber) Then
  237. NotRequiredNumber = True
  238. Else
  239. If IsNumeric(varNumber) Then
  240. NotRequiredNumber = True
  241. End If
  242. End If
  243. '
  244. 'NotRequiredNumber_Exit:
  245. '
  246. ' Exit Function
  247. '
  248. 'NotRequiredNumber_Err:
  249. '
  250. ' Call ErrorHandler("NotRequiredNumber", "basValidationFunctions")
  251. ' End
  252.  
  253. End Function
  254.  
  255. Public Function NotRequiredDate(ByVal varDate As Variant) As Boolean
  256.  
  257.  
  258. ' Validate a not required date field
  259.  
  260. ' Criteria: must be a date or null
  261.  
  262. ' establish error handler
  263.  
  264. ' On Error GoTo NotRequiredDate_Err
  265.  
  266. ' Assume invalid data
  267.  
  268. NotRequiredDate = False
  269.  
  270. ' Check criteria
  271.  
  272. If IsNull(varDate) Then
  273. NotRequiredDate = True
  274. Else
  275. If IsDate(varDate) Then
  276. NotRequiredDate = True
  277. End If
  278. End If
  279.  
  280. 'NotRequiredDate_Exit:
  281. '
  282. ' Exit Function
  283. '
  284. 'NotRequiredDate_Err:
  285. '
  286. ' Call ErrorHandler("NotRequiredDate", "basValidationFunctions")
  287. ' End
  288.  
  289. End Function
  290.  
  291. Public Function NotRequiredText(ByVal varText As Variant) As Boolean
  292.  
  293.  
  294. ' Validate a not required text field
  295.  
  296. ' Criteria: can be null or any other value except a date or a number
  297.  
  298. ' establish error handler
  299.  
  300. ' On Error GoTo NotRequiredText_Err
  301.  
  302. ' Assume invalid data
  303.  
  304. NotRequiredText = False
  305.  
  306. ' Check criteria
  307.  
  308. If IsNull(varText) Then
  309. NotRequiredText = True
  310. Else
  311. If Not IsDate(varText) Then
  312. If Not IsNumeric(varText) Then
  313. NotRequiredText = True
  314. End If
  315. End If
  316. End If
  317.  
  318. 'NotRequiredText_Exit:
  319. '
  320. ' Exit Function
  321. '
  322. 'NotRequiredText_Err:
  323. '
  324. ' Call ErrorHandler("NotRequiredText", "basValidationFunctions")
  325. ' End
  326.  
  327. End Function
  328.  
  329. Public Function NotRequiredAlphaNumeric(ByVal varAlphaNumeric As Variant) As Boolean
  330.  
  331.  
  332. ' Validate a required text field
  333.  
  334. ' Criteria: cannot be a date
  335.  
  336. ' establish error handler
  337.  
  338. On Error GoTo NotRequiredAlphaNumeric_Err
  339.  
  340. ' Assume invalid data
  341.  
  342. NotRequiredAlphaNumeric = False
  343.  
  344. ' Check criteria
  345.  
  346. If IsNull(varAlphaNumeric) Then
  347. NotRequiredAlphaNumeric = True
  348. Else
  349. If Not IsDate(varAlphaNumeric) Then
  350. NotRequiredAlphaNumeric = True
  351. End If
  352. End If
  353.  
  354. 'NotRequiredAlphaNumeric_Exit:
  355. '
  356. ' Exit Function
  357. '
  358. 'NotRequiredAlphaNumeric_Err:
  359. '
  360. ' Call ErrorHandler("NotRequiredAlphaNumeric", "basValidationFunctions")
  361. ' End
  362.  
  363. End Function
Comments on this Code Snippet
Feb 14th, 2011
-2

Re: VBA Validation Module

Click to Expand / Collapse  Quote originally posted by Paladine ...
VBA Validation Module
hi,,
i am a student currently doing a project on an automation program. the function of a program is to validate a user from ecel. this is by checking whether the the user's id no. from the 1st spreadsheet exists in the second spreadsheet...is it possible..i tried to search everywhere for the code...but failed to get anything related...please respond
Newbie Poster
pat_dohc is offline Offline
1 posts
since Feb 2011
Jul 28th, 2011
0

Re: VBA Validation Module

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub demo()
  2. Dim strDate As String
  3. strDate = "12/31/2011" 'Date to be validated
  4. MsgBox OKdate(strDate) 'Function call to validate date
  5. End Sub
  6.  
  7. Function OKdate(S) 'eight digits MM/dd/yyyy
  8. Dim mn, dt, yr
  9. On Error Resume Next
  10. mn = left(S, 2) ' First 2 digits of the string
  11. dt = Mid(S, 4, 2) ' Mid 2 digits of the string
  12. yr = Right(S, 4) ' Last 4 digits of the string
  13.  
  14. If ((mn > 12) Or (dt > 31) Or (yr > CInt(Year(Now)))) Then
  15. Err = 1
  16. GoTo label
  17. End If
  18. OKdate = CDate(mn & "/" & dt & "/" & yr)
  19. label:
  20. OKdate = (Err = 0)
  21. End Function
Last edited by Nick Evan; Jul 28th, 2011 at 6:10 am.
Newbie Poster
yathish.b10 is offline Offline
1 posts
since Jul 2011
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Sending parameters to crystal reports.
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Open Excel file from Visual Basic





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC