Creating query based on form

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

    Creating query based on form

    I am creating a contact database and I would like the user to create
    his own groups of contacts based on criterias, thefore I would like to
    let him pick the fields and criteria for a particular group. I don't
    wanna a form full of fields that ask the user for arguments. Instead a
    subform with a combobox where the user picks the [field], the
    [operator], and the [arguement]

    So I would have two tables groups and GroupCriteria for instance:

    group field operator criteria
    _______________ _______________ ______

    Accounts city = brooklyn

    Accounts status < active


    I then would live to each record to supply the query with these
    arguements. Is the wrong way to go about it? How can I create my own
    arguement. I see examples on how to pass but how do I create it using
    code?

  • Kaspa

    #2
    Re: Creating query based on form

    this is the function that I am thinking might do it:

    Function getsource(group )

    End Function
    Dim rec As DAO.Recordset
    Dim db As DAO.Database
    Set db = CurrentDb()
    Dim sql As String
    Dim qdf As QueryDef
    Dim strsql As String
    Dim fieldn As String
    Dim oper As String
    Dim crit As String
    Dim surplus As String

    Set rec = db.OpenRecordse t("select * from tblGroupCriteri a where
    groupid = " & group)

    strQueryName = "qryTemp"
    If ((rec.BOF) And (Not rec.EOF)) Then
    strsql = "SELECT * from tblIndividuals"
    Set qdf = db.CreateQueryD ef(strQueryName , strsql)
    Exit Function
    End If

    strsql = "SELECT * from tblIndividuals where "
    With rec
    If Not .EOF Then
    fieldn = !FieldName
    oper = !Operator
    crit = !Criteria
    End If
    surplus = fieldn & " " & oper & " " & crit & "and"
    strsql = strsql & surplus

    Do While Not .EOF
    fieldn = !FieldName
    oper = !Operator
    crit = !Criteria
    surplus = "and" & fieldn & " " & oper & " " & crit
    strsql = strsql & surplus
    Loop
    End With

    End Function

    Comment

    • pietlinden@hotmail.com

      #3
      Re: Creating query based on form


      Kaspa wrote:
      I am creating a contact database and I would like the user to create
      his own groups of contacts based on criterias, thefore I would like to
      let him pick the fields and criteria for a particular group. I don't
      wanna a form full of fields that ask the user for arguments. Instead a
      subform with a combobox where the user picks the [field], the
      [operator], and the [arguement]
      >
      So I would have two tables groups and GroupCriteria for instance:
      >
      group field operator criteria
      _______________ _______________ ______
      >
      Accounts city = brooklyn
      >
      Accounts status < active
      >
      >
      I then would live to each record to supply the query with these
      arguements. Is the wrong way to go about it? How can I create my own
      arguement. I see examples on how to pass but how do I create it using
      code?
      off the top of my head (do I ever do anything any other way?), You
      could open a recordset of all the records for a specific group, e.g.
      SELECT Group, Field, Operator, Criteria
      FROM GroupCriteria
      WHERE group = <point at a form here?>

      Then you could simply walk through the recordset...

      do until rs.EOF
      strFilter = strFilter & " AND " &
      rs.Fields("Fiel d") & rs.Fields("Oper ator") & fDelimiter(fiel dType)
      & rs.Fields("Valu e") & fDelimiter(fiel dType)

      rs.MoveNext

      Loop

      or you could save these as different queries (just not let the user see
      them)...

      Comment

      • pietlinden@hotmail.com

        #4
        Re: Creating query based on form

        Kaspa,

        I was thinking along the same lines (doesn't make it right, mind you).
        One thing I thought about, though, is delimiters - stuff that the QBE
        grid puts in there for you. (so maybe if you build querydefs on the
        fly, you may not need them). you might want to check field types so
        that you're sure you include delimiters for literal values, like dates
        (#), strings ('), etc.

        I guess if you wait for the other folks here to sleep and have coffee,
        you'll get more answers.

        but that's my two cents... at a discount.

        Pieter

        Comment

        • Kaspa

          #5
          Re: Creating query based on form

          thank you for that, I was not think about it.


          pietlinden@hotm ail.com wrote:
          Kaspa,
          >
          I was thinking along the same lines (doesn't make it right, mind you).
          One thing I thought about, though, is delimiters - stuff that the QBE
          grid puts in there for you. (so maybe if you build querydefs on the
          fly, you may not need them). you might want to check field types so
          that you're sure you include delimiters for literal values, like dates
          (#), strings ('), etc.
          >
          I guess if you wait for the other folks here to sleep and have coffee,
          you'll get more answers.
          >
          but that's my two cents... at a discount.
          >
          Pieter

          Comment

          • Kaspa

            #6
            Re: Creating query based on form

            Anybody else has any input?
            pietlinden@hotm ail.com wrote:
            Kaspa wrote:
            I am creating a contact database and I would like the user to create
            his own groups of contacts based on criterias, thefore I would like to
            let him pick the fields and criteria for a particular group. I don't
            wanna a form full of fields that ask the user for arguments. Instead a
            subform with a combobox where the user picks the [field], the
            [operator], and the [arguement]

            So I would have two tables groups and GroupCriteria for instance:

            group field operator criteria
            _______________ _______________ ______

            Accounts city = brooklyn

            Accounts status < active


            I then would live to each record to supply the query with these
            arguements. Is the wrong way to go about it? How can I create my own
            arguement. I see examples on how to pass but how do I create it using
            code?
            >
            off the top of my head (do I ever do anything any other way?), You
            could open a recordset of all the records for a specific group, e.g.
            SELECT Group, Field, Operator, Criteria
            FROM GroupCriteria
            WHERE group = <point at a form here?>
            >
            Then you could simply walk through the recordset...
            >
            do until rs.EOF
            strFilter = strFilter & " AND " &
            rs.Fields("Fiel d") & rs.Fields("Oper ator") & fDelimiter(fiel dType)
            & rs.Fields("Valu e") & fDelimiter(fiel dType)
            >
            rs.MoveNext
            >
            Loop
            >
            or you could save these as different queries (just not let the user see
            them)...

            Comment

            Working...