Passing variable value from code to query criteria?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Regnab

    #1

    Passing variable value from code to query criteria?

    How can I pass a variable (Dim Property As String) to the query
    criteria? I figure I could do it the long way and send it to a form and
    have the query access it from there but I figure there has gotta be an
    easier way?

  • pietlinden@hotmail.com

    #2
    Re: Passing variable value from code to query criteria?

    You can't do it directly, but you can specify a function as a query
    criterion.
    SELECT...
    FROM ...
    WHERE Table1.Field1=M yFunction(arg1, arg2...)

    Comment

    • Regnab

      #3
      Re: Passing variable value from code to query criteria?

      Sounds good but unfortunatly I haven't used them before. Could you just
      expand a little on how to use it. I think I can get it to call the
      function from the query but not sure how to set it up to pick up the
      owner code from my code. Currently have a public variable
      "PropertyCo de", which I then use in my function....

      Function GetPropertyCode () As String

      GetPropertyCode = PropertyCode

      End Function

      Public PropertyCode As String

      "PropertyCo de" is set in my sub routine before I call the query... what
      else do I need to change...

      Thanxs....

      Comment

      • pietlinden@hotmail.com

        #4
        Re: Passing variable value from code to query criteria?

        Okay, so you add GetPropertyCode to your query as a criterion or as an
        expression...

        SELECT ... GetPropertyCode () AS Whatever
        FROM SomeTable
        WHERE...

        Comment

        • Regnab

          #5
          Re: Passing variable value from code to query criteria?

          Don't quite understand that last bit.... how can I select it from a
          table when it's from code?? The current code I have is:

          SELECT Herd_Animals.Ow ner, Herd_Animals.An imal_Tag, Herd_Animals.Gr ade,
          Herd_Animals.Se x_Code, Herd_Animals.Da te_Of_Birth,
          Herd_Animals.Gr oup2_Code INTO QTable1
          FROM Herd_Animals
          WHERE (((Herd_Animals .Owner)=GetProp ertyCode()) AND
          ((Herd_Animals. Date_Of_Birth)> =[Forms]![Form1]![TxtStartDate] And
          (Herd_Animals.D ate_Of_Birth)<=[Forms]![Form1]![TxtEndDate]));

          Again, I haven't played around with SQL much before. I realise this is
          fairly basic...

          Comment

          Working...