Solved

Object Required

Posted on 2007-03-22
8
213 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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