Solved

macro to run from cmd button to include extra field

Posted on 2013-12-04
7
280 Views
Last Modified: 2013-12-04
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
0
Comment
Question by:route217
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39695309
Probably simpler to just create the whole query in code.  See the following for info/sample code using querydefs:
http://msdn.microsoft.com/en-us/library/office/bb177500(v=office.12).aspx


Your code might look something like this:


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

Open in new window



but better... create a form in datasheet view, including any needed fields,  and change the recordsource dynamically like this:

Sub CreateQuery()
     dim strSQL as string
     dim qdf as QueryDef

     strSQL = "SELECT Field1, Field2, Region FROM YourTable WHERE core = '" & me.txtCore & "' ORDER BY [GroupBy]"
      

     docmd.OpenForm "frmMyFormName"
     forms!MyformName.Recordsource = strSQL
end sub

Open in new window

0
 

Author Comment

by:route217
ID: 39695381
Mbizup...

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?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39695397
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
      

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:route217
ID: 39695408
Sorry other condition was if I wanted to remove the field region..via vba
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39695504
Not 100%clear what you are asking for, but try this:


' 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

Open in new window


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

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 39695510
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,
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39695518
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question