Solved

VB6 add a minute to the date time value

Posted on 2016-08-23
8
45 Views
Last Modified: 2016-08-24
Hi

I'm trying to add a minute to the date / time value in column 1 of the value in column 1 = the value in column 0.

date time
ex: if column 0 and 1 have the value "09/14/2016 13:00:00" then in column 1, it will add 1 minute. The result would then be: 09/14/2016 "13:01:00".

I tried like below but no luck.

Private Sub Command1_Click()
    Dim kk As Long

    For kk = 1 To MSHFlexGrid1.Rows - 1

        If MSHFlexGrid1.TextMatrix(kk, 1) = MSHFlexGrid1.TextMatrix(kk, 0) Then
          '  MSHFlexGrid1.TextMatrix(kk, 1) = Format(CDate(Format(MSHFlexGrid1.TextMatrix(kk, 0), "0000-00-00 00:00")) + DateAdd("n", 1, Time()))
        End If

    Next kk
End Sub

Open in new window


How can i do that?

Thanks
0
Comment
Question by:Wilder1626
  • 5
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 41767950
Without a project to test with it's hard, but try

MSHFlexGrid1.TextMatrix(kk, 1) = Format(CDate(Format(MSHFlexGrid1.TextMatrix(kk, 0), "0000-00-00 00:00")) + DateAdd("n", 1, MSHFlexGrid1.TextMatrix(kk, 0)))
        End If
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 41767971
Just tested and i have an error of incompatible type.

Here is the sample.
TEST-DATE.zip
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 41768024
I think i was able to do it like this. But it does not look too clean.

I will test it more:

    Dim kk As Long

    For kk = 1 To MSHFlexGrid1.Rows - 1

        If MSHFlexGrid1.TextMatrix(kk, 0) <> "" Then
            MSHFlexGrid1.TextMatrix(kk, 0) = Mid(MSHFlexGrid1.TextMatrix(kk, 0), 7, 4) & Left(MSHFlexGrid1.TextMatrix(kk, 0), 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 4, 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 12, 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 15, 2)

        End If

        If MSHFlexGrid1.TextMatrix(kk, 1) <> "" Then
            MSHFlexGrid1.TextMatrix(kk, 1) = Mid(MSHFlexGrid1.TextMatrix(kk, 1), 7, 4) & Left(MSHFlexGrid1.TextMatrix(kk, 1), 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 4, 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 12, 2) & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 15, 2)

        End If
        On Error Resume Next
        If MSHFlexGrid1.TextMatrix(kk, 1) = MSHFlexGrid1.TextMatrix(kk, 0) Then
            MSHFlexGrid1.TextMatrix(kk, 1) = Format(CDate(Format(MSHFlexGrid1.TextMatrix(kk, 0), "0000-00-00 00:00")) + 0.001, "yyyymmddhhnn")
            
        End If
        
         If MSHFlexGrid1.TextMatrix(kk, 0) <> "" Then
            MSHFlexGrid1.TextMatrix(kk, 0) = Mid(MSHFlexGrid1.TextMatrix(kk, 0), 5, 2) & "/" & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 7, 2) & "/" & Left(MSHFlexGrid1.TextMatrix(kk, 0), 4) & " " & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 9, 2) & ":" & Mid(MSHFlexGrid1.TextMatrix(kk, 0), 11, 2) & ":" & "00"
            MSHFlexGrid1.TextMatrix(kk, 1) = Mid(MSHFlexGrid1.TextMatrix(kk, 1), 5, 2) & "/" & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 7, 2) & "/" & Left(MSHFlexGrid1.TextMatrix(kk, 1), 4) & " " & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 9, 2) & ":" & Mid(MSHFlexGrid1.TextMatrix(kk, 1), 11, 2) & ":" & "00"
        End If

    Next kk

Open in new window

0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41768153
Try this and tell me what's wrong (if anything).
Private Sub Command1_Click()
    Dim kk As Long

    For kk = 1 To MSHFlexGrid1.Rows - 1
        If MSHFlexGrid1.TextMatrix(kk, 0) <> "" Then
            If MSHFlexGrid1.TextMatrix(kk, 1) = MSHFlexGrid1.TextMatrix(kk, 0) Then
                MSHFlexGrid1.TextMatrix(kk, 1) = Format(DateAdd("n", 1, MSHFlexGrid1.TextMatrix(kk, 0)), "mm/dd/yyyy hh:mm:ss")
            End If
        End If
    Next kk
    
    'Auto column fit
    Dim cell_wid As Single
    Dim col_wid As Single

    For c = 0 To MSHFlexGrid1.Cols - 1
        col_wid = 0
        For r = 0 To MSHFlexGrid1.Rows - 1
            cell_wid = TextWidth(MSHFlexGrid1.TextMatrix(r, c))
            If col_wid < cell_wid Then col_wid = cell_wid
        Next r
        MSHFlexGrid1.ColWidth(c) = col_wid + 120
    Next c

    '---------- Align MSHFlexgrid ----------------
    With MSHFlexGrid1
        Dim k As Long
        For k = 0 To .Cols - 1
            .ColAlignment(k) = flexAlignLeftCenter
        Next
    End With
    
End Sub

Open in new window

0
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

 
LVL 11

Author Comment

by:Wilder1626
ID: 41768503
I'm almost there. The only thing left is that instead of putting a "/", it puts a dash "-".

result
I assume i can do a replace and that would fix it unless you see that it could be done a better way.

Like below:
    Dim kk As Long
    Dim txt As String

    For kk = 1 To MSHFlexGrid1.Rows - 1
        If MSHFlexGrid1.TextMatrix(kk, 0) <> "" Then
            If MSHFlexGrid1.TextMatrix(kk, 1) = MSHFlexGrid1.TextMatrix(kk, 0) Then
                MSHFlexGrid1.TextMatrix(kk, 1) = Format(DateAdd("n", 1, MSHFlexGrid1.TextMatrix(kk, 0)), "mm/dd/yyyy hh:mm:ss")
                
                txt = MSHFlexGrid1.TextMatrix(kk, 1)
                If InStr(txt, "-") Then
                    txt = Replace(txt, "-", "/")
                    MSHFlexGrid1.TextMatrix(kk, 1) = txt
                    MSHFlexGrid1.Row = kk
                    MSHFlexGrid1.Col = 1
                    MSHFlexGrid1.CellBackColor = &H80FF&
                End If
            End If
        End If
    Next kk

Open in new window


what do you think?
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 41768586
I will go with this solution and add the replace function to it.

Thanks a lot again for your help.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 41768827
Are we using the same code? This is what I see.
????
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 41768882
yes, i was using the same code.

Probably a regional setting problem.

 For kk = 1 To MSHFlexGrid1.Rows - 1
        If MSHFlexGrid1.TextMatrix(kk, 0) <> "" Then
            If MSHFlexGrid1.TextMatrix(kk, 1) = MSHFlexGrid1.TextMatrix(kk, 0) Then
                MSHFlexGrid1.TextMatrix(kk, 1) = Format(DateAdd("n", 1, MSHFlexGrid1.TextMatrix(kk, 0)), "mm/dd/yyyy hh:mm:ss")
            End If
        End If
    Next kk

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now