Solved

Subtract 1 month from current month

Posted on 2010-09-16
6
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

615 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