Solved

Subtract 1 month from current month

Posted on 2010-09-16
6
386 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 75

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

831 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