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. - SELECT <several fields here>
-
-
FROM ([tblEmployee Information] INNER JOIN [tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID
-
-
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?])))
-
-
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?
- 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).
- Perform Validation on the Input (Line #10, 11), making sure that:
- Is is Not Null
- It is Numeric
- It is within a Specified Range
- Define your SQL String based on the Input Parameter (Number of Days Out) (Line #14 - 19).
- 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).
- 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).
- The anticipation is killing me, so let's Open the Query and see the Results (Line #35)!
- Dim strSQL As String
-
Dim qdf As DAO.QueryDef
-
Dim qdfNew As DAO.QueryDef
-
Dim varDaysOut As Variant
-
Const conQUERY_NAME As String = "qryTest" 'Substitute if you like
-
-
varDaysOut = InputBox("Enter the Number of Days Out", "Days Out Prompt")
-
-
'The Number of Days out cannot be NULL, must be Numeric, and cannot be < 1 or > 50
-
If IsNull(varDaysOut) Or Not IsNumeric(varDaysOut) Or varDaysOut < 1 Or _
-
varDaysOut > 50 Then Exit Sub
-
-
'Define the SQL String
-
strSQL = "SELECT <several fields here> FROM ([tblEmployee Information] INNER JOIN " & _
-
"[tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) " & _
-
"INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID " & _
-
"WHERE Iif(CurrentTrainingType()=1,(qryLawsonQueryByArea.[Next First Aid/CPR]<=Date()+" & _
-
varDaysOut & "),Iif(CurrentTrainingType()=2,(qryLawsonQueryByAre a.[Next Recert]<=Date()+" & _
-
varDaysOut & "),(Date()+" & varDaysOut & "))) ORDER BY qryLawsonQueryByArea.Name;"
-
-
'If the Query conQUERY_NAME exists, DELETE it
-
For Each qdf In CurrentDb.QueryDefs
-
If qdf.name = conQUERY_NAME Then
-
CurrentDb.QueryDefs.Delete conQUERY_NAME
-
Exit For
-
End If
-
Next
-
-
'Dynamicall Create the Query conQUERY_NAME
-
With CurrentDb
-
Set qdfNew = .CreateQueryDef(conQUERY_NAME, strSQL)
-
End With
-
-
'Open the Query conQUERY_NAME, and see the Results
-
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
Just subscribing for now, must put thinking cap on! (LOL)
- 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).
- Perform Validation on the Input (Line #10, 11), making sure that:
- Is is Not Null
- It is Numeric
- It is within a Specified Range
- Define your SQL String based on the Input Parameter (Number of Days Out) (Line #14 - 19).
- 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).
- 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).
- The anticipation is killing me, so let's Open the Query and see the Results (Line #35)!
- Dim strSQL As String
-
Dim qdf As DAO.QueryDef
-
Dim qdfNew As DAO.QueryDef
-
Dim varDaysOut As Variant
-
Const conQUERY_NAME As String = "qryTest" 'Substitute if you like
-
-
varDaysOut = InputBox("Enter the Number of Days Out", "Days Out Prompt")
-
-
'The Number of Days out cannot be NULL, must be Numeric, and cannot be < 1 or > 50
-
If IsNull(varDaysOut) Or Not IsNumeric(varDaysOut) Or varDaysOut < 1 Or _
-
varDaysOut > 50 Then Exit Sub
-
-
'Define the SQL String
-
strSQL = "SELECT <several fields here> FROM ([tblEmployee Information] INNER JOIN " & _
-
"[tblEmployee Training] ON [tblEmployee Information].ID=[tblEmployee Training].ID) " & _
-
"INNER JOIN qryLawsonQueryByArea ON [tblEmployee Information].ID=qryLawsonQueryByArea.ID " & _
-
"WHERE Iif(CurrentTrainingType()=1,(qryLawsonQueryByArea.[Next First Aid/CPR]<=Date()+" & _
-
varDaysOut & "),Iif(CurrentTrainingType()=2,(qryLawsonQueryByAre a.[Next Recert]<=Date()+" & _
-
varDaysOut & "),(Date()+" & varDaysOut & "))) ORDER BY qryLawsonQueryByArea.Name;"
-
-
'If the Query conQUERY_NAME exists, DELETE it
-
For Each qdf In CurrentDb.QueryDefs
-
If qdf.name = conQUERY_NAME Then
-
CurrentDb.QueryDefs.Delete conQUERY_NAME
-
Exit For
-
End If
-
Next
-
-
'Dynamicall Create the Query conQUERY_NAME
-
With CurrentDb
-
Set qdfNew = .CreateQueryDef(conQUERY_NAME, strSQL)
-
End With
-
-
'Open the Query conQUERY_NAME, and see the Results
-
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.
It works great. Thanks for your help.
You are quite welcome, Fred.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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:...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
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: 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...
|
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,...
|
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...
| |