Results 1 to 5 of 5

Thread: VB code problem using the DateDiff function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    VB code problem using the DateDiff function

    I am trying to finish off a small project I was given and am running into this error:

    Code:
    Microsoft VBScript runtime
    error '800a01a8' 
    Object required: '[string: "1/24/2011 9:34:00 AM"]'
    /Logons.asp, line 162
    The code I am using to do this is:

    Code:
    sql="SELECT TOP 1 * FROM LogOffs WHERE Oper='" & rs("Oper") & "' AND OperDate >= '" & rs("OperDate") & "' AND OperTime >= '" & rs("OperTime") & "' ORDER BY OperDate, OperTime;"
    
    set rs2 = conn.execute(sql) 
    'Dim LogOnDate as string
    'Dim LogOffDate as string
    if not rs2.eof and not rs2.bof then
    response.write "<td nowrap align='center'>" & rs2("OperDate") & "</td>"
    response.write "<td nowrap align='center'>" & FormatDateTime(rs2("OperTime"),vbLongTime) & "</td>"
    '3rd column goes here for minutes
    Set LogOnDate = (rs("OperDate") & " " & FormatDateTime(rs("Opertime"), vbLongTime)) 
    Set LogOffDate = (rs2("OperDate") & " " & FormatDateTime(rs2("Opertime"), vbLongTime)) 
    response.write "<td nowrap align='center'>" & DateDiff(Dateinterval.Minute, LogOnDate, LogOffDate) & "</td>"
    else
    response.write "<td colspan='2'>No Logoff Data Found</td>"
    response.write "<td align='center'>0</td>"
    end if
    This is an already existant asp page that someone created a while back and just adding to their code. The reason the dim statements are commented out is because this version of VB does not seem to like them (the guy who made the page used no variables in this way either). Basically what I am trying to do is concatenate 2 different date/time combinations (1 from rs and the other from rs2) and then get the DateDiff from them and write it in the next column. The Error I got comes AFTER the first date/time is concatenated. So the first date/time is "1/24/2011 9:34:00 AM" and the second should be "1/24/2011 10:41:00 am" but it never got to it.

    If it helps the format of the date/time is listed below (this is not an example of the matching pair I am trying to piece together, just examples of formatting):
    rs date: 1/9/2008 12:00:00 AM
    rs time: 1/1/1900 8:25:00 pm

    rs1 date: 2/18/2008 12:00:00 am
    rs1 time: 1/1/1900 5:11:00 pm

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB code problem using the DateDiff function

    Welcome to VBForums

    VBScript does like Dim statements, what it doesn't like is the data types - because everything has a data type of Variant (in VB.Net the closest equivalent is Object).

    The error is because you seem to be trying to use VB.Net code:
    Code:
    DateDiff(Dateinterval.Minute, LogOnDate, LogOffDate)
    ...whereas the Classic VB (and presumably VBScript) version of DateDiff uses strings (n for minutes, m for months):
    Code:
    DateDiff("n", LogOnDate, LogOffDate)

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VB code problem using the DateDiff function

    What is returned if you try this?
    Code:
    sql="SELECT TOP 1 * FROM LogOffs WHERE Oper='" & rs("Oper") & "' AND OperDate >= '" & rs("OperDate") & "' AND OperTime >= '" & rs("OperTime") & "' ORDER BY OperDate, OperTime;"
    set rs2 = conn.execute(sql) 
    
    Response.Write rs("OperDate") & <br />
    Response.Write rs("Opertime") & <br />
    Response.Write rs2("OperDate") & <br />
    Response.Write rs2("Opertime") 
    Response.End

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Re: VB code problem using the DateDiff function

    When I run that code it pulls the dates from each data set (I tested the validity of the data previously , but I did run yours just to check).

    I tried the "n" instead of 'dateinterval.minute' previously before and get the same object error.

    When I comment out the new lines I added (datediff line, set logoffdate, and responsewrite "0" lines) and just leave the Set DateLogOn line it gives me the error which is why I think its something to do with the formatting in that line.

    I was thinking about removing the Set LogOnDate & Set LogOffDate and just concatenating it all in the DateDiff statement, but it seems like that would be an excessively long line. Good or bad idea?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB code problem using the DateDiff function

    You shouldn't be using the Set keyword to assign a string, either use Let or nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width