route217
asked on
macro to run from cmd button to include extra field
Hi Experts
Need a macro when the command button cmdarea is clicked..it insert into the select qry the field "region" to the far right and sort by groupby and add the criteria "core"and run..To show the output.
Qry name:-
qry salesbyarea
Need a macro when the command button cmdarea is clicked..it insert into the select qry the field "region" to the far right and sort by groupby and add the criteria "core"and run..To show the output.
Qry name:-
qry salesbyarea
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are there some conditions where you want the field and other conditions where you dont? If so, just include them in the code and add the field accordingly:
Sub CreateQuery()
dim strSQL as string
dim qdf as QueryDef
' For example, if you have a 'Use region' checkbox:
If chkUseRegion = true then
strSQL = "SELECT Field1, Field2, Region FROM YourTable WHERE core = '" & me.txtCore & "' ORDER BY [GroupBy]"
else
strSQL = "SELECT Field1, Field2 FROM YourTable WHERE core = '" & me.txtCore & "' ORDER BY [GroupBy]"
End if
' etcetera
ASKER
Sorry other condition was if I wanted to remove the field region..via vba
Not 100%clear what you are asking for, but try this:
' To create a query including region
To go back to not including region
' To create a query including region
Sub CreateQuery()
dim strSQL as string
dim qdf as QueryDef
strSQL = "SELECT Field1, Field2, Region FROM YourTable WHERE core = '" & me.txtCore & "' ORDER BY [GroupBy]"
set qdf = Currentdb.CreateQueryDef("qryMyQuery", strSQL)
docmd.OpenQuery "qryMyQuery"
end sub
To go back to not including region
Sub RevertQuery()
dim strSQL as string
dim qdf as QueryDef
' Delete the query
currentdb.QueryDefs.Delete "qryMyQuery"
' Recreate it without Region
strSQL = "SELECT Field1, Field2 FROM YourTable WHERE core = '" & me.txtCore & "' ORDER BY [GroupBy]"
set qdf = Currentdb.CreateQueryDef("qryMyQuery", strSQL)
docmd.OpenQuery "qryMyQuery"
end sub
Finally, I'm not sure how you intend to use this... and it is a good academic exercise... but I am almost certain that this is NOT the optimal way to build your database,
to preserve the original sql statement you have to store it in a variable and used it back when your done with adding/altering the original sql statement
dim oSql as string, qd as dao.querydef, db as dao.database
set db=currentdb
set qd=db.querydefs("qry salesbyarea")
oSql=qd.sql
'do your thing here
strSql="select..... <with added> fields ..etc.."
qd.sql=strSQl
'now return the original sql statement
qd.sql=oSql
dim oSql as string, qd as dao.querydef, db as dao.database
set db=currentdb
set qd=db.querydefs("qry salesbyarea")
oSql=qd.sql
'do your thing here
strSql="select..... <with added> fields ..etc.."
qd.sql=strSQl
'now return the original sql statement
qd.sql=oSql
ASKER
excellent feedback....as always...
just one last point if I was to revert the query back to its original state I.e. remove the region field....what would the vba be?