Closing query in a macro if blank

tim Alliance

New Member
Joined
Sep 27, 2004
Messages
6
Hi

I am writing a database, and wish to run an autoexec macro.

This will open various queries, which will test the integrity of the data.

If the query result is blank, then I want the macro to close the query, if it is not blank then the query result should remain on the screen to alert the user.

I would rather do this using the macro writer list than visual basic if possible.

Anyone help please (surely this is possible)

Thanks

Tim
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
surely this is possible

ASFAIK not without VBA.

How are you going to test if a query is 'empty' or not?

What do you actually mean by 'empty'?
 
Upvote 0
By blank, I mean no fields returned in a select query.

So for example, I want to test that of say £100,000 we have been given to spend over 4 years, that I have allocated exactly £100,000 over those years.

So I have a query that has budget total, and then compares it to how the budget has been allocated over time.

Hope this is clearer

Cheers

Tim
 
Upvote 0
As Norie suggested, it can be done with VBA, but like him, I cannot imagine how to do it without VBA. I don't believe that macros are advanced enough to do it.
 
Upvote 0
Blight !

OK, thanks,

Do you have the "duffers guide" on how to do this in VBA, my knowledge is pretty ropey ?

Thanks again

Tim
 
Upvote 0
Do the queries already exist?

If so do they have similar names?

Are they the only queries in the database?

Do the queries expect particular parameters?

Can you explain further exactly what you want to do?
 
Upvote 0
I have a database, of donors, who have promised my organisation money over so many years.

This money is then split over variosu teams to spend.

I need to test the integrity of the data such that

Fro each donor, all the pledged money is allocated to a year, and for each year that money is allocated over time.

So I have a donor table, a table with the donor, years and the amount allocated to that year. Then a table that has each donor/year and gives what percentage fo that money is allocated to a particular team.

For example Donor A gives £100,000

Allocated

2004 30,000
2005 40,000
2006 30,000

This is allocated

2004 Africa Team 80% Asia Team 20%
2005 Africa Team 100%
2006 Americas Team 50% Asia Team 50%

I need to check that the first list adds to £100,000, and that for each year that there is an allocated amount, I have allocated 100% of that money to teams.

Due to "events", these allocations change over time, so I can not do it as a one off.

Hope this is clearer

Cheers

Tim
 
Upvote 0
Hi Tim

This can be done but you will need to do it with a combination of queries and forms in your autoexec macro.

I managed to get my autoexec macro to take 2 differing paths depending on whether or not it found any differences between the total pledge and the annual pledges. This is how I did it :

I created a query that calculated the differences between the total pledge and the annual amounts, by donor. This query sums the annual amounts and then you can compare the total of the annual amounts to the initial pledge (per donor) and then filter out any differences that are <>0 with the following :

1st Query :
New Query, Add the 2 Tables (with the initial pledge and annual amounts), View Totals
Field 1 = Donor ID (or whatever variable name you have used, per first table)
Field 2 = Total Pledge (per first table)
Field 3 = Amt: Sum(IIf(IsNull([Amount]),0,[Amount])) - where "amt" is the amount allocated to each year per your 2nd table - don't forget to change the "Total" field to "Expression" and to use your variable name
Field 4 = Diff: [Pledge]-Sum(IIf(IsNull([Amount]),0,[Amount])) - set the criteria to <> 0 and change the "Total" to "Expression".
Save

This query will give you the donors where the total pledge <> the sum of the annual amounts. However, you can't use this successfully with a form and macro (I tried and failed) so you need a 2nd query, as follows :

2nd Query :
Create a 2nd query, add the first query to the 2nd query, view totals.
Field 1 = Donor, change "Total" to "Count".
Save

This 2nd query will count the number of donors where the initial pledge <> the sum of the annual amounts.

Create a new form, based on the 2nd query - add the one and only field being "CountofDonor".
Save

{There may be a quicker way of doing this - I'll leave that for the experts}

In your autoexec macro :
View Conditions
Action 1 : no condition, open the form (I used F_Count]
Action 2 : Condition = [Forms]![F_Count]![CountofDonor]=0, Action = Close the form
Action 3 : Condition = [Forms]![F_Count]![CountofDonor]=0, Action = Stop Macro
Action 4 : Stop Macro.

Save and Run.

This macro will keep the form open if there are >0 donors and close it if all is ok. I realise this isn't the exact solution you were looking for but it shows that what you are seeking can be done with queries (& forms!) and macros. To add more lines to your macro under the first condition, insert some new lines between actions 2 and 3 - but remember to copy the condition for each line. There is a shortcut to avoid re-typing the entire condition (3 periods I think) but I can't remember it at the moment.

You can also set other conditions under the null response between actions 3 and 4 (e.g. close the count form, open a data entry form showing the amounts to be fixed etc.) - for this you don't need any conditions.

You can then repeat this entire exercise for the 2nd test (i.e. regional annual allocations per donor <>1 or <>100%) - however, you might want to start the 2nd test before you "stop the macro" under the first condition on the first macro. (Getting too late to explain this last paragraph fully but I'm sure you know what I mean - just ask if you're not sure and I'll check again here in 12 hours time).

HTH, Andrew. :)

P.S. A couple of minor typo's were edited.
 
Upvote 0
BTW before I get knocked for making an obvious error, you can't actually close the form (that counts the records) where the condition for the command or action to close the form is dependant upon the form itself (i.e. the macro that I presented above won't work as I have presented it literally - my test used message boxes).

However, a way around this given the "count" forms are only used for testing (not reporting or for data entry) is to leave all of the count forms open (and possibly hidden?) until the very end of the autoexec macro whereupon you no longer need them and you can close all of the count forms (irrespective of the values on the form).

The point being is that you can get the autoexec macro to take differing paths based on a value in a query (albeit presented in a form), which was your original question and you can use the differing paths to open data forms (or queries) for the user (or not).

Andrew. :)
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,135
Members
451,744
Latest member
outis_

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top