+ Reply to Thread
Results 1 to 9 of 9

Open the recordset in access and use table name in dmax statement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Open the recordset in access and use table name in dmax statement

    Hi folks

    I have developed a project in access as a backend and excel as a frontend so all the coding is in excel/vba.
    I am writing the following function but becvause I have to refer a table in access database , I need to open the recordset and use that table name in the dmax statement. Please see the code below:

    Public Function Func3(AnyPerson As String, AnyStartTime As Date, AnyEndTime As Date) As String
     Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=C:\Documents and Settings\Amanpreet Kaur\Desktop\Sunlife Direct Pilot System.mdb;"
          Set rs = CreateObject("ADODB.Recordset")
        rs.Open "tblbatchdetails", cn, adOpenKeyset, adLockOptimistic, adCmdTable
      Dim dtmLower As Date
        Dim dtmUpper As Date
        dtmUpper = AnyStartTime
            dtmLower = nz(DMax("Finishtime", "tblbatchdetails", "[Name]='" & AnyPerson & "' and [Date1]=dateserial(" & Format(Date, "yyyy,mm,dd") & ")"))         
      Dim dtmtotaltime As Date
        dtmtotaltime = dtmUpper - dtmLower
        If dtmtotaltime = dtmUpper Then
        Func3 = "00:00:00"
        Else
                Func3 = dtmtotaltime
                End If
                       End Function
    I have a problem in code in red colour as tblbatchdetails is a table in access so how can we connect the frontend to this table. If I keed the access database opened while running the userform then it works fine but if the access database is closed and we try to tun the userform then it gives reserved error 2950 in dmax statement.

    Thanks for any help.

    Aman

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Open the recordset in access and use table name in dmax statement

    What is the point of the recordset if you do that? Why not include a WHERE statement in the SQL rather than just opening the table and select the Max value? In other wordds, something like:
    strSQL = "SELECT Max(Finishtime) FROM tblbatchdetails WHERE [Name]='" & AnyPerson & "' and [Date1]=dateserial(" & Format(Date, "yyyy,mm,dd") & ")"
    rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Re: Open the recordset in access and use table name in dmax statement

    Thanks for your reply. But I am getting runtime error 3265 "Item cannot be found in the collection corresponding to the requested name or ordinal" in the red line.
    Public Function Func2(AnyPerson As String, AnyStartTime As Date, AnyEndTime As Date) As String
     Dim dtmLower As Date
        Dim dtmUpper As Date
        dtmUpper = AnyStartTime
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=C:\Documents and Settings\Amanpreet Kaur\Desktop\Sunlife Direct Pilot System.mdb;"
          Set rs = CreateObject("ADODB.Recordset")
        strSQL = "SELECT Max(Finishtime) FROM tblbatchdetails WHERE [Name]='" & AnyPerson & "' and [Date1]=dateserial(" & Format(Date, "yyyy,mm,dd") & ")"
    rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
    dtmLower = rs.Fields("Finishtime") 
                Dim dtmtotaltime As Date
        dtmtotaltime = dtmUpper - dtmLower
                Func2 = dtmtotaltime
           End Function
    Can you please how to store the max finish time in a variable so that we can find out the elapsed time.

    Thanks
    Aman
    Last edited by aman1234; 05-10-2010 at 06:05 AM.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Open the recordset in access and use table name in dmax statement

    Your field is probably called MaxOfFinishTime or something similar. You can alias it in the SQL string or use Fields(0) since it's the only field in the recordset.

  5. #5
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Re: Open the recordset in access and use table name in dmax statement

    Many thanks for your reply. The code works fine when there are few records in the table and if the table is empty then in gives me an error "Invalid use of null"
    Public Function Func2(AnyPerson As String, AnyStartTime As Date, AnyEndTime As Date) As String
     Dim dtmLower As Date
        Dim dtmUpper As Date
        dtmUpper = AnyStartTime
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=C:\Documents and Settings\Amanpreet Kaur\Desktop\Sunlife Direct Pilot System.mdb;"
          Set rs = CreateObject("ADODB.Recordset")
        strSQL = "SELECT Max(Finishtime) FROM tblbatchdetails WHERE [Name]='" & AnyPerson & "' and [Date1]=dateserial(" & Format(Date, "yyyy,mm,dd") & ")"
    rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
    dtmLower = rs.Fields(0)
        Dim dtmtotaltime As Date
        dtmtotaltime = dtmUpper - dtmLower
        If dtmtotaltime = dtmUpper Then
        Func2 = "00:00:00"
        Else
                Func2 = dtmtotaltime
                End If
        End Function

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Open the recordset in access and use table name in dmax statement

    Check to see if records were returned:
    Public Function Func2(AnyPerson As String, AnyStartTime As Date, AnyEndTime As Date) As String
     Dim dtmLower As Date
        Dim dtmUpper As Date
        dtmUpper = AnyStartTime
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=C:\Documents and Settings\Amanpreet Kaur\Desktop\Sunlife Direct Pilot System.mdb;"
          Set rs = CreateObject("ADODB.Recordset")
        strSQL = "SELECT Max(Finishtime) FROM tblbatchdetails WHERE [Name]='" & AnyPerson & "' and [Date1]=dateserial(" & Format(Date, "yyyy,mm,dd") & ")"
    rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
    if not rs.EOF Then
    dtmLower = rs.Fields(0)
        Dim dtmtotaltime As Date
        dtmtotaltime = dtmUpper - dtmLower
        If dtmtotaltime = dtmUpper Then
             Func2 = "00:00:00"
        Else
                Func2 = dtmtotaltime
        End If
    Else
    Func2 = "00:00:00"
    End If
    End Function

  7. #7
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Re: Open the recordset in access and use table name in dmax statement

    Again it gives runtime error 94, "Invalid use of null" at

    dtmLower = rs.Fields(0)

    Thanks

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,138

    Re: Open the recordset in access and use table name in dmax statement

    Oh, so you are actually getting a null field:
    If IsNull(rs.Fields(0)) Then
     ' handle accordingly
    Else
    dtmLower = rs.Fields(0)
    End If

  9. #9
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Re: Open the recordset in access and use table name in dmax statement

    It worked gr8. Thanks a lot 4 your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1