VB.Net 2008 - DateDiff Function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: VB.Net 2008 - DateDiff Function

  1. #1
    Join Date
    Mar 2009
    Posts
    8

    VB.Net 2008 - DateDiff Function

    Hi again

    I just wanted to ask if it is possible to use the DateDiff function and insert its result into a table in the database?

    And if so, what would be the code for it? ^___^

    thank you again for your help~

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Of course you can.
    Code:
    INSERT INTO sometable (field1) 
    VALUES (DateDiff("d", Now(), DateSerial(Year(Now()), 12, 31)))
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    What kind of database? Do you want to update a single row, as Hack showed, or several rows?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  4. #4
    Join Date
    Mar 2009
    Posts
    8
    Quote Originally Posted by Hack View Post
    Of course you can.
    Code:
    INSERT INTO sometable (field1) 
    VALUES (DateDiff("d", Now(), DateSerial(Year(Now()), 12, 31)))
    wow. thanks~
    what if I have to base the time using two different fixed times?
    like one would be 08:00:00 and the other would be 16:00:00.

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Ok. So are you saying you would need to separate records created; one for 08:00:00 and the other for 16:00:00?

    What are you doing exactly?

    And you still haven't said what kind of database you are using. That can, and often does, make a difference in coding.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Mar 2009
    Posts
    8
    Quote Originally Posted by Phil Weber View Post
    What kind of database? Do you want to update a single row, as Hack showed, or several rows?
    SQL
    well., just a single row but having a code for several rows would be helpful as well.

    your help is much appreciated thank you~

  7. #7
    Join Date
    Mar 2009
    Posts
    8
    No. Not really. More like, since they would both be stored in one table, I just need to know if the DateDiff function works in an If statement and a column in a table can be a condition like:

    Code:
     
    
    Dim a,b as DateTime
    a = 08:00:00
    b= 16:00:00
    
    If TIME.Time_In >= a then
    (datediff function)
    
    else
    
    condition;
    I'm using SQL

    I'm currently doing a payroll system as a final requirement. And the company has two types of shifts. Regular and graveyard.

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Ok.

    You are using SQL Server and writing your code in VB.NET, right?

    DateDiff can be used in an IF statement.

    I really don't know what to say to you.

    Most of what you have said is in the form of a statement, rather than question. And, the questions arre really, really broad based and general.

    Perhaps if you have a specific question, we could be more help.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  9. #9
    Join Date
    Mar 2009
    Posts
    8
    Oh. I'm sorry ^^

    Ok. Uhm, I have two tables right now. one named attend and the other time. Time stores the Emp_ID, Time_In, Time_Out and Minutes_Late of the employee. But the since the company has two shifts, we decided to make a separate (which is attend) to store the fixed times where we will base the DateDiff. The problem now is, would it be possible to retrieve the fixed time from another table and use those values in a DateDiff function in the time table? ^^

  10. #10
    Join Date
    Mar 2009
    Posts
    8
    sorry bout that. here is the code I am using right now:

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim timeout As String
            Dim user As String
            Dim tim As String
    
            con = New SqlConnection("Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=C:\Users\Carlo\Desktop\OTTO Payroll System\OTTO Payroll System\OTTOdBase.mdf;User Instance=true")
            con.Open()
    
            user = "'" & Trim(TextBox1.Text) & "'"
            timeout = "'" & Trim(Label1.Text) & "'"
            Try
                If CType(Me.ACCESSTableAdapter.LoginQuery(Me.TextBox1.Text, _
                         Me.TextBox2.Text), Integer) > 0 Then
    
                    tim = "UPDATE TIME SET Time_Out = " & timeout & ", Minutes_late = DateDiff(mi, ATTEND.TimeStart, TIME.Time_In) From TIME INNER JOIN ATTEND ON TIME.Emp_ID = ATTEND.Emp_ID WHERE Sessions = Sessions"
                    com = New SqlCommand(tim, con)
                    com.ExecuteNonQuery()
                    MsgBox("Hachoo!")
                Else
                    MsgBox("Invalid Username and/or Password!.")
    
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    End Class
    What confuses me is that when I log out, the last time for the log out is updated on all rows instead of just the row it's supposed to be in. like so:



    I tried chaging the UPDATE statement but still, nothing happens. What can be my course of action for this? >_<

    Your help is much appreciated..

  11. #11
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by dollipop View Post
    What confuses me is that when I log out, the last time for the log out is updated on all rows instead of just the row it's supposed to be in.
    This tells me your WHERE clause is not working properly.
    Code:
    tim = "UPDATE TIME SET Time_Out = " & timeout & ", 
    Minutes_late = DateDiff(mi, ATTEND.TimeStart, TIME.Time_In) From TIME INNER JOIN ATTEND ON TIME.Emp_ID = ATTEND.Emp_ID 
    WHERE Sessions = Sessions"
    What does "WHERE sesssions = sessions" mean?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. call function for button
    By angela_quests in forum VB Classic
    Replies: 2
    Last Post: 04-13-2007, 04:57 AM
  2. HELP.... DATEDIFF function for INFORMIX. THX.
    By Juan Alfredo in forum Database
    Replies: 1
    Last Post: 04-23-2003, 09:46 AM
  3. VB6 vs VB.NET...Procedure Calling Syntax
    By Patrick Troughton in forum .NET
    Replies: 78
    Last Post: 08-13-2002, 11:18 AM
  4. AnimateWindow API
    By Tim Manos in forum VB Classic
    Replies: 4
    Last Post: 10-19-2001, 06:06 AM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center