Solved

VB6 add a minute to the date time value

Posted on 2016-08-23
8
72 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 46

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 46

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
 
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 46

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no 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 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…
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…

815 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

10 Experts available now in Live!

Get 1:1 Help Now