Solved

Object Required

Posted on 2007-03-22
8
183 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now