?
Solved

Object Required

Posted on 2007-03-22
8
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 93

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 93

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 93

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 93

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 93

Accepted Solution

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

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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

765 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