LauraLee: Fear not. There are no real dumb questions in this forum as long as you are trying to learn something. I will try to explain it to you.
Here is the ACCESS definition from the Help files:
A QueryDefs collection contains all QueryDef objects of a Database object in a Microsoft Jet database, and all QueryDef objects of a Connection object in an ODBCDirect workspace.
Now what this means is that in your database every saved query is a QueryDef object in the QueryDef collection. So, the query that you are trying to modify is in the QueryDef collection as an object with a name. We have dimensioned the current database as an Alias "db". Then we are making a reference to the db's QueryDef collection and specifically the saved query that you named "whatever". That object has a property called .SQL which is the actual Structured Query Language code generated when you created your query in the Query Design Window. If you have never seen this code just open the query in design and click the left button at the top and select SQL. You will now be looking at the code that we are trying to programmatically modify with the code that I provided you.
Now how does the code work? Let's use an example.
Code:
[red]Select A.Name, A.Address, A.City, A.State, A.Zip, A.Amount
FROM Customers as A
WHERE [/red]A.Amount >= 100;
The red portion of the code we do not want to change because that should be static. The expression behind the WHERE clause needs to be modified each time you run it.
What we want to do with the code is manipulate a large string of characters. First, we want to preserve the beginning of the string from character 1 thru the 5th character after the word "WHERE". The below green code does that.
Code:
db.QueryDefs("YourQueryName").SQL = [green]Mid$(db.QueryDefs("YourQueryName").SQL, 1, Instr(1,db.QueryDefs("YourQueryName").SQL, "WHERE")+ 5)[/green] & " [YourFieldName] " & FORMS![YourFormName]![cboYourComboName] & ";"
This code looks at the SQL string of the query and using a combination of Mid$(mid-string) and Instr(instring) functions selects characters 1 thru the 5th character past the beginning of the word WHERE or:
Select A.Name, A.Address, A.City, A.State, A.Zip
FROM Customers as A
WHERE
Now we want to cancatenate(add to this string of characters) the new expression to select the city. So using the ampersand(&) we add the name of the field
& " A.[Amount] " Then adding on the value from the forms combobox by referencing it with the long reference.
& "FORMS![formname]![comboboxname]". Then we add on the closing semicolon
& ";"
After all of this, this SQL string is assigned in the expression to the
db.QueryDef.SQL =
I hope that I have not confused you with all of this. If you have any questions please feel free to post back.
Bob Scriver
[blue]Want the best answers? See FAQ181-2886[/blue]