473,385 Members | 1,620 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,385 software developers and data experts.

Have a query prompt for input on different fields based on which button selected on

Using Access 2007, I have a form with 3 buttons. The OnClick event sets a global variable and each of them opens another form whose record source is the following query.

Expand|Select|Wrap|Line Numbers
  1. SELECT <several fields here> 
  2.  
  3. FROM ([tblEmployee Information] INNER JOIN [tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID
  4.  
  5. WHERE Iif(CurrentTrainingType()=1,(qryLawsonQueryByArea.[Next First Aid/CPR]<=Date()+[How Many Days Out?]),Iif(CurrentTrainingType()=2,(qryLawsonQueryByArea.[Next Recert]<=Date()+[How Many Days Out?]),(Date()+[How Many Days Out?])))
  6.  
  7. ORDER BY qryLawsonQueryByArea.Name;
The function CurrentTrainingType returns the value of the global variable set in the OnClick event procedure.

Instead of getting only 1 prompt when any of the buttons is clicked, based on that variable, the query prompts for all 3.

Any ideas how to rectify?
Aug 26 '11 #1

✓ answered by ADezii

  1. Prompt the User in some manner in order to obtain the Number of Days Out. For this Demo, I used the InputBox() Function, but you can just as easily use a Text Box, Combo Box, etc. (Line #7).
  2. Perform Validation on the Input (Line #10, 11), making sure that:
    1. Is is Not Null
    2. It is Numeric
    3. It is within a Specified Range
  3. Define your SQL String based on the Input Parameter (Number of Days Out) (Line #14 - 19).
  4. We will dynamically be creating a Query named qryTest, but we must first see if it already exists. If it exists, then DELETE it (Line #22 - 27).
  5. Create a Query whose Name is defined by a Constant (conQUERY_NAME), and whose SQL was already defined in a String strSQL (Line #30 - 32).
  6. The anticipation is killing me, so let's Open the Query and see the Results (Line #35)!
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim qdf As DAO.QueryDef
  3. Dim qdfNew As DAO.QueryDef
  4. Dim varDaysOut As Variant
  5. Const conQUERY_NAME As String = "qryTest"       'Substitute if you like
  6.  
  7. varDaysOut = InputBox("Enter the Number of Days Out", "Days Out Prompt")
  8.  
  9. 'The Number of Days out cannot be NULL, must be Numeric, and cannot be < 1 or > 50
  10. If IsNull(varDaysOut) Or Not IsNumeric(varDaysOut) Or varDaysOut < 1 Or _
  11.                          varDaysOut > 50 Then Exit Sub
  12.  
  13. 'Define the SQL String
  14. strSQL = "SELECT <several fields here> FROM ([tblEmployee Information] INNER JOIN " & _
  15.          "[tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) " & _
  16.          "INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID " & _
  17.          "WHERE Iif(CurrentTrainingType()=1,(qryLawsonQueryByArea.[Next First Aid/CPR]<=Date()+" & _
  18.          varDaysOut & "),Iif(CurrentTrainingType()=2,(qryLawsonQueryByAre a.[Next Recert]<=Date()+" & _
  19.          varDaysOut & "),(Date()+" & varDaysOut & "))) ORDER BY qryLawsonQueryByArea.Name;"
  20.  
  21. 'If the Query conQUERY_NAME exists, DELETE it
  22. For Each qdf In CurrentDb.QueryDefs
  23.   If qdf.name = conQUERY_NAME Then
  24.     CurrentDb.QueryDefs.Delete conQUERY_NAME
  25.       Exit For
  26.   End If
  27. Next
  28.  
  29. 'Dynamicall Create the Query conQUERY_NAME
  30. With CurrentDb
  31.   Set qdfNew = .CreateQueryDef(conQUERY_NAME, strSQL)
  32. End With
  33.  
  34. 'Open the Query conQUERY_NAME, and see the Results
  35. DoCmd.OpenQuery conQUERY_NAME, acViewNormal, acReadOnly
P.S. - Obviously, I have no way of actually testing the SQL Statement, so I'll leave that up to you. The Logic, however, has been tested and does appear to be sound.

4 1526
ADezii
8,834 Expert 8TB
Just subscribing for now, must put thinking cap on! (LOL)
Aug 27 '11 #2
ADezii
8,834 Expert 8TB
  1. Prompt the User in some manner in order to obtain the Number of Days Out. For this Demo, I used the InputBox() Function, but you can just as easily use a Text Box, Combo Box, etc. (Line #7).
  2. Perform Validation on the Input (Line #10, 11), making sure that:
    1. Is is Not Null
    2. It is Numeric
    3. It is within a Specified Range
  3. Define your SQL String based on the Input Parameter (Number of Days Out) (Line #14 - 19).
  4. We will dynamically be creating a Query named qryTest, but we must first see if it already exists. If it exists, then DELETE it (Line #22 - 27).
  5. Create a Query whose Name is defined by a Constant (conQUERY_NAME), and whose SQL was already defined in a String strSQL (Line #30 - 32).
  6. The anticipation is killing me, so let's Open the Query and see the Results (Line #35)!
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim qdf As DAO.QueryDef
  3. Dim qdfNew As DAO.QueryDef
  4. Dim varDaysOut As Variant
  5. Const conQUERY_NAME As String = "qryTest"       'Substitute if you like
  6.  
  7. varDaysOut = InputBox("Enter the Number of Days Out", "Days Out Prompt")
  8.  
  9. 'The Number of Days out cannot be NULL, must be Numeric, and cannot be < 1 or > 50
  10. If IsNull(varDaysOut) Or Not IsNumeric(varDaysOut) Or varDaysOut < 1 Or _
  11.                          varDaysOut > 50 Then Exit Sub
  12.  
  13. 'Define the SQL String
  14. strSQL = "SELECT <several fields here> FROM ([tblEmployee Information] INNER JOIN " & _
  15.          "[tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) " & _
  16.          "INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID " & _
  17.          "WHERE Iif(CurrentTrainingType()=1,(qryLawsonQueryByArea.[Next First Aid/CPR]<=Date()+" & _
  18.          varDaysOut & "),Iif(CurrentTrainingType()=2,(qryLawsonQueryByAre a.[Next Recert]<=Date()+" & _
  19.          varDaysOut & "),(Date()+" & varDaysOut & "))) ORDER BY qryLawsonQueryByArea.Name;"
  20.  
  21. 'If the Query conQUERY_NAME exists, DELETE it
  22. For Each qdf In CurrentDb.QueryDefs
  23.   If qdf.name = conQUERY_NAME Then
  24.     CurrentDb.QueryDefs.Delete conQUERY_NAME
  25.       Exit For
  26.   End If
  27. Next
  28.  
  29. 'Dynamicall Create the Query conQUERY_NAME
  30. With CurrentDb
  31.   Set qdfNew = .CreateQueryDef(conQUERY_NAME, strSQL)
  32. End With
  33.  
  34. 'Open the Query conQUERY_NAME, and see the Results
  35. DoCmd.OpenQuery conQUERY_NAME, acViewNormal, acReadOnly
P.S. - Obviously, I have no way of actually testing the SQL Statement, so I'll leave that up to you. The Logic, however, has been tested and does appear to be sound.
Aug 27 '11 #3
It works great. Thanks for your help.
Aug 29 '11 #4
ADezii
8,834 Expert 8TB
You are quite welcome, Fred.
Aug 29 '11 #5

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

Similar topics

7
by: M | last post by:
i have a form which i would like to input different "action" url depending on the button that was clicked. is there a way that javascript can prefill a defined action based on the button...
3
by: I_was_here | last post by:
Hey if anyone is a query pro please showoff some knowledge thx. Ie: I have a table with : part price location qty 1 part repeats throughout the table and its price remains the same but it...
3
by: darrel | last post by:
I have a file upload tool. Before I save the file to a directory on the server, I first check to see if that file exists. If it does, I exit out of the 'save' function and display a different...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
1
by: Bill_W_Stephens | last post by:
I need a form with two buttons and ability to detect which button was pressed. I got this code to work using a javascript global variable, but I know there must be a better way using DOM. Any...
1
by: greg.southerland | last post by:
How would I direct users to a different page based on which radio button they select in a form? Thanks for any help you can provide. Greg
1
by: dympna | last post by:
Hi I have created a form which is derived form a query the query has a prompt box which prompts the user to input the criteria they wish to see. I have worked out how to put a command button into...
3
by: plaguna | last post by:
I'm trying to Count data in a Query Combining two different fields based on the "Date" the product was sold, and for some reason I get a wrong data. When I use only this expression, It works fine:...
1
by: angelicdevil | last post by:
i have listbox 1 which displays status , based on selection of status listbox 2 displays usernames. and based on username selected the textbox displays the email id. its working fine till...
2
by: mbedford | last post by:
I have a table that has data from several different forms. Prior to building the form, I thought the best idea was to create several queries that pull all the data together from the various forms...
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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.