Solved

VB6 add a minute to the date time value

Posted on 2016-08-23
8
83 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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