What's wrong with this code?...

B

Baby Face Lee

Hi guys,
Can you help me with this. Below is some code that I got from somewhere
(apologies but I can't remember where now), which I've tried to adapt to do
what I want.
I'm trying up update a column of figures within Excel from a COLUMN of
figures within an Access Query. I use the unaltered version of this function
alot which updates a column within Excel via a single row of figures in an
Access Query.
As I say, I'm trying to get this code to loop through the (58) records that
are in a single column in Access.
I currently get Error 3265 'Iten not found in this collection'. What's wrong?

Function fColumnToExcel(strQuery As String, strPath As String, intSheet As
Integer, fldColumn As Integer, fldRow As Integer) As Variant

On Error GoTo E_Handle

Dim db As DAO.Database, rs As DAO.Recordset
Dim c As Integer, r As Integer, i As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim xl As Object
Dim Workbook As Object
Dim Sheet As Object

Set db = DBEngine.Workspaces(0).Databases(0)

RsSql = "SELECT * FROM " & strQuery & ";"
Set rs = db.OpenRecordset(RsSql, dbOpenDynaset)
Set xl = CreateObject("Excel.Application")
Set Workbook = xl.Workbooks.Open(strPath)
Set Sheet = xl.activeworkbook.sheets(intSheet)

c = fldColumn
r = fldRow
i = 0

' Loop through the query's records and copy the records
' to the Excel spreadsheet.

Do Until i = rs.RecordCount
CurrentField = rs(i)
Sheet.cells(r, c).Value = CurrentField
r = r + 1
i = i + 1
Loop

xl.Application.activeworkbook.Save

Set Sheet = Nothing
xl.Quit
Set xl = Nothing

sExit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Function

Many thanks!
Lee
 
D

Dan Artuso

Hi,
Please tell us which line is causing the error.
Is it:
CurrentField = rs(i)

And what is strQuery
 
B

Baby Face Lee

Hi Dan,
The strQuery is the query from which the data is 'transferred' to Excel.
In this case it's 'qry_CountCentresBy1st2Dig', the SQL of which is:
SELECT Count(tbl_Centres.CentreNo) AS CountOfCentreNo
FROM tbl_Centres
GROUP BY Left([CentreNo],2);

As far as I can tell, yes, the problem is with
CurrentField = rs(i)

Lee
 
D

Dan Artuso

Hi,
Well, the query only retirns one field so rs(1) doesn't exist hence:
item not found in this collection. The collection is zero based so...
rs(0) is what you want.

--
HTH
-------
Dan Artuso, MVP


Baby Face Lee said:
Hi Dan,
The strQuery is the query from which the data is 'transferred' to Excel.
In this case it's 'qry_CountCentresBy1st2Dig', the SQL of which is:
SELECT Count(tbl_Centres.CentreNo) AS CountOfCentreNo
FROM tbl_Centres
GROUP BY Left([CentreNo],2);

As far as I can tell, yes, the problem is with
CurrentField = rs(i)

Lee

Dan Artuso said:
Hi,
Please tell us which line is causing the error.
Is it:
CurrentField = rs(i)

And what is strQuery
 
B

Baby Face Lee

Thanks, I've set this to zero as you say but I need it to loop through the
records within the query which it doesn't do. I'm rather flakey on using
Recordsets!

Dan Artuso said:
Hi,
Well, the query only retirns one field so rs(1) doesn't exist hence:
item not found in this collection. The collection is zero based so...
rs(0) is what you want.

--
HTH
-------
Dan Artuso, MVP


Baby Face Lee said:
Hi Dan,
The strQuery is the query from which the data is 'transferred' to Excel.
In this case it's 'qry_CountCentresBy1st2Dig', the SQL of which is:
SELECT Count(tbl_Centres.CentreNo) AS CountOfCentreNo
FROM tbl_Centres
GROUP BY Left([CentreNo],2);

As far as I can tell, yes, the problem is with
CurrentField = rs(i)

Lee

Dan Artuso said:
Hi,
Please tell us which line is causing the error.
Is it:
CurrentField = rs(i)

And what is strQuery

--
HTH
-------
Dan Artuso, MVP


Hi guys,
Can you help me with this. Below is some code that I got from somewhere
(apologies but I can't remember where now), which I've tried to adapt to do
what I want.
I'm trying up update a column of figures within Excel from a COLUMN of
figures within an Access Query. I use the unaltered version of this function
alot which updates a column within Excel via a single row of figures in an
Access Query.
As I say, I'm trying to get this code to loop through the (58) records that
are in a single column in Access.
I currently get Error 3265 'Iten not found in this collection'. What's wrong?

Function fColumnToExcel(strQuery As String, strPath As String, intSheet As
Integer, fldColumn As Integer, fldRow As Integer) As Variant

On Error GoTo E_Handle

Dim db As DAO.Database, rs As DAO.Recordset
Dim c As Integer, r As Integer, i As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim xl As Object
Dim Workbook As Object
Dim Sheet As Object

Set db = DBEngine.Workspaces(0).Databases(0)

RsSql = "SELECT * FROM " & strQuery & ";"
Set rs = db.OpenRecordset(RsSql, dbOpenDynaset)
Set xl = CreateObject("Excel.Application")
Set Workbook = xl.Workbooks.Open(strPath)
Set Sheet = xl.activeworkbook.sheets(intSheet)

c = fldColumn
r = fldRow
i = 0

' Loop through the query's records and copy the records
' to the Excel spreadsheet.

Do Until i = rs.RecordCount
CurrentField = rs(i)
Sheet.cells(r, c).Value = CurrentField
r = r + 1
i = i + 1
Loop

xl.Application.activeworkbook.Save

Set Sheet = Nothing
xl.Quit
Set xl = Nothing

sExit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Function

Many thanks!
Lee
 
D

Dan Artuso

Hi,
Where did you get this code??

Try:
Do While Not rs.EOF
CurrentField = rs(0)
Sheet.cells(r, c).Value = CurrentField
rs.MoveNext
Loop


--
HTH
Dan Artuso, Access MVP


Baby Face Lee said:
Thanks, I've set this to zero as you say but I need it to loop through the
records within the query which it doesn't do. I'm rather flakey on using
Recordsets!

Dan Artuso said:
Hi,
Well, the query only retirns one field so rs(1) doesn't exist hence:
item not found in this collection. The collection is zero based so...
rs(0) is what you want.

--
HTH
-------
Dan Artuso, MVP


Baby Face Lee said:
Hi Dan,
The strQuery is the query from which the data is 'transferred' to Excel.
In this case it's 'qry_CountCentresBy1st2Dig', the SQL of which is:
SELECT Count(tbl_Centres.CentreNo) AS CountOfCentreNo
FROM tbl_Centres
GROUP BY Left([CentreNo],2);

As far as I can tell, yes, the problem is with
CurrentField = rs(i)

Lee

:

Hi,
Please tell us which line is causing the error.
Is it:
CurrentField = rs(i)

And what is strQuery

--
HTH
-------
Dan Artuso, MVP


Hi guys,
Can you help me with this. Below is some code that I got from somewhere
(apologies but I can't remember where now), which I've tried to adapt to do
what I want.
I'm trying up update a column of figures within Excel from a COLUMN of
figures within an Access Query. I use the unaltered version of this function
alot which updates a column within Excel via a single row of figures in an
Access Query.
As I say, I'm trying to get this code to loop through the (58) records that
are in a single column in Access.
I currently get Error 3265 'Iten not found in this collection'. What's wrong?

Function fColumnToExcel(strQuery As String, strPath As String, intSheet As
Integer, fldColumn As Integer, fldRow As Integer) As Variant

On Error GoTo E_Handle

Dim db As DAO.Database, rs As DAO.Recordset
Dim c As Integer, r As Integer, i As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim xl As Object
Dim Workbook As Object
Dim Sheet As Object

Set db = DBEngine.Workspaces(0).Databases(0)

RsSql = "SELECT * FROM " & strQuery & ";"
Set rs = db.OpenRecordset(RsSql, dbOpenDynaset)
Set xl = CreateObject("Excel.Application")
Set Workbook = xl.Workbooks.Open(strPath)
Set Sheet = xl.activeworkbook.sheets(intSheet)

c = fldColumn
r = fldRow
i = 0

' Loop through the query's records and copy the records
' to the Excel spreadsheet.

Do Until i = rs.RecordCount
CurrentField = rs(i)
Sheet.cells(r, c).Value = CurrentField
r = r + 1
i = i + 1
Loop

xl.Application.activeworkbook.Save

Set Sheet = Nothing
xl.Quit
Set xl = Nothing

sExit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Function

Many thanks!
Lee
 
C

Carl Rapson

Hi,

In place of

CurrentField = rs(i)

you may want to use

CurrentField = rs.Fields(i)

to return the field at position i.

HTH,

Carl Rapson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top