473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this SQL for Date?

418 Contributor
My form has two text boxes, txtDateFrom and txtDateTo. A macro button with the caption Month is to automatically fill in txtDateFrom and txtDateTo with current month date. My code reads as:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdmonth_Click()
  2. 'Sets the Date From and Date To text boxes
  3. 'to show complete month (from start to end of current month)
  4.  
  5.     Me!txtdatefrom = CDate("01/" & Month(Date) & "/" & Year(Date))
  6.     Me!txtDateTo = DateAdd("d", -1, DateAdd("m", 1, Me!txtdatefrom))
  7.  
  8. End Sub
But it's giving me wrong date: 1/5/09 and 2/4/09. I expect it to show 5/1/09 and 5/31/09

By the way, macros for Today, Week and Year are working just fine. These codes are as follow:

TODAY
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdtoday_Click()
  2. 'Sets the Date From and Date To text boxes
  3. 'to Today's Date
  4.  
  5.     Me!txtdatefrom = Date
  6.     Me!txtDateTo = Date
  7.  
  8. End Sub
THIS WEEK
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdweek_Click()
  2. 'Sets the Date From and Date To text boxes
  3. 'to show complete working week (Mon - Fri)
  4.  
  5.     Dim today
  6.  
  7.     today = Weekday(Date)
  8.  
  9.     Me!txtdatefrom = DateAdd("d", (today * -1) + 2, Date)
  10.     Me!txtDateTo = DateAdd("d", 6 - today, Date)
  11.  
  12. End Sub
I need your experties to help me with my code for MONTH. Thanks.
May 15 '09 #1
11 2089
MNNovice
418 Contributor
To get current month (05/01/2009 - 05/31/2009) I modified the code to read as:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdmonth_Click()
  2. 'Sets the Date From and Date To text boxes
  3. 'to show complete month (from start to end of current month)
  4.  
  5.     Me!txtdatefrom = DateAdd("m", (today * -1), Date)
  6.     Me!txtDateTo = DateAdd("d", -1, DateAdd("m", 1, Me!txtdatefrom))
  7.  
  8. End Sub
but the end result has been 5/15/2009 - 6/14/2009.

Is it possible to get This month instead of a month from today?

Thanks.
May 15 '09 #2
Denburt
1,356 Recognized Expert Top Contributor
Is this a double post? If it is do you mind if I remove the other one? If it isn't please let me know so I can adjust the title as needed.

http://bytes.com/topic/access/answer...wrong-sql-date

Expand|Select|Wrap|Line Numbers
  1.    Me!txtdatefrom = Dateserial(Year(Date),month(Date),1)
  2.      Me!txtDateTo =Dateserial(Year(Date),month(Date),1-1)
  3.  
May 15 '09 #3
IT Couple
36 New Member
Hi

Try adding the format function
format(date, "dd/mm/yyyy") or format(date, "short date")

Regards
Emil
May 15 '09 #4
Denburt
1,356 Recognized Expert Top Contributor
@IT Couple
The format function would return a Variant (String) not an actual date if you would like to keep it as a date you would need to convert it like so:
Expand|Select|Wrap|Line Numbers
  1. CDate(format(date, "dd/mm/yyyy"))
Just an FYI
May 15 '09 #5
Chinde
52 New Member
The format function would return a Variant (String) not an actual date if you would like to keep it as a date you would need to convert it like so:
Expand|Select|Wrap|Line Numbers
  1. CDate(format(date, "dd/mm/yyyy"))
Just an FYI
Good tip thank you!!
May 18 '09 #6
MNNovice
418 Contributor
Denburt:

The following code:
Expand|Select|Wrap|Line Numbers
  1.    Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
  2.      Me!txtDateTo =Dateserial(Year(Date),month(Date),1-1) 
gave me this:
Start Date: 5/1/09
End Date: 4/30/09

So I changed it to read as
Expand|Select|Wrap|Line Numbers
  1.    Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
  2.      Me!txtDateTo =Dateserial(Year(Date),month(Date),1+30) 
Now I got: Start date: 5/1/09 and End Date 5/31/09

BUT what happens when the month has 28 days or 30 days? Can I include an IIF statement somewhere to make it dynamic?

Thanks.
May 18 '09 #7
Denburt
1,356 Recognized Expert Top Contributor
I should have added a month then subtracted 1-1in the day field to get the last day of any month. Yes I missed a little something there. The following will suit your needs and will take care of the ending date.
Expand|Select|Wrap|Line Numbers
  1.    Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
  2.      Me!txtDateTo =Dateserial(Year(Date),month(Date)+1,1-1) 
May 18 '09 #8
Denburt
1,356 Recognized Expert Top Contributor
@Chinde
Your quite welcome, glad you found it useful.
May 18 '09 #9
MNNovice
418 Contributor
Denburt: Many thanks. It worked just fine.

How do I close this thread now? Just delete from "my subscription?" Thanks.
May 18 '09 #10

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

Similar topics

3
1532
by: K R | last post by:
Hi, I have generated this XML from my application. But, when I open this XML, it is throwing error. Please help me to resolve this. <?xml version="1.0" encoding="utf-8" ?> <searchResults> <totalhits>16916</totalhits> <searchquery>*</searchquery> <timetaken>TIME TAKEN</timetaken>
7
6026
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date. Returns ' the date of the last day of that month (in the same year) '**************************************************************************...
3
5472
by: Soren Jorgensen | last post by:
Hi, Following code should give the number of weeks in years 1998-2010 for a Danish calendar (on a Danish box) GregorianCalendar cal = new GregorianCalendar(); for(int i = 1998; i < 2010; i++) { DateTime date = new DateTime(i, 12, 31); int week= cal.GetWeekOfYear(date,
3
1524
by: Terry Olsen | last post by:
I've got 2 different web pages, both updating the same SQL database. One is for the Technician and one is for the Manager. The technician's update page works fine but the Manager's update page doesn't. They are both the same..as far as I can tell... --------------------- This code works fine, updating the SQL database with the edited...
4
1155
by: Hyphessobrycon | last post by:
Private Sub btnrubriekbij_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrubriekbij.Click 'insert hier Dim cn As New OleDb.OleDbConnection(constr) Dim dc As New OleDb.OleDbCommand dc.Connection = cn
7
6270
by: arun_shamli | last post by:
class CDate { public: CDate() {} CDate(const CDate& date) {} }; CDate function1() { CDate date(); return date;
30
15395
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions to get both some understanding of how wide candidates knowledge is, and how much DB2 specifics they know. Of the questions below, how many do you...
2
2761
by: Angus | last post by:
There are the C runtime library functions of course, but are there any STL time or date related functions? I couldn't find any.
34
2360
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - What online resources are available? ----------------------------------------------------------------------- * The Official ECMAScript Specification http://www.ecma-international.org/publications/standards/Ecma-262.htm
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
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
7819
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
5377
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
5097
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
3505
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
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.