A More Convenient cInt
 
Published: 07 Oct 2003
Unedited - Community Contributed
Abstract
The cInt function raises an exception when passed a null value, blank string, or non-numerical string. You can adapt the function to cInt2 to handle these values and use it in the same manner.
by Dan Powderhill
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 16127/ 16

Introduction

When you work with forms and databases a lot you will often want to save the numerical value of a form element, in particular a group of radio buttons, to a database. In general this is easy, however if the field is not mandatory and the user chooses not to answer then the value which gets passed is null, or a blank string. If you pass the cInt function a null value, blank string, or non-numerical string it raises an exception.

Whilst that in itself isn’t a problem as you can write a routine to check the value before using cInt and make sure it is a valid number, it is easier simply to adapt the cInt function to handle them.
cInt2 Function

The function below, cInt2, does just that and can be used in the same way as the regular cInt function.

function cInt2(strInput)
     'Remove any trailing or leading spaces
     strInput = trim(strInput)

     'If the input is null, blank, or not a number then set it to zero
     if strInput = "" or isNull(strInput) or not isNumeric(strInput) then strInput = "0"

     ' Convert it to an integer using the regular cInt function and return it.
     cInt2 = cInt(strInput)
end function

This function could also be adapted to cater for converting the “on” value passed by checked checkboxes to a given integer.



User Comments

Title: Great stuff!!   
Name: Peter
Date: 2008-11-11 7:42:05 AM
Comment:
Thanks so much - saved me time (that I dont have) in resolving this issue!! Beautiful.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-16 3:23:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search