Solved

Subtract 1 month from current month

Posted on 2010-09-16
6
387 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

830 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