Solved

Object Required

Posted on 2007-03-22
8
200 Views
Last Modified: 2010-08-05
Hi: Can any one please tell me why it gives error: Object Required
on line: Set rs = CurrentDB.OpenRecordset(sSQL)

ORIGINAL CODE
Private Sub ProcessIntoExcell()
Dim rs As DAO.Recordset
Dim DBConn
Dim oConn
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim rngColumn As Excel.Range
Dim rngCell As Excel.Range
Dim strTarget As String
Dim mysheet As String
Dim lngRow As Long
'ProgressBar1.Value = 1
'ProgressBar1.Value = 10
'ProgressBar1.Value = 20
'ProgressBar1.Value = 30

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=SQLOLEDB; Data Source=MS0062K\DEVELOPMENT; Initial Catalog=AccountDistribution; User ID=sa"
Dim sSQL As String
sSQL = "select name, birth, death from tblinfo"
Set rs = CurrentDB.OpenRecordset(sSQL)
Set WShell = CreateObject("wscript.shell")
vPath = WShell.SpecialFolders("MyDocuments") & "\"
strTarget = vPath & "Deceased-Information-" & Format$(Now, "yyyymmdd-hhmmss") & ".xls"
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Add 'this creates a new, blank workbook
mysheet = xlApp.Worksheets(1).Name
xlApp.Columns("A:H").EntireColumn.AutoFit
Set xlWS = xlWb.Worksheets(mysheet)
xlWS.Range("A1").Value = "NAME"
xlWS.Range("B1").Value = "BIRTH"
xlWS.Range("C1").Value = "DEATH"
lngRow = 2
Do While Not rs.EOF
    'If ProgressBar1.Value >= 100 Then ProgressBar1.Value = 0
    'ProgressBar1.Value = ProgressBar1.Value + 1
    xlWS.Cells(lngRow, 1).Value = rs!Name
    xlWS.Cells(lngRow, 2).Value = rs!Birth
    xlWS.Cells(lngRow, 3).Value = rs!Death
    lngRow = lngRow + 1
    rs.MoveNext
Loop

xlWb.Close SaveChanges:=True, FileName:=strTarget
Set xlWb = Nothing
Set xlWS = Nothing
xlApp.Quit
Set xlApp = Nothing
ShellExecute 0, vbNullString, Chr(34) & strSource & Chr(34), vbNullString, strSource, vbMaximizedFocus
End Sub
0
Comment
Question by:mustish1
  • 5
  • 3
8 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18771153
CurrentDB is a DAO object, and I am guessing that this code is not running from an Access MDB file (or that if it is, you do not have a reference set to one of the DAO version libraries).

Patrick
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18771162
OK, I kind of grabled that.  Basically, the CurrentDB object is not set yet.  Where does this code reside?
0
 

Author Comment

by:mustish1
ID: 18771225
Code is in VB6 and connection is with SQL Server database name AccountDistribution
Thanks.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18771392
Private Sub ProcessIntoExcell()

Dim rs As Object
Dim oConn As Object
Dim xlApp As Excel.Application
Dim strTarget As String
Dim sSQL As String

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=SQLOLEDB; Data Source=MS0062K\DEVELOPMENT; Initial Catalog=AccountDistribution; User ID=sa"
sSQL = "select name, birth, death from tblinfo"
Set rs = CreateObject("ADODB.Recordset")
rs.Open sSQL, oConn
Set WShell = CreateObject("wscript.shell")
vPath = WShell.SpecialFolders("MyDocuments") & "\"
strTarget = vPath & "Deceased-Information-" & Format$(Now, "yyyymmdd-hhmmss") & ".xls"
Set xlApp = New Excel.Application
With xlApp.ActiveSheet
    .[a1:c1] = Array("NAME", "BIRTH", "DEATH")
    .[a2].CopyFromRecordset rs
    .Columns("A:H").EntireColumn.AutoFit
End With

xlApp.ActiveWorkbook.Close SaveChanges:=True, FileName:=strTarget
xlApp.Quit
Set xlApp = Nothing
rs.Close
Set rs = Nothing
Set oConn = Nothing

ShellExecute 0, vbNullString, Chr(34) & strSource & Chr(34), vbNullString, strSource, vbMaximizedFocus
End Sub
0
 

Author Comment

by:mustish1
ID: 18771465
Thanks. It gives error on line:
    .[a1:c1] = Array("NAME", "BIRTH", "DEATH")
Object variable or with block variable not set
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18771692
Change:

With xlApp.ActiveSheet

to:

With xlApp.ActiveWorkbook.Worksheets(1)
0
 

Author Comment

by:mustish1
ID: 18771749
Thanks. It still gives same message
Object variable with block variable not set
on line
With xlApp.ActiveWorkbook.Worksheets(1)

I try this too but again same message
With xlApp.ActiveWorkbook.ActiveSheet
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 18771986
Sorry!

With xlApp.Workbooks.Add.Worksheets(1)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This video teaches viewers about errors in exception handling.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question