none
Beginner - "elseIf" Statement give true value even if conditions are false RRS feed

  • Question

  • Hi,

    Forgive me if this is a simple problem. I only started developing in MS Access in the past few months and everything I know if self-taught. I have the following code

            If intCheckDiscount = 1 Then
               Me!txtStandardDiscountRate = DLookup("[Statement]", "qry_MembersDiscountsSalesInvoicing")
               Me!txtSettlementDiscountRate = DLookup("[Settlement]", "qry_MembersDiscountsSalesInvoicing")
               Me!txtTaxCode = DLookup("[DefaultTax]", "qry_MembersDiscountsSalesInvoicing")
               Me!txtTaxRate = DLookup("[DefaultTaxPercent]", "qry_MembersDiscountsSalesInvoicing")
               Me!txtDiscountType = "Special"
            ElseIf [qry_MembersDiscountsSalesInvoicingPaymentMethod]![qPaymentMethod] = 1 Then
                Me!txtStandardDiscountRate = DLookup("[Statement]+[Settlement]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtSettlementDiscountRate = 0
                Me!txtTaxCode = DLookup("[DefaultTax]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtTaxRate = DLookup("[DefaultTaxPercent]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtDiscountType = "Standard"
            Else
                Me!txtStandardDiscountRate = DLookup("[Statement]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtSettlementDiscountRate = DLookup("[Settlement]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtTaxCode = DLookup("[DefaultTax]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtTaxRate = DLookup("[DefaultTaxPercent]", "qry_MembersDiscountsSalesInvoicingSuppliers")
                Me!txtDiscountType = "Standard"
                
            End If

    Hopefully this makes sense. In short if "intCheckDiscount" = 1 then it selects the values for StandardDiscountRate, SettlementDiscountRate, etc from "qry_MembersDiscountsSalesInvoicing". This seems to work fine.

    Then next bit is where I am having issues. Even if [qPaymentMethod] doesn't = 1, the true part still runs. I have no idea why. Pulling my hair out cause of this.

    Thanks in advance!


    Thursday, March 17, 2016 1:03 PM

Answers

  • Hi. You can't refer to a query field directly like that from a form. You can use DLookup() or if the form is bound to the query, then you can use the Me keyword, like you did with the other fields on your form. Hope that helps...
    • Marked as answer by David_JunFeng Tuesday, March 29, 2016 1:33 AM
    Thursday, March 17, 2016 1:14 PM
  • >>>Then next bit is where I am having issues. Even if [qPaymentMethod] doesn't = 1, the true part still runs. I have no idea why. Pulling my hair out cause of this.

    According to your description, I agree with the DB guy's suggestion, could you help us figure out which type object is [qry_MembersDiscountsSalesInvoicingPaymentMethod]. If it is Query object, you could use DLookup to get [qPaymentMethod]'s value:
    DLookup("[qPaymentMethod]","[qry_MembersDiscountsSalesInvoicingPaymentMethod]")

    In addition I suggest that you could set breakpoint to debug your VBA code to check that which value is [qry_MembersDiscountsSalesInvoicingPaymentMethod]![qPaymentMethod] .
    • Marked as answer by David_JunFeng Tuesday, March 29, 2016 1:33 AM
    Friday, March 18, 2016 1:53 AM

All replies

  • Hi. You can't refer to a query field directly like that from a form. You can use DLookup() or if the form is bound to the query, then you can use the Me keyword, like you did with the other fields on your form. Hope that helps...
    • Marked as answer by David_JunFeng Tuesday, March 29, 2016 1:33 AM
    Thursday, March 17, 2016 1:14 PM
  • >>>Then next bit is where I am having issues. Even if [qPaymentMethod] doesn't = 1, the true part still runs. I have no idea why. Pulling my hair out cause of this.

    According to your description, I agree with the DB guy's suggestion, could you help us figure out which type object is [qry_MembersDiscountsSalesInvoicingPaymentMethod]. If it is Query object, you could use DLookup to get [qPaymentMethod]'s value:
    DLookup("[qPaymentMethod]","[qry_MembersDiscountsSalesInvoicingPaymentMethod]")

    In addition I suggest that you could set breakpoint to debug your VBA code to check that which value is [qry_MembersDiscountsSalesInvoicingPaymentMethod]![qPaymentMethod] .
    • Marked as answer by David_JunFeng Tuesday, March 29, 2016 1:33 AM
    Friday, March 18, 2016 1:53 AM