views:

36

answers:

2

Hi all,

I have a form with 1 combo box (YearToBeBuilt) and 2 text box fields (Cost and YearofExpenditureCost). All controls are linked to a main table and the table is updated once the selections/entries have been made on the form.

I have written a procedure in VB called ReCalcIt()which performs the following procedure when called:

Private Sub ReCalcIt()
If Me.YearToBeBuilt = "" Then
Me.YearofExpenditureCost = Me.Cost
Else
Me.YearofExpenditureCost = Me.Cost * (1 + 0.031) ^ (Me.YearToBeBuilt - 2010)
End If
End Sub

When I wrote this I thought that this would do the following:

If the combo box [YearToBeBuilt] is blank (e.g.-no selection made) Then the textbox [YearOfExpenditureCost] will return the value of the textbox [Cost]. Else, the calculation for YearofExpenditureCost is performed.

But this is not working the way it should

What am I doing wrong? I am a VBA n00b so perhaps my syntax is incorrect?

Thanks in advance!

+1  A: 

Try it with

If Len(Me.YearToBeBuilt & vbNullString) = 0

so the code will look like this:

Private Sub ReCalcIt()
If Len(Me.YearToBeBuilt & vbNullString) = 0 Then
Me.YearofExpenditureCost = Me.Cost
Else
Me.YearofExpenditureCost = Me.Cost * (1 + 0.031) ^ (Me.YearToBeBuilt - 2010)
End If
End Sub
Parkyprg
You're awesome--that did exactly what I wanted it to do! Thanks so much! So because I am trying to understand this...basically that blank selection in the combobox is being interpreted as a string value? So you have to tell the form that if the string value = 0 then do this...etc. Else...etc.? Is that correct?
myClone
Yes, you are correct. Interpret it as a string, see the length of it and check if it is 0. I think it should work also without the vbNullString part - you should test it, but it is safer this way.
Parkyprg
myClone
A: 

The usual way is to say:

If IsNull(MyControl) Then

See also: http://stackoverflow.com/questions/3751233/access-vba-if-form-value-null-then-run-query-else-end-if-not-doing-anythi/3751399#3751399

Remou
Recent versions of Access default to allowing ZLS in the table designer. I don't know if this leads to the storage of ZLS's unless you do something specific or not, but using just Not IsNull() is only going to work 100% reliably for fields that disallow ZLS.
David-W-Fenton
I have tested with Access 2010 and a form based on a table that was deliberately set up to allow ZLS. Even when a field (column) bound to such a control is space-filled, the control is returned by If IsNull(ctl).
Remou
Really? Well, that seems like a bug to me. What if you're using a Null as your default value (not known) and ZLS to mean "known to be blank"? If a control bound to that field returns Null regardless of which value is stored, it completely negates the value of allowing the storage of ZLS's. Hmm. reading again, you say if it's got spaces it's returned as Null, too? That's truly worrisome to me.
David-W-Fenton