Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object Required

Posted on 2007-03-22
8
Medium Priority
?
225 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to implement Singleton Design Pattern in Java.
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…
Suggested Courses

650 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