DLOOKUP works in test database, but not in real one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #1

    DLOOKUP works in test database, but not in real one

    I ran a test database that looks up product information from the Products table to populate fields in the Sales table through a form. In the test DB, I enter the product number, and the item description and price appear(I used DLOOKUP to do this). When I manually copy the code over to the real DB, it doesn't matter what product ID I type in, only the first item in the Products table populates the Item description and Price fields. On both databases, I used VBA under the After Update event. I have opened both VBA files and they look exactly the same. Why does one work and the other doesn't? Is there a better way to do the same thing besides DLOOKUP?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Kindly Post the code that you are using.

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      Private Sub ProductID_After Update()
      ProductName = DLookup("Produc tName", "PriceList" , "[ProductID] =" & ProductID)
      Profit = DLookup("Profit ", "PriceList" , "[ProductID] =" & ProductID)
      Price = DLookup("Price" , "PriceList" , "[ProductID] =" & ProductID)
      Tax = DLookup("Tax", "PriceList" , "[ProductID] =" & ProductID)
      End Sub

      All fields in the form are text boxes. It doesn't matter what product ID I put in the ProductID text-box, the same product name, profit, price and tax shows up. I also just remembered, the one where it works has the ProductID as a combo box that is linked to both the ProductID and ProductName, but it just shows the ProductName. Is that the difference, combo-box vs. text-box?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Personally, I would include all 5 Fields in the Row Source of the Combo Box, hide selective Fields, then derive the appropriate Values once the User has selected a Product based on the Column Number. I created a Demo that includes all the Fields, displays only the [ProductName] Field (sorted alphabetically) , while hiding the other Fields (Columns). Once a Value is selected, the other Fields are pushed to the appropriate Text Boxes. This approach is essentially foolproof, since there can be no Typos, and the only Values that may be selected are those in PriceList Table (LimitToList = Yes). Download the Attachment to see what I am referring to.
        Attached Files

        Comment

        • Seth Schrock
          Recognized Expert Specialist
          • Dec 2010
          • 2965

          #5
          I really don't want to use a combo box. For my store, a item number works better because of having different shipments of the same item costing different amounts. A text box which allows you to enter a item number that is placed on the product is easier than looking through product, new product, 3rd shipment product, etc. At first I liked the idea, but after thinking about it for a while, a combo box just won't work for me. Using your DB as an example, I would leave off the combo box, type the product ID in, and have the rest of the fields fill. Does that make sense? The reason that I started with a combo box was that it was the way described in a video I found online. It was when I tried translating that into a text-box as the source that I ran into trouble.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            In that case:
            Attached Files

            Comment

            • Seth Schrock
              Recognized Expert Specialist
              • Dec 2010
              • 2965

              #7
              That looks like it will work! Now I have to figure out how you did it :) I assume that the Private Sub cboProducts_Aft erUpdate section is no longer being used. What is the DCount command being used for? Why do you use the Me![txtProductName]=DLookup... and all of the others instead of just ProductName=DLo okup...? I really want to know because I would like to understand the VBA side of Access much more. Do you have any recommendations for books that I should study or websites so that I could learn this stuff? I was looking at Access 2007 VBA Programmer's Reference on Amazon. What do you think of it?

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                1) I assume that the Private Sub cboProducts_Aft erUpdate section is no longer being used
                That is correct and is replaced by the AfterUpdate() Event of txtProductID, namely:
                Code:
                Private Sub txtProductID_AfterUpdate()
                End sub
                2) What is the DCount command being used for?
                It lets you find out whether or not there are any Records that match the ProductID.
                3) Why do you use the Me![txtProductName]=DLookup... and all of the others instead of just ProductName=DLo okup...?
                You could very well assign the Result of the DLookup() Functions to Variables, but I chose to simply write the Results to Text Boxes on the Form.

                Comment

                • Seth Schrock
                  Recognized Expert Specialist
                  • Dec 2010
                  • 2965

                  #9
                  For the third question, I wasn't meaning variables, but the names of the text-box controls. Here is what I had before that didn't work. It kind of gives you an idea of the format that I'm looking for. Hopefully I will be able to do a bit of copy and paste with a very few changes to get it to work.
                  Attached Files

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Sorry, but I am using Access 2003, but if you can Convert it to 2003 Format, I'll be more than happy to have alook at it.

                    Comment

                    • Seth Schrock
                      Recognized Expert Specialist
                      • Dec 2010
                      • 2965

                      #11
                      Sorry, here it is.
                      Attached Files

                      Comment

                      • ADezii
                        Recognized Expert Expert
                        • Apr 2006
                        • 8834

                        #12
                        The Field to be referenced in the PriceList Table is [ID] and NOT [ProductID]
                        Code:
                        Private Sub ProductID_AfterUpdate()
                          ProductName = DLookup("ProductName", "PriceList", "[ID] =" & ProductID)
                          Profit = DLookup("Profit", "PriceList", "[ID] =" & ProductID)
                          Price = DLookup("Price", "PriceList", "[ID] =" & ProductID)
                          Tax = DLookup("Tax", "PriceList", "[ID] =" & ProductID)
                        End Sub

                        Comment

                        • Seth Schrock
                          Recognized Expert Specialist
                          • Dec 2010
                          • 2965

                          #13
                          I will have to change that and see if it works. I figured it was some simple problem like that, but I looked and looked and still missed it. As soon as I get a chance I will try that and see if it fixes the problem. Thank you so much for your help. I will also try your code and see if I can duplicate what you did. Thanks again.

                          Comment

                          • Seth Schrock
                            Recognized Expert Specialist
                            • Dec 2010
                            • 2965

                            #14
                            That did it! I just knew I had to be missing something.

                            Comment

                            Working...