Dlookup function just pick the first record ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mayskys1999
    New Member
    • Feb 2013
    • 12

    Dlookup function just pick the first record ?

    Hello to All Expert.
    I'm new in here and need your help.
    I have 2 tables Models and ProdReport. I have form name ProductsReport. I made Dlookup function in Model drop down to seclect model from table Models. I would like to have right Revision in Models table will pick up to insert in field Firmware section in the form ProuctsReport.
    Here is my work.

    Code:
    Private Sub Model_AfterUpdate()
    
    Firmware = DLookup("[Revision]", "[Models]", _
        "[ModelID]= [ModelID]")
    
    End Sub
    the probem is it just pick the first on table Models only.
    I don't know how to fix it.
    Plesae help !!!
    Thanks in advance.

    I put attachment for your review.
    Thanks again
    Attached Files
    Last edited by Rabbit; Feb 8 '13, 02:35 AM. Reason: Please use code tags when posting code.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    Sorry, I don't open attachments from people that I don't know. And please read Before Posting (VBA or SQL) Code

    You have a problem with your WHERE argument. I can make a guess that you have a control named ModelID and that is what you are wanting the record to match. If that is the case then you need your code to be:
    Code:
    Firmware = DLookup("Revision", "Models", "ModelID = " & Me.ModelID)
    If I'm wrong with my guess, then you just need to place your control name that contains the value you need in place of the Me.ModelID.

    DLookup returns the first record that matches the criteria. In your current code, ModelID will always equal ModelID because these are referring to the same field in the same table. Hopefully that makes sense.

    Comment

    • mayskys1999
      New Member
      • Feb 2013
      • 12

      #3
      I tried your but still showing error

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        I need to know what the error message is. Both the number and the message.

        Also read How to ask good questions. This will tell you what information is expected when you ask a question.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          mayskys1999:
          As Seth indicated, No one here will normally open a file from unknown user. This is matter of good computing practice and security NOT a reflection upon any one person. The reality is that many people have infections on their PC and don't know that fact.

          Because you are a new user, and before you run afoul of the rules:
          Please keep in mind: Bytes is not a code nor homework service. Please read the FAQ and posting guidelines before re-posting your question.
          Before Posting (VBA or SQL) Code.
          How to ask "good" questions -- READ BEFORE SUBMITTING A QUESTION!.
          POSTING_GUIDELI NES: Please Read Carefully Before Posting to a Forum.


          Note the very first link referring to VBA.
          Before you do anything else, please complete the requirements listed in the first section.
          DO a debug compile on your code until you get no errors. The compiler stops on the first error encountered; thus, requiring that you REPEAT the compile step until there are no errors.

          Post back here once you have fixed everything.
          If you have issues fixing something, more than likely you will need to start a new thread unless the error is DIRECTLY related to this question.

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            Lets look at your code:
            Code:
            Firmware = DLookup("[Revision]", "[Models]", _
                "[ModelID]= [ModelID]")
            I presume each and every record in your table will fullfill the requirement that ModelID=ModelID .

            I presume that what meant is for one of the ModelID's to be a value you provide?
            Make sure your modelID combobox has a sensible name for example cmb_SelectModel ID. The cmb short for combobox.

            Now modify your code:
            Code:
            Private Sub [B]cmb_SelectModelID[/B]_AfterUpdate()
              Firmware = DLookup("[Revision]", "[Models]", _
                "[ModelID]= "[B] & Me.cmb_SelectModelID[/B])
            End Sub
            Best of luck with your project.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              Smiley,
              that's what Seth said:
              (...) If I'm wrong with my guess, then you just need to place your control name that contains the value you need in place of the Me.ModelID. (...) ModelID will always equal ModelID (...)

              Comment

              • mayskys1999
                New Member
                • Feb 2013
                • 12

                #8
                Thanks for your help.
                I inserted your code but function still get error. Run-time error '2471. and next line is showing the expression you enter as a query parameter produced this error: 'CCC"
                Could you please help me again.
                thanks alot
                Attached Files

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  Please read my post #5
                  Please do as asked therein.


                  Also take a look at Post #11 in Access 97 to 2010 conversion problem
                  As you have the same pet-peeve within your DLOOKUP function.
                  Pull that criteria string out of the function in a similar manner as I indicate in the post #11 of the referred to thread.
                  This way you can take a look at what the string is resolving to!
                  Last edited by zmbd; Feb 8 '13, 04:12 PM. Reason: [z{removed error message block}]

                  Comment

                  • mayskys1999
                    New Member
                    • Feb 2013
                    • 12

                    #10
                    Thanks for your comment.
                    I'm new and try to learn. Please forgive any mistake I did.
                    Actualy I don't know what I have to do to make it work. I tried so many ways but my project doesn't work.

                    Comment

                    • Seth Schrock
                      Recognized Expert Specialist
                      • Dec 2010
                      • 2965

                      #11
                      What exactly are you wanting the criteria to do? For example, are you trying to find the Revision based on a the value selected in a control or you using a variable that contains the value you are searching by?

                      Comment

                      • mayskys1999
                        New Member
                        • Feb 2013
                        • 12

                        #12
                        I would like to do like this.
                        whenever I pick up a model from cmb_SelectModel ID_AfterUpdate( )
                        It will take right Revision from Models table to put in Firmware of ProductsReport form.
                        Thanks for your support.

                        Comment

                        • Seth Schrock
                          Recognized Expert Specialist
                          • Dec 2010
                          • 2965

                          #13
                          Okay, that is helpful to know. For ease, we will do as Z suggested and make the criteria be a string like so:
                          Code:
                          dim strWhere as string
                          
                          strWhere = "ModelID = " & Me.cmb_SelectModelID
                          Debug.Print strWhere
                          
                          Firmware = DLookup("Revision", "Models", strWhere)
                          Now make sure that the immediate window is open (Ctrl + G will open it if it isn't already) and then change the value in cmb_SelectModel ID to trigger the AfterUpdate event. If this works then great. If not, we need to see the value that got entered in the immediate window.

                          Something that I just noticed, your original post says that it is in the Model_AfterUpda te event, not the cmb_SelectModel ID_AfterUpdate event. Please verify which one is correct.
                          Last edited by Seth Schrock; Feb 8 '13, 05:44 PM. Reason: Added last paragraph.

                          Comment

                          • mayskys1999
                            New Member
                            • Feb 2013
                            • 12

                            #14
                            I used your code to my project.
                            I try to use cmb_SelectModel ID_AfterUpdate.
                            the immediate window showing ModelID = CCC when I picked the third model.

                            Comment

                            • Seth Schrock
                              Recognized Expert Specialist
                              • Dec 2010
                              • 2965

                              #15
                              Okay. In table Models, what is the data type for ModelID?

                              Comment

                              Working...