I still cannot get out of this one. I have just created a new form with two sub-forms, one of which has a sub-sub-form. The problem is wholly confined to the sub-sub-form and is as follows:
The combo box, [CNVATRate], is bound to tbl_VAT_Rates, which is not linked to, or used by, the main form nor any of the other sub-forms. Nor is it bound to the sub-sub-form, in any way other than to the combo box in question
tbl_VAT_Rates comprises two columns. VAT rate category is listed in the first (VAT_Rate), as strings, and the associated percentage Value Added Tax rates are listed in the second (VAT_Percent), as single precision numbers with one place of decimals. Thus "Standard" in the first column is associated with 17.5 in the second. The combo box, [CNVATRate], is bound to this table and consequently offers the options "Non-VAT", "Reduced","Standard" and "Zero-Rated". I cannot work out how to get the matching value of the second column from the combo box directly, so I use the following bit of VBA to asign the matching percentage rate to the field [CNVAT]. (The CN prefix simply indicates that this sub-sub-form is used to issue credit notes.)
If (IsNull(Me![CNVATRate]) = False) And Me![CNCredit] > 0 The
Me![CNVAT] = DLookup("VAT_Percent", "tbl_VAT_Rates", "VAT_Rate =" & Me![CNVATRate]
End I
This is pretty self-explanatory. It just says, "If the VAT Rate category has been selected in the combo box [CNVATRate] and a non-zero credit has been entered in [CNCredit] then look up the matching percentage VAT in tbl_VAT_Rates, the table to which the combo box is also bound, and display it in the unbound field [CNVAT] (from where it can subsequently be multiplied by [CNCredit] to arrive at the amount of VAT to be credited.
As with all other attempts I have made to use DLookUp in this way, on a number of other forms and in different circumstances using different combo boxes, the instruction fails with the, "You cancelled the previous operation," error message. The syntax is acceptable to the the compiler and, when checked though the debugger, the Me![CNVATRate] criterion always displays the correct value when DLookUp is run - ie it faithfully reflects the currently selected option in the combo box
In circumstances where there is no criterion, DLookUp works just fine. In cases where a criterion is involved, however, if that criterion is derived from a combo box the result is invariably failure, wherever or however DLookUp is used. I cannot understand this nor see how to solve my problem without, perhaps, using a query followed by a Requery after the VAT Rate has been selected in the combo box. However, that is not the point. It is always possible to 'find another way' but what I need to know is why DLookUp won't play ball in these circumstances. It is galling not to be able to take advantage of a useful function because I cannot get it to work for reasons that I do not comprehend.