Avatar of route217
route217
Flag for United Kingdom of Great Britain and Northern Ireland asked on

macro to remove field and restore original qry

Hi Experts

need a macro that will remove the field region  from the following vba code


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

and restore the qry back to its original form..
Microsoft Access

Avatar of undefined
Last Comment
route217

8/22/2022 - Mon
mbizup

As I mentioned in your previous question - I don't think this is the best/most efficient way of doing this.

What are you ultimately using these queries for?
Why not just select the fields you need from your table when and where you need them rather than building on and reverting your query to its original state?
route217

ASKER
Hi mbizup

using the qry to populate figures.....year end...

i understand what u are saying but the end users have no working knowledge of access. ..very limited. ..so hence the vba route they are happy to click buttons. .

so would strsql = DELETE * FROM qry xxxx WHERE Region
do????
ASKER CERTIFIED SOLUTION
mbizup

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mbizup

The reason I'm suggesting the form approach is that it is more standard, generally considered 'best practice' to show the data through a form interface rather than directly displaying tables or queries (it gives you more control).  

That way, your users can see the data they want at the click of a button, and you as the developer are working with a single form and a single query, which have plenty of possibilities for *easily* displaying the data in a wide variety of ways.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mbizup

Your criteria can also be easily handled like this.

Assuming you have a textbox on your form for CORE, set the SQL of your query to include all fields, but NOT the where clause:

SELECT Field1, Field2, Region FROM YourTable

Then this will display all fields with the core criteria:

Private sub cmdShowAll_Click()
       Docmd.OpenForm "YourFormName", WhereCondition := "Core = '" & me.txtCore & "'"
End Sub

Open in new window


Then this will display without the region and without the core criteria
Private sub cmdShowAll_Click()
       Docmd.OpenForm "YourFormName"
       Forms!YourFormName.txtRegion.Visible = False
End Sub

Open in new window

route217

ASKER
Thanks experts