Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

macro to remove field and restore original qry

Posted on 2013-12-04
7
Medium Priority
?
284 Views
Last Modified: 2013-12-04
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..
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

Expert Comment

by:mbizup
ID: 39695569
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?
0
 

Author Comment

by:route217
ID: 39695606
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????
0
 
LVL 61

Accepted Solution

by:
mbizup earned 1000 total points
ID: 39695723
Here's what I would suggest doing...

Create a form, default view Datasheet view use your query (with ALL the fields) as its recordsource, using textboxes bound to each of the fields.

If you want two separate views, with and without the region, you can do something like this:


Private sub cmdShowAll_Click()
       Docmd.OpenForm "YourFormName"
End Sub

Open in new window



Private sub cmdShowWithoutRegion_Click()
       Docmd.OpenForm "YourFormName"
       Forms!YourFormName!txtYourRegion.Visible = false
End Sub

Open in new window

0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 total points
ID: 39695730
i'll repost my comment from the previous thread


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

Expert Comment

by:mbizup
ID: 39695748
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.
0
 
LVL 61

Expert Comment

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

0
 

Author Comment

by:route217
ID: 39695775
Thanks experts
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

704 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