Combobox selecting wrong value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lrw0831
    New Member
    • Sep 2008
    • 18

    #1

    Combobox selecting wrong value

    On my main form I have a Product drop down. Once a product is select, the Module combo box is to be popluated with modules related to that product.

    The combo box populates correctly, but if I click on any module, it automatically selects the first item in the box.

    Example:

    My Product is CA, the modules are Assessment, Capacity, Implementation, Planning, Evaluation, REports, Other.

    If I select CA, then under module I select Implementation, the module combobox popluates with "assessment "

    Here is my code for the Product on change event
    Code:
    Private Sub Product_Change()
    Dim t As String
        
        t = Me.Product.Value
        
        cboModule.SetFocus
        cboModule.RowSource = "Select [product id], Module FROM Module where [product id] = " & t & ";"
        Product.SetFocus
        
    End Sub
    Can anyone help me with this problem?

    Thank you
    Last edited by NeoPa; Oct 13 '08, 03:57 PM. Reason: Please use the [CODE] tags provided
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    You need to surround t with quotes (Line #7) :
    Code:
    "... = '" & t & "';"

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Unless I miss my guess, you also want to consider using the AfterUpdate event rather than the Change event.

      Comment

      • lrw0831
        New Member
        • Sep 2008
        • 18

        #4
        Adding the quotes did not work. It caused it to not populate the module combobox.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          What's the value of t when this happens? It seems that something you're saying is not quite accurate.

          Add a "MsgBox t" line before you set up the SQL and post in here the value displayed please.

          Comment

          • lrw0831
            New Member
            • Sep 2008
            • 18

            #6
            I am new to Access and I'm not sure how to do this. Could you tell me what to add to the code?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Of course. No problem.
              Code:
              Private Sub Product_Change()
              Dim t As String
               
                  t = Me.Product.Value
                  MsgBox t
               
                  cboModule.SetFocus
                  cboModule.RowSource = "Select [product id], Module FROM Module where [product id] = " & t & ";"
                  Product.SetFocus
               
              End Sub
              The line I added is found at #5.

              To get to where the code is go into Design mode of the form, Select the [Product] control, Hit Alt-Enter to view the proerties, Find the On Change property, Click on the elipsis button to the right of it when it is selected.

              Now you are in the VBA Editor window. Code can be manipulated from here. Paste in this version over the top of your earlier version.

              Comment

              • lrw0831
                New Member
                • Sep 2008
                • 18

                #8
                Thank you for your help. I was able to get this to work.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  Very pleased to hear it. Well done :)

                  Comment

                  Working...