473,548 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

filtering on a date field in Access using VB6

9 New Member
Hi all,

I am having trouble with VB6 (running in windows7) talking to an Access database, and was hoping someone
could help me locate the error.

the database contains a "DateIn" field of type "Date" (short Date)
I am reading the table into a recordset called "rsDiv"

I am trying to read only records for the financial year, between [1 Jul (name the year)] to [30 Jun] the next year.

my code is as follows.....

Expand|Select|Wrap|Line Numbers
  1.    FY = InputBox("Financial Year starting:-  Jul, ", "Enter Financial Year")
  2.    st = "DateIn > '30/06/" & CStr(FY) & "' and DateIn < '1/07/" & CStr(FY + 1) & "'"
  3.    rsDiv.Filter = st
  4.  
I have tried using CDate(construct ed date), CStr(DateIn), even #constructed date#.
so far, the filter allows all records through.

any sugggestions, apart from "give it away" will be appreciated.

Many thanks
Phil
Aug 13 '12 #1
10 12607
MikeTheBike
639 Recognized Expert Contributor
Hi

Just recently I have a similar problem with inconsistent filtering of a recordset.

However, I work almost exclusively in VBA (Excel and Access) and a little in .Net or stand alone exe programms, and always use ADO objects, so this may invalidate my post?

In code you need to format the date as 'America' ie. mm/dd/yy. This has always worked fine when creating record sets, but not filtering it would seem, hence the inconsistent results.

My solution was to use the 'universal' date format yyyy/mm/dd.

Therefore I would suggest you try this
Expand|Select|Wrap|Line Numbers
  1.    FY = InputBox("Financial Year starting:-  Jul, ", "Enter Financial Year") 
  2.    st = "DateIn > #" & CStr(FY) & "/06/30# and DateIn < #" & CStr(FY + 1) & "/07/01#" 
  3.    rsDiv.Filter = st 
You will need to validate the 'year' entered by the user is a four digit year or work out which century it should be and add the relevant 19/20 to the two digit year!?

HTH


MTB
Aug 13 '12 #2
Rabbit
12,516 Recognized Expert Moderator MVP
In Access VBA, asside from setting the filter, you also have to turn the filter on. I don't know if that is also the case for VB6.
Aug 13 '12 #3
MikeTheBike
639 Recognized Expert Contributor
Hi

I believe that ADO (and DAO?) recordsets don't have a FilterOn property (this is Form property); to remove the filtering you just use .Filter = ""

I also think that ADO objects are the same in VB6 and VBA, but I haven't used VB6 for about 10 years.


MTB
Aug 13 '12 #4
Phil Frankel
9 New Member
Thank you gentlemen, but your idea didn't work either. My system date is set to Australia, so I don't usually have to worry about American date.
I agree with Mike about the FilterOn property.

many thanks for your input though.
Phil
Aug 13 '12 #5
Phil Frankel
9 New Member
Hi all,

Thanks for the input, but I have created a "work-around" as follows.

Expand|Select|Wrap|Line Numbers
  1.   Do While Not rsDiv.EOF
  2.      If Month(rsDiv!DateIn) > 6 And Year(rsDiv!DateIn) = FY or Month(rsDiv!DateIn) < 7 And Year(rsDiv!DateIn) = FY+1 Then
  3. '   do stuff here
  4.  
many thanks to all who replied.
cheers
Phil
Aug 14 '12 #6
Killer42
8,435 Recognized Expert Expert
Glad to see you found a workaround.

I'd just like to point out, in SQL you'd generally use the Between clause for a date range like that.

Another handy tip to prevent issues with misinterpretati on of dates is to use an unambiguous format. For example "01/07/2012" could mean either of two dates depending on the Regional settings. On the other hand, "01-Jul-2012" can't easily be misinterpreted. And it's simple to produce using VB's Format function.

P.S. Perhaps you need to ReQuery or something after setting a filter? (I've never used filters much). Even though you've found a way around the problem, it's useful to try and get a solution on record for the benefit of anyone else who comes searching.
Aug 14 '12 #7
Phil Frankel
9 New Member
Good point, thank you.
Aug 14 '12 #8
MikeTheBike
639 Recognized Expert Contributor
Hi

I've been experimenting with this, and I also figured that BEWTEEN is the more normal criteria, but it doesn't work when filtering ('auguments are of the wrong type,are out of acceptable range, or are in conflict with one another' is the error message).
This all seemed to work OK without any formatting (my computer is set to UK date)
Expand|Select|Wrap|Line Numbers
  1. Sub TestFilter()
  2.     Dim cn As ADODB.Connection
  3.     Dim rs As ADODB.Recordset
  4.  
  5.     Dim Yr As String
  6.     Dim dFromDate As Date
  7.     Dim dToDate As Date
  8.     Dim strFilter As String
  9.  
  10.     Set cn = New ADODB.Connection
  11.     cn.Open "File Name=H:\My Data Sources\CES Timesheets DB DEV.udl"
  12.  
  13.     Set rs = New ADODB.Recordset
  14.     rs.Open "SELECT WE_Date, WKNo FROM tblTimesheets WHERE WE_Date > #2012/03/31# GROUP BY WE_Date, WKNo ORDER BY WE_Date", cn, adOpenStatic, adLockReadOnly
  15.  
  16.     MsgBox rs("WE_Date") & " : " & rs.RecordCount
  17.  
  18.     Yr = "12"
  19.     dFromDate = DateSerial(Yr, 6, 1)
  20.     dToDate = DateSerial(Yr + 1, 5, 30)
  21.     strFilter = "WE_Date >= #" & dFromDate & "# AND WE_Date <= #" & dToDate & "#"
  22.  
  23.     MsgBox strFilter
  24.  
  25.     rs.Filter = strFilter
  26.  
  27.     If rs.EOF Then
  28.         MsgBox "No Date found (" & dFromDate & ")"
  29.     Else
  30.         MsgBox rs("WE_Date") & " : " & rs.RecordCount
  31.     End If
  32.  
  33.     rs.Filter = ""
  34.     MsgBox rs("WE_Date") & " : " & rs.RecordCount
  35.  
  36.     rs.Close
  37.     cn.Close
  38.     Set rs = Nothing
  39.     Set cn = Nothing
  40.  
  41. End Sub
I assume as the date is created in code (as apposed to being read from say a spreadsheet when I encountered previous inconsistancies ) that it just knows the date!

Agian this is written in Excel VBA, but there is nothing in this code that is Excel specific, and, I believe, should run in VB6 with change.

MTb
Aug 14 '12 #9
Killer42
8,435 Recognized Expert Expert
I don't understand what you mean about a date being created in code, or when you say VBA "just knows the date". When you move a value directly from a date variable into a string (rather than using something like the Format function), it would most likely be formatted using your Windows date format.

If BETWEEN didn't work, I'd say it's likely that you didn't quite get the syntax right. But as long as your code works, the end result's the same so who's worrying?

By the way, your latest code is using a range of June to May, rather than (as stated) July to June. Just thought I'd mention that, in case it was accidental.

Oh, one more thing. Naturally we're happy to help out wherever possible. But keep in mind that our real experts in VBA, Access and Excel tend to hang out over in the Access / VBA forum.

P.S. Whoops! Didn't realise until after writing my response that the latest message was from a different person to the original poster.
Aug 15 '12 #10

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

Similar topics

3
23803
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004, 12:28 pm EDT This makes them essentially useless for sorting and logical evaluation. Understandbly, the MySQL date functions don't seem able to...
3
4869
by: Fran Zablocki | last post by:
I have a process that exports an Access table to a comma-delimited text file. One of the fields that is exported shows the date it was exported, using the Date() function. In the Access table, the date appears in the records in mm/dd/yyyy format, but after exporting, the date appears as mm/dd/yyyy 0:00:00. For example, today is 11/06/2003. It...
2
2309
by: Norbert Lieckfeldt | last post by:
I am setting up a database for a friend who's an Optician, using MS Access 2002. All seems to be working well, but I have hit a snag. There's a calculated field both in a form and a query which calculates the date for the next appointment from a date field and the number of months to the next appointment. That works fine. For reminder...
4
5283
by: S. van Beek | last post by:
Dear reader, By a Date field with Now() as default value the content of the field is date plus time. As I need a filter in a query on date only (excluding time) I invented the following solution:
1
1834
by: Graham Feeley | last post by:
I have a date field named rcdate it is a general date eg: 12/08/2006 3:30:00 PM 12/08/2006 3:00:00 PM I used to update another empty field named rdate with the with using in a update query " Left(Ratings!RCDate,10)" This now does not work for some reason gives me the error "undefined function in left function"
2
21488
by: Igor | last post by:
Can someone tell me sql query for filtering date field for current day, not last 24hours but from 00:00 to current time?
8
6392
by: Ragbrai | last post by:
Howdy All, I have a query that is used for filtering results to be used in a combo box. The query needs to test fields from both a table and then unbound text boxes on the form that also contains the combo box. The fields I am working with are date fields that are formated as Short Date. I have written an IIF statement and placed it within the...
7
6715
by: Katherine | last post by:
I'm trying to filter the records on the mainform (MailingList) of my database using a field contained in a subform (Donations). I was basing my code off Allen Browne's Access Tips page (here: http://allenbrowne.com/ser-28.html) since it has worked so well in the past. However, now I'm trying to filter by date instead of by a string or a...
7
10170
by: sheri | last post by:
I have a field called date, the date is in the field like this 70925 (which means Sept. 25, 2007). I have another field called day, it is a text field. How do I write a query to populate the day field from the date field? Thanks for your help.
1
1237
by: ndeeley | last post by:
Okay... I've created a drop down list fed by a table. I want my users to select an item in the list, say `Awaiting Repairs`, then fill in a single date text field. When a command button is pressed Access takes this date and fills in one of six disabled text boxes below (ie the one relating to the item in the list). I can do this for one...
0
7707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.