Results 1 to 7 of 7
  1. #1
    tennisbuck is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2012
    Posts
    51

    Calling function from Excel

    Hello,

    How exactly could I modify the code below so I can call slope, intercept, and R squared from excel into access. My Y values and X values are set up like the example below. My X values are the month Names and Y values are the sums: I probably would have to return a value instead of message box also.

    November October Septermber August July June May April March February January
    0 0 100 50 0.68 0.39 0.21 0.35 0.64 0.24 0.07
    Sub xlMedian()


    Dim obj As Excel.Application
    Set obj = CreateObject("Excel.Application")
    MsgBox obj.Application.Median(1, 2, 5, 8, 12, 13)
    obj.Quit
    Set obj = Nothing
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    What do you mean by 'call slope, intercept, and R squared from excel into access'? You want to use Excel functions in Access VBA?

    Maybe you need to open a recordset then use fields of the recordset in the expression.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    tennisbuck is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2012
    Posts
    51
    Yep. I would like to use the excel function slope in access vba. This excel formula requires two arrays a X and Y and I'm unsure how to write it into vba for access.

  4. #4
    tennisbuck is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2012
    Posts
    51
    Maybe this will help. The code was written for everything to be in columns where my data is set up in rows. I've also saved the DB that the code works in below.

    X November October September August July June May April March Feb Jan
    Y 0 0 100 50 0.68 0.39 0.21 0.35 0.64 0.24 0.07
    Y 1000 1300.02 100 50 0.98 0.35 0.87 0.13 0.16 0.90 0.85
    Y 1000 1300.02 100 50 0.98 0.35 0.87 0.13 0.16 0.90 0.85
    Y 0 0 100 50 0.34 0.34 0.55 0.17 0.38 0.51 0.77
    Y 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 99999
    Y 20000 5263.74 100 50 0.92 0.67 0.22 0.42 0.52 0.27 0.12
    Y 500000 21830.49 100 50 0.87 0.22 0.69 0.22 0.42 0.49 0.02
    Y 0 0 100 50 0.00 0.40 0.90 0.25 0.04 0.12 0.04
    Y 120000 6385.47 100 50 0.17 0.68 0.85 0.09 0.78 0.98 0.81
    Y 100000 22876.02 100 50 0.28 0.07 0.56 0.11 0.94 0.95 0.16
    Y 100000 130512.95 100 50 0.58 0.96 0.16 0.85 0.69 0.48 0.19


    Code:
    Option Compare Database
    
    
    
    
    
    Enum SimpleRegressStat
        SimpleRegressStat_All = 0
        SimpleRegressStat_RSquared = 1
        SimpleRegressStat_XCoeff = 2
        SimpleRegressStat_Intercept = 3
        SimpleRegressStat_SE_Resid = 4
        SimpleRegressStat_SE_XCoeff = 5
        SimpleRegressStat_SE_Intercept = 6
        SimpleRegressStat_T_XCoeff = 7
        SimpleRegressStat_T_Intercept = 8
    End Enum
    
    
    Function DSimpleRegress(X_Column As String, Y_Column As String, Tbl As String, _
        Optional Criteria As String = "", Optional Stat As Variant = "")
        
        ' Function by Patrick G. Matthews
        
        ' Feel free to use and distribute this code, so long as you credit authorship and indicate the
        ' URL where you found it
        
        ' This function calculates statistics for simple linear regression between a single independent
        ' variable (X_Column) and a dependent variable (Y_Column), and is intended for use in Microsoft
        ' Access.  This function requires a reference to the Microsoft DAO library.
        
        ' Depending on the value of the Stat argument, this function returns a single regression
        ' statistic, such as the R squared or the X coefficient, or it returns an array of all
        ' available regression stats (for a list of available stats, please see the enumeration
        ' SimpleRegressStat)
        
        ' If either or both corresponding values in the paired X, Y data set are null, those records
        ' are ignored
        
        ' This function is labeled according to the domain aggregate function naming convention as it
        ' behaves similarly to the other domain aggregates
        
        ' X_Column is the independent variable
        ' Y_Column is the dependent variable
        ' Tbl is the source table or query for the data
        ' Criteria defines any filtering criteria you wish to apply to the data set.  Be sure to enclose
        '       text items in single quotes and date values in the # date qualifiers
        ' Stat determines which regression statistic the function returns.  For a full list of the valid
        '       values (NOT case sensitive) for the Stat argument, see the Select Case structure under
        '       the label DetermineMode
        
        ' For each of the arguments, I strongly recommend that you encase column and table names in
        ' square brackets.  This is mandatory of the column/table name does not follow the usual rules
        ' for naming database objects
        
        Static Last_X_Column As String
        Static Last_Y_Column As String
        Static Last_Tbl As String
        Static Last_Criteria As String
        Static Last_Runtime As Date
        Static Result_RSquared As Variant
        Static Result_XCoeff As Variant
        Static Result_Intercept As Variant
        Static Result_SE_Resid As Variant
        Static Result_SE_XCoeff As Variant
        Static Result_SE_Intercept As Variant
        Static Result_T_XCoeff As Variant
        Static Result_T_Intercept As Variant
        
        Dim N As Long
        Dim AvgX As Variant
        Dim AvgY As Variant
        Dim AvgXY As Variant
        Dim VarPX As Variant
        Dim VarPY As Variant
        Dim Covar As Variant
        
        Dim SQL As String
        Dim rs As DAO.Recordset
        Dim Mode As SimpleRegressStat
        Dim Results(1 To 8) As Variant
        
        Const ForceRefreshSeconds As Long = 30
        
        On Error GoTo ErrHandler
        
    DetermineMode:
        
        ' Determines whether a single regression stat is returned (and if so, which), or whether
        ' an array of all available stats is returned
        
        Select Case LCase(Stat)
            Case "1", "r squared", "rsquared", "r sq", "rsq", "r square", "rsquare", "r-squared", "r-squared", _
                "r-sq", "r-sq", "r-square", "r-square"
                Mode = SimpleRegressStat_RSquared
            Case "2", "x", "x coefficient", "x coeff", "xcoeff", "coeff", "coefficient"
                Mode = SimpleRegressStat_XCoeff
            Case "3", "intercept", "constant"
                Mode = SimpleRegressStat_Intercept
            Case "4", "se model", "se regression", "se resid", "se residual", "se residuals", "std error model", _
                "std error regression", "std error resid", "std error residual", "std error residuals", _
                "standard error model", "standard error regression", "standard error resid", _
                "standard error residual", "standard error residuals"
                Mode = SimpleRegressStat_SE_Resid
            Case "5", "se x", "se x coefficient", "se x coeff", "se xcoeff", "se coeff", "se coefficient", _
                "std error x", "std error x coefficient", "std error x coeff", "std error xcoeff", _
                "std error coeff", "std error coefficient", "standard error x", "standard error x coefficient", _
                "standard error x coeff", "standard error xcoeff", "standard error coeff", _
                "standard error coefficient"
                Mode = SimpleRegressStat_SE_XCoeff
            Case "6", "se intercept", "se constant", "std error intercept", "std error constant", _
                "standard error intercept", "standard error constant"
                Mode = SimpleRegressStat_SE_Intercept
            Case "7", "t x", "t x coefficient", "t x coeff", "t xcoeff", "t coeff", "t coefficient"
                Mode = SimpleRegressStat_T_XCoeff
            Case "8", "t intercept", "t constant"
                Mode = SimpleRegressStat_T_Intercept
            Case Else
                Mode = SimpleRegressStat_All
        End Select
        
    CalculateStats:
        
        ' Calculate the regression stats
        
        ' This function holds the regression stats in static variables, which retain their state
        ' between calls.  If the values for the X_Column, Y_Column, Tbl, and Criteria arguments
        ' are the same as those for the last call, and if the seconds elapsed since the last
        ' call are less than what is specified in the ForceRefreshSeconds constant, then we can
        ' skip the calculations and go right to assigning the return value
        
        If DateDiff("s", Last_Runtime, Now) >= ForceRefreshSeconds Or Last_X_Column <> X_Column Or _
            Last_Y_Column <> Y_Column Or Last_Tbl <> Tbl Or Last_Criteria <> Criteria Then
            
            ' Initialize stats to null
            
            Result_RSquared = Null
            Result_XCoeff = Null
            Result_Intercept = Null
            Result_SE_Resid = Null
            Result_SE_XCoeff = Null
            Result_SE_Intercept = Null
            Result_T_XCoeff = Null
            Result_T_Intercept = Null
        
            ' All the regression stats can be calculated from the following six values: N, Avg(X), Avg(Y),
            ' Avg(X * Y), VarP(X), and VarP(Y).  Use the following SQL statement to get these six values
            
            SQL = "SELECT Count(1) AS N, Avg(" & X_Column & ") AS AvgX, Avg(" & Y_Column & ") AS AvgY, " & _
                "Avg(" & X_Column & " * " & Y_Column & ") AS AvgXY, VarP(" & X_Column & ") AS VarPX, " & _
                "VarP(" & Y_Column & ") AS VarPY " & _
                "FROM " & Tbl & " " & _
                "WHERE " & IIf(Trim(Criteria) <> "", Criteria & " And ", "") & X_Column & " Is Not Null " & _
                    "And " & Y_Column & " Is Not Null"
            
            Set rs = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)
            
            ' Transfer values from recordset to variables, then close recordset
            
            AvgX = CDbl(rs!AvgX)
            AvgY = CDbl(rs!AvgY)
            AvgXY = CDbl(rs!AvgXY)
            N = rs!N
            VarPX = CDbl(rs!VarPX)
            VarPY = CDbl(rs!VarPY)
            Covar = AvgXY - AvgX * AvgY
            
            rs.Close
            
            ' There must be at least 3 valid data points for regression to work.  If there are 2 or
            ' fewer data points, we skip the rest of the calculations, thus allowing the regression
            ' stats to remain as null
            
            If N > 2 Then
                
                ' Calculate various stats
                
                Result_RSquared = Covar ^ 2 / (VarPX * VarPY)
                
                Result_XCoeff = Covar / VarPX
                
                Result_Intercept = AvgY - AvgX * Result_XCoeff
                
                Result_SE_Resid = ((N / (N - 2)) * (VarPY - Covar ^ 2 / VarPX)) ^ 0.5
                
                Result_SE_XCoeff = Result_SE_Resid * (1 / (N * VarPX)) ^ 0.5
                
                Result_SE_Intercept = Result_SE_Resid * ((VarPX + AvgX ^ 2) / (N * VarPX)) ^ 0.5
                
                Result_T_XCoeff = Result_XCoeff / Result_SE_XCoeff
                
                Result_T_Intercept = Result_Intercept / Result_SE_Intercept
                
            End If
            
        End If
        
    ReturnValue:
        
        ' Set the fnction's return value
        
        Select Case Mode
            Case SimpleRegressStat_All
                Results(1) = Result_RSquared
                Results(2) = Result_XCoeff
                Results(3) = Result_Intercept
                Results(4) = Result_SE_Resid
                Results(5) = Result_SE_XCoeff
                Results(6) = Result_SE_Intercept
                Results(7) = Result_T_XCoeff
                Results(8) = Result_T_Intercept
                DSimpleRegress = Results
            Case SimpleRegressStat_RSquared
                DSimpleRegress = Result_RSquared
            Case SimpleRegressStat_XCoeff
                DSimpleRegress = Result_XCoeff
            Case SimpleRegressStat_Intercept
                DSimpleRegress = Result_Intercept
            Case SimpleRegressStat_SE_Resid
                DSimpleRegress = Result_SE_Resid
            Case SimpleRegressStat_SE_XCoeff
                DSimpleRegress = Result_SE_XCoeff
            Case SimpleRegressStat_SE_Intercept
                DSimpleRegress = Result_SE_Intercept
            Case SimpleRegressStat_T_XCoeff
                DSimpleRegress = Result_T_XCoeff
            Case SimpleRegressStat_T_Intercept
                DSimpleRegress = Result_T_Intercept
        End Select
        
        Last_Runtime = Now
        
        GoTo Cleanup
        
    ErrHandler:
        DSimpleRegress = CVErr(Err.Number)
        
    Cleanup:
        Set rs = Nothing
    End Function
    Attached Files Attached Files

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I would like to use the excel function slope in access vba.
    You don't need to use
    Code:
    Dim obj As Excel.Application
          Set obj = CreateObject("Excel.Application")
    In the IDE, set a reference to Microsoft Excel 14.0 Object Library"
    Note: Excel 14.0 is for Access 2010. Select the Excel Object Library for A2007 (13.0??)


    As far as how to use the functions, I don't have a clue.
    But once you set the reference, you can use the Excel functions you want just like you would use built in functions in Access (ex. Date() or Time() ).

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I find it a little more complicated than that. Have to declare the Excel object in code OR set the Excel library reference. Then call Excel functions like:

    Excel.Application.WorksheetFunction.Slope(Array(1, 2,3,4,5,6),Array(4,5,3,6,5,8))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    tennisbuck is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2012
    Posts
    51
    Thanks to both of you. I actually used a Union query that June 7 suggested in a different thread I had awhile back to run the code. Thanks and Happy New Years!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calling a Private Function from Another Form
    By MintChipMadness in forum Programming
    Replies: 1
    Last Post: 01-07-2013, 12:08 PM
  2. Calling a Function From A Form.
    By ksmith in forum Access
    Replies: 2
    Last Post: 06-07-2012, 02:23 PM
  3. Calling A Module Function To Open A Form
    By orcinus in forum Modules
    Replies: 3
    Last Post: 09-29-2010, 04:43 PM
  4. calling function
    By ManvinderKaur in forum Programming
    Replies: 3
    Last Post: 07-22-2010, 10:53 PM
  5. Calling a function and returning a value
    By 3dmgirl in forum Programming
    Replies: 0
    Last Post: 04-23-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums