Solved

Subtract 1 month from current month

Posted on 2010-09-16
6
384 Views
Last Modified: 2012-05-10
The following code does not work. It should "do something" but it does not. I'm trying to subtract 1 month from current month and compare it to a string variable pulled from database. The string variable strCCMonth is '8', so it should do something since the current month is '9'.

Thank you.
Dim monthadd As Date = DateAdd("m", -1, Date.Now)

        If strCCMonth = monthadd.ToString() Then

           Do Something here

        End If

Open in new window

0
Comment
Question by:mlg101
6 Comments
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33695801
Try this:

        Dim monthadd As Date = DateAdd("m", -1, Date.Now)

        If strCCMonth = monthadd.Month.ToString() Then

            'Do Something here

        End If

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33695819
try Dim SubMonth As Integer = Today.Month - 1
0
 
LVL 7

Expert Comment

by:EYoung
ID: 33695830
Put a break on the "Do Something here" line and when the program stops, put the cursor over each of the two values, i.e. strCCMonth and monthadd.ToString().  That will show you the value in each and the field sizes, blank spaces, etc.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33695834
A simpler way:

        If strCCMonth = Now.AddMonths(-1).ToString() Then

            'Do Something here

        End If

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33695838
A bit simpler, I think:
Dim monthadd As Date = Now.AddMonths(-1)

If Convert.ToInt32(strCCMonth) = monthadd.Month Then
    'Do Something
End If

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33695857
....
Dim SubMonth As Integer = Today.Month - 1

If strCCMonth = SubMonth.ToString() Then

            'Do Something here

 End If

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now