• Modify a query in code (Access 97/XP)

    Author
    Topic
    #381892

    Hey All!

    I have a rather strange one that I’m working on. Can a query be modified in code? Now I don’t mean create the whole query, or work with the SQL. What I’m trying to do is similar to the way you can append fields to a table and then modify their properties. Here is the example from Access 97’s help:

    Sub NewField()
    Dim dbs As Database, tdf As TableDef
    Dim fld As Field
    ‘ Return reference to current database.
    Set dbs = CurrentDb
    ‘ Return reference to Employees table.
    Set tdf = dbs.TableDefs!Employees
    ‘ Create new Field object.
    Set fld = tdf.CreateField(“SSN#”)
    ‘ Set Type and Size properties of Field object.
    fld.Type = dbText
    fld.Size = 11
    ‘ Append field.
    tdf.Fields.Append fld
    Set dbs = Nothing
    End Sub

    What I’d like to do is exactly this, but except for a table, do it for a query. But not just a query, an Append query. So I need to be able to add the “field” to the design grid, then set its properties (ie. Append To, etc).

    Has anyone done anything like this successfully? In the mood for a challenge?

    TIA!

    Cecilia
    I can do it, but unsuccessfully *g*

    Viewing 1 reply thread
    Author
    Replies
    • #645433

      I don’t think you can do this type of thing to a query, but I could be wrong, have been before.
      What I would do is to use the CreateQueryDef to create the query from a SQL you have built in code, it’s not hard.
      Pat cheers

    • #645562

      The only way I know of to do this is by creating a new query (in code) or by replacing the SQL of the existing query. In either case, you have to build the SQL for the whole query, not just somehow append a field to the query object. Sorry, but queries aren’t the same kind of objects that tables are, so you can’t do it the way you want to.

      • #645782

        Thanks, Charlotte.

        The help in Access 97 appeared to indicate that you could. The help in XP has a chart that clearly shows you can’t. Big time bummer for me.

        • #645921

          According to the DAO help in Access 97, the CreateField method doesn’t apply to a QueryDef object, and Append isn’t a method of the Fields collection of a QueryDef, as shown by the attached screenshot.

    Viewing 1 reply thread
    Reply To: Modify a query in code (Access 97/XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: