Solved

VB6 add a minute to the date time value

Posted on 2016-08-23
8
61 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now