473,471 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Scrambled Excel Automation

I ran into something really bizzaro today.

A team member had me look into some export code for some data he was
outputting. The data looks at shipping containers and attempts to look
at how often their being reused. Details are output according to the
maximum number of times the same container was used, up to a total of
six moves.

The code we're executing is so base-simple that it's second nature to
us. We have a select query that formats the data for output, and has a
static variable that corresponds to the maximum times the container was
used. Then, through code (see loop snippet at end of post), we loop
through the potential "max moves", and export the "format" query all
into the same workbook.

The bizzaro thing: As often as we've done this in the past (and our
users have similar routines running all day long), this is COMPLETELY
scrambling the data with each pass.

Pass 1: "Moves1" looks fine.
Pass 2: "Moves2" looks fine, but "Moves1" has some orders jumbled.
Pass 3: "Moves3" looks fine, "Moves2" has some orders jumbled,
"Moves1" is even more jumbled.
Pass 4: "Moves4" looks fine, "Moves3" has some orders jumbled,
"Moves2" is even more jumbled, and "Moves 1" is barely recognizable.

By the time pass 6 six hits, there are dozens of duplications in
Moves1. Data from completely different columns are moved around. The
original column headings are obliterated.

The code "ExportToXLS" basically runs DoCmd.TransferSpreadsheet, and
does some bolding, but does NO manipulation of actual data.

The only thing I can think of us the monstrosity of data in this
output. "Moves1" is 38K records, then 12K, 4K, 1K, 400, and "Moves6"
has 100 records.

Has anyone else encountered something like this? Insight appreciated.

Thanks,
Anthony.
For intX = 1 To 6
Call StaticInteger(intX)
strTempQuery = "Moves" & intX
' Delete the Temp Query (in case it didn't get deleted last time)
Call KillObject(strTempQuery, acQuery)
strSQL = "SELECT [BRT - qryTemp].* FROM [BRT - qryTemp];"
Set qdfBRT = dbBRT.CreateQueryDef(strTempQuery, strSQL)
Call ExportToXLS(strTempQuery, strOutputPath & strOutputFile)
'Cleanup - kill the Temp Query
Call KillObject(strTempQuery, acQuery)
Next intX

Nov 13 '05 #1
3 2180
On 17 Feb 2005 18:23:22 -0800, "ac*******@railvan.com"
<ac*******@railvan.com> wrote:

Your code looks clean enough.
One thing I might add is:
dbBRT.Querydefs.Refresh
after the KillObject call.
Another thing: you are creating a QueryDef object, and not cleaning it
up. Add:
qdfBRT.Close
set qdfBRT = Nothing

-Tom.
<clip>

For intX = 1 To 6
Call StaticInteger(intX)
strTempQuery = "Moves" & intX
' Delete the Temp Query (in case it didn't get deleted last time)
Call KillObject(strTempQuery, acQuery)
strSQL = "SELECT [BRT - qryTemp].* FROM [BRT - qryTemp];"
Set qdfBRT = dbBRT.CreateQueryDef(strTempQuery, strSQL)
Call ExportToXLS(strTempQuery, strOutputPath & strOutputFile)
'Cleanup - kill the Temp Query
Call KillObject(strTempQuery, acQuery)
Next intX


Nov 13 '05 #2
Inside of the code that runs KillObject, the QueryDef.Refresh occurs.

The qdf.close isn't there, but the = Nothing happens in the exit
section of the code.

Still doesn't explain the transpositioning of the data.

Nov 13 '05 #3
If you are using TransferSpreadsheet in your code, then that is where
your problem is. My experience with TransferSpreadsheet is that it
works reliably only once on a NEW workbook. If you use it again and
again on the same workbook or in a loop, the results become
unpredictable. Anyway, that has been my experience.

My workaround has been to use Com ADO to write data to Excel from
Access. You can use DAO with DBEngine, but for me, the easiest way was
ADO (make a reference to Microsoft ActiveX Data Objects Library 2.5 or
2.6).

Here is a sample:

---------------------------------------------------
Sub WriteDataToExcelWithADO()
Dim cn As New ADODB.Connection, RS As New ADODB.Recordset
Dim i As Integer, DB As Database, RS1 As DAO.Recordset

Set DB = CurrentDb

strSourcePath = Left(DB.Name, Len(DB.Name) - Len(Dir(DB.Name)))
'---note: need semicolon at end of .xls; for ADO to Excel
strSourcePath = strSourcePath & "TestWorkBook.xls;"
RS.CursorLocation = adUseClient
cn.Mode = adModeReadWrite
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strSourcePath & _
"Extended Properties=""Excel 8.0;HDR=NO;"""

Set RS1 = DB.OpenRecordset("tbl1")

j = 2
DoEvents
Do While Not RS1.EOF
strSql = "SELECT * FROM [Sheet1$A" & j & ":U" & j & "]"
RS.Open strSql, cn, adOpenDynamic, adLockPessimistic
For i = 0 To RS1.Fields.Count - 1
RS(k) = RS1(k)
Next
RS.Update
RS.Close
j = j + 1
RS1.MoveNext
Loop
RS1.Close
cn.Close
End Sub

--------------------------------------------

Here are some caveats. You have to prep the Excel workbook. Unlike
TransferSpreadsheet, you can rewrite and also write to any cell in the
Spreadsheet. But you have to plant some fake data in the spreadsheet
cells you need to write to like this:

ColA ColB ColC ColD
Row1 sdfs sdf sdf sdf
Row2 sdf sdf sdf sdf
Row3 sdf sdf sdf sdf

having the fake data prevents an apostrophe from appearing in front of
the data

ColA ColB ColC ColD
Row1 '1 '3 '34 '5
Row2 '45 '67 '5 '9
Row3 '0 '90 '36 '78

If you don't prep with the fake data and you try to sum a column with
the data you just passed in, it won't work.

Also, ADO only writes one row at a time. So you select one row in your
range at a time
j = 2
strSql = "SELECT * FROM [Sheet1$A" & j & ":U" & j & "]"

j is the row counter. Here the range is A2:U2

You have to close your ADO recordset, RS.Close after writing a row of
data. Then MoveNext for RS1 (dao recordset) and increment j (j = 3,
etc).

If this seems tedious and not worth the work, I have dozens of programs
that write thousands of rows of data from Access to dozens of Excel
workbooks on a daily basis. These programs run continuously 7 days a
week hands off/unattended. Com ADO has been serving me reliably for
over 5 years with this method. So I just thought I would share.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
2
by: jeffgeorge | last post by:
I'm currently exporting a form to Excel. Because there are controls and totals in the header, I first have a button for users to convert to a datasheet. Then I use the automated quick office...
7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
1
by: cybertof | last post by:
Hello, Is there a way to connect (through automation) a c# application to a running Excel 2003 instance on a specific workbook ? In the past, i used to use GetObject(...) function in VB6. ...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
12
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report...
3
by: Carlos Magalhaes | last post by:
Hey All, I am doing some excel automation using the excel COM. I can do most of the functions and its working well until I come across a formula. I can run a formula and insert the formula...
3
by: Mitchell Vincent | last post by:
Does anyone have some good examples of Excel automation with (VB).NET? I have some Excel spreadsheets that a customer needs parsed out but I've never tried to use Excel programatically before! ...
6
by: a.theil | last post by:
Please help! I need a simple excel automation, just 2 write some files into excel. I do: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.