Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to call Excel spreadsheet in a function

Status
Not open for further replies.

mastro78

Programmer
Apr 10, 2007
70
US
I know how to call/set CurrentDb and open the table I'm working with. Just curious as to how to do the same thing with an Excel worksheet and the associated cell data. Thanks.
 
Try using get object -

dim objXL as object
set objXL = getobject("","Excel.application")


 




Hi,

Example...
Code:
Function GetNbrRes(sResource As String)
    Dim sConn As String, sSQL As String
    Dim rst As ADODB.Recordset, cnn As ADODB.Connection
    Dim sPath As String, sDB As String
    Dim sPrevCC As String, BCC As Boolean
    
    sPath = "D:\My Documents\_Databases\_Excel"
    sDB = "ResourceData"
    
    Set cnn = New ADODB.Connection
    
    sConn = "Provider=MSDASQL.1;"
    sConn = sConn & "Persist Security Info=False;"
    sConn = sConn & "Extended Properties=""DSN=Excel Files;"
    sConn = sConn & "DBQ=" & sPath & "\" & sDB & ".xls;"
    sConn = sConn & "DefaultDir=" & sPath & ";"
    sConn = sConn & "DriverId=790;MaxBufferSize=2048;PageTimeout=5;"""
    
    cnn.Open sConn
    
    Set rst = New ADODB.Recordset
    
    sSQL = "SELECT ResCC "
    sSQL = sSQL & "FROM `" & sPath & "\" & sDB & "`.`ResourceData$` A "
    sSQL = sSQL & "WHERE Left(ResCC,1)='5' "
    sSQL = sSQL & "  AND Resource Like '%" & sResource & "%' "
    sSQL = sSQL & "Order By ResCC "
        
    With rst
        .Open sSQL, cnn, adOpenStatic, adLockReadOnly, adCmdText

        .MoveFirst
        GetNbrRes = rst(0)
        .Close
    End With
    cnn.Close
    
    Set rst = Nothing
    Set cnn = Nothing
End Function


Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
I'm trying to fit this into my current OnOpen event where I'm gathering the Excel file info (name,date modified). Thanks Skip.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top