?
Solved

VB.NET: problem with DB dates populating - wrong day/month formats

Posted on 2009-12-22
12
Medium Priority
?
206 Views
Last Modified: 2013-12-25
Hi X-perts,

I need to create a column in the table with incrementing dates (by months). Here is my code.

The problem is that it creates a column with incrementing days, like

01-Jan-95
02-Jan-95
......
12-Jan-95
01-Jan-96
02-Jan-96

etc

How can I fix this?

thanks
            Dim startDate As Date = New Date(1994, 12, 1)
            Dim nextDate As Date

            nextDate = startDate
                For i As Integer = 1 To diffMonths
                    nextDate = nextDate.AddMonths(1)
                    SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate & "#)"
                    conn.Execute(SQL)
                Next

Open in new window

0
Comment
Question by:andy7789
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 2

Expert Comment

by:abelallen
ID: 26110118
Hi,

Y can't you try something like this?
dim your_date as date
dim next_date as date

your_date = CDate("1/1/03")
next_date = DateAdd("m", 1, your_date)

Use the appropriate logic to integrate this piece of code along with your solution...

Regards,
Abel Allen
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26110238
one way is this

SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("MM-dd-yyyy") & "#)"

or

SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("dd-MM-yyyy") & "#)"

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26110257
or use this

convert(datetime, '10/23/2016', 101)

SQL = "INSERT INTO Trend ([dates]) VALUES (convert(datetime, '" & nextDate.ToString("MM-dd-yyyy") & "', 101))"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26110278
HI,
 the code u have provided works great.. check the below code

Dim startDate As Date = New Date(1994, 12, 1)
        Dim nextDate As Date
        Dim str As String = ""

        nextDate = startDate
        For i As Integer = 1 To 10
            nextDate = nextDate.AddMonths(1)
            str += nextDate.ToShortDateString() + "<Br />"
        Next
        Response.Write(str)

I have just write to the screen and it give the desired output
0
 

Author Comment

by:andy7789
ID: 26110279
The problem is that if I use ##, it requires date (not string values) and returns an error. So far I have managed to get it working as this (see code).

Is it a right way of doing this? The field [dates] is of date/time type in the db table.
                For i As Integer = 1 To diffMonths
                    nextDate = nextDate.AddMonths(1)
                    SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("M/yyyy") & "')"
                    conn.Execute(SQL)
                Next

Open in new window

0
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26110433
why you dont try these ;)

SQL = "INSERT INTO Trend ([dates]) VALUES (convert(datetime, '" & nextDate.ToString("MM-dd-yyyy") & "', 101))"

or

SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("MM-dd-yyyy") & "#)"

or

SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("dd-MM-yyyy") & "#)"

or

SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("MM-dd-yyyy") & "')"

or

SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("dd-MM-yyyy") & "')"
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26110444
are you using access or sql db? and which version?
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26110450
I guess you are using access and default date format is this : mm/dd/yyyy

so

this should work

SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("MM/dd/yyyy") & "#)"
0
 

Author Comment

by:andy7789
ID: 26110538
Yes, I am using access. This is the result of all the experiments:

1) My version

SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("M/yyyy") & "')"

works OK => adds dates like 1/12/2007 (for December 01, 2007)

2)
SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("dd-MM-yyyy") & "')"

is exactly the same as mine => add  dates like 1/12/2007 (for December 01, 2007)

3) All the below variants:

SQL = "INSERT INTO Trend ([dates]) VALUES ('" & nextDate.ToString("MM-dd-yyyy") & "')"
SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("dd-MM-yyyy") & "#)"
SQL = "INSERT INTO Trend ([dates]) VALUES (#" & nextDate.ToString("MM-dd-yyyy") & "#)"

create dates like 12/01/2007 (for December 01)

4)
SQL = "INSERT INTO Trend ([dates]) VALUES (convert(datetime, '" & nextDate.ToString("MM-dd-yyyy") & "', 101))"

Generates an error

================================================
Which one from (1) or (2) is preferable? Is there something wrong with my code ("M/yyy")?

In fact, my code generates input strings like "08/2007"; when it is added to Access, it always adds 1 as a day, i.e. making it 1/08/2007

Please, comment.

Thank you!

0
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 26111836
If it's a datetime field, just try this will do:

SQL = "INSERT INTO Trend ([dates]) VALUES (Date()+1) "

Then when you want to output it on your page, just format it accordingly.


>>when it is added to Access, it always adds 1 as a day, i.e. making it 1/08/2007
if it's a datetime field, it MUST be a datetime or a NULL value.
0
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 26111837
>>if it's a datetime field, it MUST be a datetime or a NULL value.
if your field generally accept NULL value.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26113286
did you try 26110450
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

839 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