idea about code

maxmangion

AWF VIP
Local time
Today, 00:14
Joined
Feb 26, 2003
Messages
2,805
on a form i have a control (textbox), in which i enter positive integers only. Now sometimes i need to add this integer so i created a public sub with the following code:

Code:
Public Sub AddStamps()
On Error GoTo Err_AddStamps
Dim frm As Form
Dim ctl As Control
Dim amount As Integer
Set frm = Screen.ActiveForm
Set ctl = Screen.ActiveControl
amount = InputBox("Enter the Amount you wish to add", "Add Stamps")
ctl = ctl + amount
Exit_AddStamps:
Exit Sub
Err_AddStamps:
MsgBox Err.Description, vbExclamation, Err.Number
Resume Next
End Sub

The above code works fine, except when the field is null. If the field is blank nothing happens. Eventually, i tried to include the following code instead of the simple line:

Code:
If ctl Is Not Null Then
ctl = ctl + amount
Else
ctl = amount
End If

With this newly added code i am getting an error 424 object required.

I have also tried putting the following:

Code:
If ctl is null then 
ctl = 0
end if

this last statement will still still give me the error 424, however, after clicking ok i will get the correct result (but since i am getting the error message before, i believe i have something wrong).

Thank you for any suggestions.
 
Try the NZ() function. The default behaviour of this function returns a zero-length-string when the argument is null, or the value of the argument when it is not null. You can over-ride the default (zero-length-string) by specifying an optional parameter to substitute for when null is encountered, in your case zero would be appropriate.

amount = InputBox("Enter the Amount you wish to add", "Add Stamps")
ctl = NZ(ctl,0) + amount


Alternately, the syntax for what you were trying to do is
if isnull(ctl) then...
but this is less elegant IMHO, and requires extra lines of code.

edit:spelling
 
Last edited:
Thanks for the tip of using the NZ function, it worked a treat.
 

Users who are viewing this thread

Back
Top Bottom