Hi All, if anyone could help me with my codes. I have a form with txtStartDate, txtEndDate, with a listbox lstWorkSLY and a cmdRunExtract button. I wanted to query records filtering txtStartDate to txtEndDate based on the items selected in lstWorkSLY. Here's my code, it is not working when I set Nov. 5, 2008 as my txtStartDate and Nov. 28, 2008 as my txtEndDate still all records with DateX June 1, 2008 to January 2009 appears as my output. I don't know where to place my date range condition. Please help me! Thanks in advance. - Private Sub cmdRunExtract_click()
-
-
On Error GoTo Err_cmdRunExtract_click
-
Dim db As Database
-
Dim qdef As QueryDef
-
Dim i As Integer
-
Dim strSQL As String
-
Dim strWhere As String
-
Dim strIN As String
-
Dim flgSelectAll As Boolean
-
Dim varItem As Variant
-
Dim strDateField As String
-
Dim strWhereDate As String
-
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'do not change it to much your local settings
-
-
-
Set db = CurrentDb()
-
-
strSQL = "select * from tblSLY"
-
strDateField = "[DateX]" 'date field from table tblSLY
-
-
'Build the filter string
-
If IsDate(Me.txtStartDate) Then
-
strWhereDate = "(" & strDateField & ">= " & Format(Me.txtStartDate, strcJetDate) & ")"
-
End If
-
If IsDate(Me.txtEndDate) Then
-
If strWhereDate <> vbNullString Then
-
strWhereDate = strWhereDate & " and "
-
End If
-
strWhereDate = strWhereDate & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
-
'end if
-
'build the IN string by looping through the list
-
For i = 0 To lstWorkSIMMSLY.ListCount - 1
-
If lstWorkSLY.Selected(i) Then
-
If lstWorkSLY.Column(0, i) = "All" Then
-
flgSelectAll = True
-
End If
-
strIN = strIN & "'" & lstWorkSLY.Column(0, i) & "',"
-
End If
-
Next i
-
-
'Create the WHERE string, and strip off the last comma of the IN string
-
strWhere = " where [Natr] in " & _
-
"(" & Left(strIN, Len(strIN) - 1) & ")"
-
-
'if ALL was selected in the listbox, don't add the WHERE condition
-
If Not flgSelectAll Then
-
strSQL = strSQL & strWhere
-
End If
-
End If
-
db.QueryDefs.Delete "qrySLY"
-
Set qdef = db.CreateQueryDef("qrySLY", strSQL)
-
-
'Open the query, built using the In clause to set the criteria
-
DoCmd.OpenQuery "qrySLY", acViewNormal
-
-
'Clear listbox selection after running query
-
For Each varItem In Me.lstWorkSLY.ItemsSelected
-
Me.lstWorkSLY.Selected(varItem) = False
-
Next varItem
-
-
exit_cmdRunExtract_click:
-
Exit Sub
-
-
Err_cmdRunExtract_click:
-
If Err.Number = 5 Then
-
MsgBox "You must make a selection(s) from the list", , "Selection Required!"
-
Resume exit_cmdRunExtract_click
-
Else
-
'Write out the error and exit the sub
-
MsgBox Err.Description
-
Resume exit_cmdRunExtract_click
-
End If
-
-
End Sub
I have reviewed your code. No where do you actually add the where condition regarding the dates. It also seems you have commented out a End IF on line 31 then isn't meant to be commented out.
Try stepping through your code, and look at the values as they change, and which "way" the code runs through the IF statements. While the code is running, you can always use the Immediate Pane to check values (and even modify them) by simply writing "? strSQL" to see the value of the string variable strSQL.
I have tried to modify your code, but please don't just copy paste it, look at it, and understand the changes made. I have also added indentation to your code. It helps to make the code more readable, and illustrates the structure and buildup of for example if statements. - Private Sub cmdRunExtract_click()
-
-
On Error GoTo Err_cmdRunExtract_click
-
Dim db As Database
-
Dim qdef As QueryDef
-
Dim i As Integer
-
Dim strSQL As String
-
Dim strWhere As String
-
Dim strIN As String
-
Dim flgSelectAll As Boolean
-
Dim varItem As Variant
-
Dim strDateField As String
-
Dim strWhereDate As String
-
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'do not change it to much your local settings
-
-
-
Set db = CurrentDb()
-
-
strSQL = "select * from tblSLY"
-
strDateField = "[DateX]" 'date field from table tblSLY
-
-
'Build the filter string
-
If IsDate(Me.txtStartDate) Then
-
strWhereDate = "(" & strDateField & ">= " & Format(Me.txtStartDate, strcJetDate) & ")"
-
End If
-
-
If IsDate(Me.txtEndDate) Then
-
If strWhereDate <> vbNullString Then
-
strWhereDate = strWhereDate & " and "
-
End If
-
strWhereDate = strWhereDate & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
-
End If
-
-
'build the IN string by looping through the list
-
For i = 0 To lstWorkSIMMSLY.ListCount - 1
-
If lstWorkSLY.Selected(i) Then
-
If lstWorkSLY.Column(0, i) = "All" Then
-
flgSelectAll = True
-
End If
-
strIN = strIN & "'" & lstWorkSLY.Column(0, i) & "',"
-
End If
-
Next i
-
-
'Create the WHERE string, and strip off the last comma of the IN string
-
strWhere = " where [Natr] in " & "(" & Left(strIN, Len(strIN) - 1) & ")"
-
-
'if ALL was selected in the listbox, don't add the WHERE condition
-
If Not flgSelectAll Then
-
strSQL = strSQL & strWhere
-
End If
-
-
'Check to see if we need to add data where clause
-
If strWhereDate <> "" Then
-
'We need to add it.
-
If flgSelectAll Then
-
strSQL = strSQL & " AND " & strWhereDate
-
Else
-
strSQL = strSQL & " WHERE " & strWhereDate
-
End If
-
End If
-
-
db.QueryDefs.Delete "qrySLY"
-
Set qdef = db.CreateQueryDef("qrySLY", strSQL)
-
-
'Open the query, built using the In clause to set the criteria
-
DoCmd.OpenQuery "qrySLY", acViewNormal
-
-
'Clear listbox selection after running query
-
For Each varItem In Me.lstWorkSLY.ItemsSelected
-
Me.lstWorkSLY.Selected(varItem) = False
-
Next varItem
-
-
exit_cmdRunExtract_click:
-
Exit Sub
-
-
Err_cmdRunExtract_click:
-
If Err.Number = 5 Then
-
MsgBox "You must make a selection(s) from the list", , "Selection Required!"
-
Resume exit_cmdRunExtract_click
-
Else
-
'Write out the error and exit the sub
-
MsgBox Err.Description
-
Resume exit_cmdRunExtract_click
-
End If
-
-
End Sub
-
-
-
6 2539
I have reviewed your code. No where do you actually add the where condition regarding the dates. It also seems you have commented out a End IF on line 31 then isn't meant to be commented out.
Try stepping through your code, and look at the values as they change, and which "way" the code runs through the IF statements. While the code is running, you can always use the Immediate Pane to check values (and even modify them) by simply writing "? strSQL" to see the value of the string variable strSQL.
I have tried to modify your code, but please don't just copy paste it, look at it, and understand the changes made. I have also added indentation to your code. It helps to make the code more readable, and illustrates the structure and buildup of for example if statements. - Private Sub cmdRunExtract_click()
-
-
On Error GoTo Err_cmdRunExtract_click
-
Dim db As Database
-
Dim qdef As QueryDef
-
Dim i As Integer
-
Dim strSQL As String
-
Dim strWhere As String
-
Dim strIN As String
-
Dim flgSelectAll As Boolean
-
Dim varItem As Variant
-
Dim strDateField As String
-
Dim strWhereDate As String
-
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'do not change it to much your local settings
-
-
-
Set db = CurrentDb()
-
-
strSQL = "select * from tblSLY"
-
strDateField = "[DateX]" 'date field from table tblSLY
-
-
'Build the filter string
-
If IsDate(Me.txtStartDate) Then
-
strWhereDate = "(" & strDateField & ">= " & Format(Me.txtStartDate, strcJetDate) & ")"
-
End If
-
-
If IsDate(Me.txtEndDate) Then
-
If strWhereDate <> vbNullString Then
-
strWhereDate = strWhereDate & " and "
-
End If
-
strWhereDate = strWhereDate & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
-
End If
-
-
'build the IN string by looping through the list
-
For i = 0 To lstWorkSIMMSLY.ListCount - 1
-
If lstWorkSLY.Selected(i) Then
-
If lstWorkSLY.Column(0, i) = "All" Then
-
flgSelectAll = True
-
End If
-
strIN = strIN & "'" & lstWorkSLY.Column(0, i) & "',"
-
End If
-
Next i
-
-
'Create the WHERE string, and strip off the last comma of the IN string
-
strWhere = " where [Natr] in " & "(" & Left(strIN, Len(strIN) - 1) & ")"
-
-
'if ALL was selected in the listbox, don't add the WHERE condition
-
If Not flgSelectAll Then
-
strSQL = strSQL & strWhere
-
End If
-
-
'Check to see if we need to add data where clause
-
If strWhereDate <> "" Then
-
'We need to add it.
-
If flgSelectAll Then
-
strSQL = strSQL & " AND " & strWhereDate
-
Else
-
strSQL = strSQL & " WHERE " & strWhereDate
-
End If
-
End If
-
-
db.QueryDefs.Delete "qrySLY"
-
Set qdef = db.CreateQueryDef("qrySLY", strSQL)
-
-
'Open the query, built using the In clause to set the criteria
-
DoCmd.OpenQuery "qrySLY", acViewNormal
-
-
'Clear listbox selection after running query
-
For Each varItem In Me.lstWorkSLY.ItemsSelected
-
Me.lstWorkSLY.Selected(varItem) = False
-
Next varItem
-
-
exit_cmdRunExtract_click:
-
Exit Sub
-
-
Err_cmdRunExtract_click:
-
If Err.Number = 5 Then
-
MsgBox "You must make a selection(s) from the list", , "Selection Required!"
-
Resume exit_cmdRunExtract_click
-
Else
-
'Write out the error and exit the sub
-
MsgBox Err.Description
-
Resume exit_cmdRunExtract_click
-
End If
-
-
End Sub
-
-
-
Hi Smiley, I stepped into my codes and I noticed it skipped these codes and error "Item not found in this collection" appeared. - Set qdef = db.CreateQueryDef("qrySLY", strSQL)
-
-
'Open the query, built using the In clause to set the criteria
-
DoCmd.OpenQuery "qrySLY", acViewNormal
-
-
'Clear listbox selection after running query
-
For Each varItem In Me.lstWorkSLY.ItemsSelected
-
Me.lstWorkSLY.Selected(varItem) = False
-
Next varItem
-
-
exit_cmdRunExtract_click:
-
Exit Sub
Its been a while since I used the listbox control, but try: - For Each varItem In Me.lstWorkSLY.ItemsSelected
-
varItem.Selected = False
-
Next varItem
Remember though, that the For Each will loop through all items in that collection. So there is no need to refernce it again inside the loop.
Hi Smiley, it's working already. The error "Item not found in the collection" appeared because I didn't yet create a query "qrySLY" from query design. I created it and it's working already. I've chosen your first reply as the best answer. Thanks a lot!
I just want to ask a follow up question, what if I add another listbox here for filtering records? Is it possible? Another is that if i select "ALL" in the listbox the filtering date range was disregarded, I mean it should find first all records within the date range before selecting "ALL" codes in the listbox. Let's say, i have records from Feb 1 t0 28, 2012 with codes from listbox A, B, C, D, E and ALL. If I set start date - Feb. 1, 2012 and end date - Feb. 14, 2012 and select ALL from listbox, It should display ALL records from Feb. 1, 2012 to Feb. 14, 2012 not ALL records of Feb. 1 to 28, 2012. Thanks in advance!
NeoPa 32,556
Expert Mod 16PB Cascaded Form Filtering explains many of the possibilities, but you have to implement your specific logic. The possibilities are various, but this gives the tools with which you can build your logic.
If what you need is more than this simple direction then you should ask it in a new thread. This one's already answered.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brian Jorgenson |
last post by:
I am looking for a formula to put in my query to pull data based on
the last 3 months. It starts with the current day and will go back 3
months.
Here is my wrkflow language for example:
...
|
by: Cy |
last post by:
I have a custom access 2000 database, that has contracts with starting
and ending dates. What I'd like to be able to do, is key in a date, say
today 9/2/05 and get a list of all "current"...
|
by: DhavalPatel1983 |
last post by:
Hi,
I need help in VB Datareport, my problem describe as below....
I have one Bank application project with all the information like Deposite,Clearness,Check,Cash,Creadit,ATM...
|
by: pili |
last post by:
Help!
I have a form that contains a From and To - Date Fields and I would like to be able to enter the dates and then click a button labeled 'Display' which will then list records that are between...
|
by: megahurtz |
last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
|
by: AllyFrog |
last post by:
Hello,
New here - wondering if someone may be able to help me with an issue I'm having.
I have a table where I am recording details of shifts worked. Each record has the date of the shift,...
|
by: zandiT |
last post by:
hello again
i have almost finished with my database. i have decided to generate the reports by using a date or date range and i can't get it to work. first i used parameters in a query but its...
|
by: tonymcc |
last post by:
Hi all
I am having a head bursting nightmare trying to get a query to work, I will try and explain.
I have developed a database for the calculating storage costs in a warehouse. Customers are...
|
by: ogdcl |
last post by:
Dear Friends......I have the data like:-
Emp_ID Start_Date End_Date Pay_Due Pay_Drawn
===== ======== ======= ======= ========
101 07-APR-2008 31-JAN-2009 15000 12000
101 01-FEB-2009...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |