473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to create a code for a button in order to show the results of a querry?

Hello, i am new with access vba, and i have a certain question about a specific code.
I want to create a list of buttons on a form (or one button),that each one represent a pollutant of the atmosfere.This is the easy part.Now i want to create a code, that by clicking on one of these pollutants (aka on one of these buttons)access creates a box on the same form which shows the results that correspond to each pollutant.
The results are querries for each pollutant, and by clicking on NO2 for example it will show the results from the NO2 querry on a box .
Here is the coding i have done so far, which basically is only the action when clicking on the pollutants button that opens the querry.


Private Sub button17_GotFocus()
DoCmd.OpenQuery "Metrics CO"
End Sub

Private Sub button18_GotFocus()
DoCmd.OpenQuery "Metrics NO2"
End Sub

Private Sub button19_GotFocus()
DoCmd.OpenQuery "Metrics NOX"
End Sub

Private Sub button20_GotFocus()
DoCmd.OpenQuery "Metrics O3"
End Sub

I dont want access to open the querries but display instead a box with the results from the querries.
Jan 11 '11 #1
1 1649
Stewart Ross
2,545 Expert Mod 2GB
(1) Place an unbound textbox on your form (that is, one whose Control Source property is blank).

(2) Rename this unbound textbox to something meaningful, such as txtResult

(3) In place of the DoCmd.OpenQuery statements set the textbox value to the result of a DLookup on each query. You will need to provide the name of the field from each query that contains the result, which you don't tell us in your post. I will call it QueryResult below, but you must supply the correct name yourself in the DLookup statement or it will not work. Also, it is normal to place such code in the On Click event of a button, not the Got Focus event.

(4) Give the label of the textbox a suitable name such as lblResult and set its initial value to, say, "Result" beside the textbox.

(5) Change the label's Caption property in each of the OnClick events to give the user a meaningful clue as to what result is being shown:

Expand|Select|Wrap|Line Numbers
  1. Private Sub button17_OnClick()
  2.   Me!txtResult = DLookup("[QueryResult]", "[Metrics CO]")
  3.   Me!lblResult.Caption = "CO Result"
  4. End Sub
  5.  
  6. Private Sub button18_OnClick()
  7.   Me!txtResult = DLookup("[QueryResult]", "[Metrics NO2]")
  8.   Me!lblResult = "NO2 Result"
  9. End Sub
  10.  
  11. ...
Clearly you need to ensure that the names you use in your own versions match the names you give to the textbox, its label, and the DLookup elements or the above example may not compile at all, or may give an immediate run-time error if the DLookup can't find what it is being asked to look up.

-Stewart
Jan 14 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Keith Smith | last post by:
What am I missing? I am trying to dynamically create a button... Button myButton = new Button(); myButton.Name="ButtonX";
3
by: Mark Goldin | last post by:
I need to show results of transformation on the server. Thansformation returns an attribute based xml string. Can I bind DataRepeater to the xml string? Thanks
4
by: Tim Wallace | last post by:
I have an ascx file in which I want to sometimes create an asp:button control based on a given value. Nothing I've tried has worked. How can I accomplish this task? Tim
4
by: Nathan Pinno | last post by:
Hey all, Is there a way to create a button in either pygame or livewires, that is able to be clicked and when clicked sends a command to restart the program? Thanks, Nathan Pinno -- For...
4
by: Adam - Regus | last post by:
I'm trying to create a button that prints 2 copies of a form. Using the wizard, I created a button that prints one automatically, using the following code: End Sub Private Sub...
3
by: simbat | last post by:
hello guys, i am experiencing some difficulty making a full screen button for my streaming video. i have the following code in my page. but it doesn't seem to work. if you have a code thats working...
3
by: oozy | last post by:
Hi, I'm currently using Page.Form.DefaultButton = ChangePassword1.FindControl("xx").UniqueID to set my DefaultButtons in a form that has multiple wizards(each in it's own panel). I can...
2
by: peterv6 | last post by:
I want to create a button on my page that when clicked, will take me to my homepage. Using just a link on the page works <a href="index.html">Main Page</a , but I want to use a button. ...
0
by: cindrie | last post by:
please correct my code using boa constructor ------------frame1--------------- #Boa:Frame:Frame1 import wx import Frame2 import Frame3
3
by: Diana Miller | last post by:
Hi All, I am creating a report from Access 2007. I have a form where the user selects a Month from a combo box and then presses a button to show the sum of all pages worked for the Month selected...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.