Pass Combo Value to Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • artemetis
    New Member
    • Jul 2007
    • 76

    #1

    Pass Combo Value to Query

    I have a form named: frmSurveyInput
    On the form there are two controls.
    1. cboTableSelect - select input table
    2. cmdQrySurveyInp ut - run qrySurveyInput

    cboTableSelect gets it's values from:

    Code:
    SELECT MSysObjects.Name 
    FROM MSysObjectsWHERE (((MSysObjects.Type=1) AND ((MSysObjects.Name) Like "*tbl*")) OR (((MSysObjects.Type=6) AND ((MSysObjects.Name) Not Like "*~TMPCLP*"))
    ORDER BY MSysObjects.Name;
    and
    cmdQrySurveyInp ut runs the query which contains the following:

    Code:
     UPDATE     [your access table]       AS A
    I would like the [table name] in the query, to be passed to (received from the cboTableSelect) the sql from the cboTableSelect value.

    Am I looking in the right direction?
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    To run the update, you can just execute an SQL statement. Something like:
    Code:
     Dim strSQL as string
    strSQL = "Update " & cboTableSelect & " SET ..."
    Currentdb.Execute strSQL, dbfailonerror
    On the other hand, if you have a parameter query that you want to show on click, then you would set the parameter in the query to Forms![frmSurveyInput]!cboTableSelect . Then you just have to make sure that form is open to run the query. Details in Everything About Using Parameters from Code.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Originally posted by ChipR
      Code:
      strSQL = "Update " & cboTableSelect & " SET ..."
      For unspecified table names, I'd use :
      Code:
      strSQL = "Update [" & cboTableSelect & "] SET ..."
      It's ok to leave the brackets ([]) out when you already know the name has only valid characters. Otherwise, it's safer to use them.

      Comment

      • ChipR
        Recognized Expert Top Contributor
        • Jul 2008
        • 1289

        #4
        Thanks NeoPa, I see what you mean, and I'll be sure to do that in the future.

        Comment

        • artemetis
          New Member
          • Jul 2007
          • 76

          #5
          I must be missing something? No worky

          I'm assuming (I know...I know) that this was to be the event for the button?
          Here's the code from the Form:

          Code:
          Private Sub cmdImportSurvey_Click()
          Dim strSQL As String
          strSQL = "UPDATE tblMain AS A INNER JOIN [" & cboTableSelect & "] AS E ON A.empId=E.[Employee Id] SET A.empReview= E.[Employee Review], so on and so on.........;"
           
          End Sub

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            The SQL seems ok from what you've shown. There appears to be no code executing it in your version however. Is it as simple as that? Or have you just left that out as irrelevant (Not a good idea ;) )?

            Comment

            • artemetis
              New Member
              • Jul 2007
              • 76

              #7
              Originally posted by NeoPa
              The SQL seems ok from what you've shown. There appears to be no code executing it in your version however. Is it as simple as that? Or have you just left that out as irrelevant (Not a good idea ;) )?
              Yeah, this is where I lost myself....I don't know how to execute that with the cmdButton.....i s there a DoCmd?

              Comment

              • Dan2kx
                Contributor
                • Oct 2007
                • 365

                #8
                Code:
                DoCmd.RunSQL strSQL
                'OR JUST
                DoCmd.RunSQL "SELECT.........."

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  There is also the "CurrentDB.Exec ute" method of course (as found in ChipR's post #2).
                  Check out the Help section for each to see what different parameters each presents.

                  Comment

                  • artemetis
                    New Member
                    • Jul 2007
                    • 76

                    #10
                    Originally posted by Dan2kx
                    Code:
                    DoCmd.RunSQL strSQL
                    'OR JUST
                    DoCmd.RunSQL "SELECT.........."
                    Thanks to all for the assistance!!!
                    This did it.......

                    BTW - Did I say how much I LOVE this site???

                    Such nice people.....you all are the best!

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32669

                      #11
                      We're glad to help Art :)

                      Very pleased to hear how happy you are with everything.

                      Comment

                      Working...