Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default clear rows and insert records from recordset on refresh

Hi Everyone,

I need some assistance with trying to achieve what I'm wanting to do.

I have a worksheet that has a series of headings and a row that sums
up the totals. I am guessing that there will be further calculations
but for now I'm trying to achieve the first step which is to delete
the existing data and insert the new data.

For example,

If I want to look at a certain branch's data, I insert their account
number and refresh the calculation. This will bring up all the
records associated with that branch and provide subtotals.

I have managed to succesfully create an ADO connection to an MS Access
database with the code below, but unfortunatley for me it's not
clearing the data and I can't seem to find how I go about doing it.

I did think of using .XLDOWN to delete the existing data and .INSERT
to insert the data but I have a feeling that there is something more
practicable to do this.

Any assistance would be appreciated.

<CODE

Sub import_salaries()
Dim conn As New Connection, rec As New Recordset
Dim ws As Worksheet
Dim sql$, i&

Set ws = ThisWorkbook.Worksheets("Sheet1")

conn.Open "Provider=microsoft.jet.oledb.4.0;" + _
"Data Source=" + ThisWorkbook.Path + "\HR_Occupancy_Table.mdb"

' T3Lookup = ws.Range("CCLOOKUP").Value

sql = "SELECT Employee_No, Employee_Name, Cost_Centre_Description,
Salary_Year, T3_CC_Lookup " & _
"FROM HR_OCCUPANCY WHERE T3_CC_Lookup = " &
ws.Range("CCLOOKUP").Value & " ORDER BY Cost_Centre_Description "

rec.Open sql, conn

While Not rec.EOF
i = i + 1
ws.[A5].Cells(i) = rec!Employee_No
ws.[B5].Cells(i) = rec!Employee_Name
ws.[C5].Cells(i) = rec!Cost_Centre_Description
ws.[D5].Cells(i) = rec!Salary_Year
ws.[E5].Cells(i) = rec!T3_CC_Lookup
rec.MoveNext
Wend
rec.Close: conn.Close


End Sub

</CODE

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default clear rows and insert records from recordset on refresh

I take that you don't want to perform row-by-row deletions/insertions.

You can use the adOpenStatic CursorType to get a scrollable recordset, then
use RecordCount to figure out how many rows to clear/insert. Then you can
clear out data in one action...much more efficient than deleting data one
row at a time.

rec.Open sql, conn, adOpenStatic

ws.Range("A5").EntireRow.Resize(rec.RecordCount).I nsert shift:=xlDown

' Use RecordCount in conjunction with xlDown to know how many rows to
batch delete/insert before adding the recordset.

While Not rec.EOF
... rest of code

I would imagine you would use a combination of xlDown and Insert to know how
many rows to delete beforehand. Hard to give more advice on where to put the
data without more specifics. Hope you can adapt this, if not post back with
more details.


--
Tim Zych
www.higherdata.com
Compare data in Excel and find differences with Workbook Compare
A free, powerful, flexible Excel utility


"Forgone" wrote in message
...
Hi Everyone,

I need some assistance with trying to achieve what I'm wanting to do.

I have a worksheet that has a series of headings and a row that sums
up the totals. I am guessing that there will be further calculations
but for now I'm trying to achieve the first step which is to delete
the existing data and insert the new data.

For example,

If I want to look at a certain branch's data, I insert their account
number and refresh the calculation. This will bring up all the
records associated with that branch and provide subtotals.

I have managed to succesfully create an ADO connection to an MS Access
database with the code below, but unfortunatley for me it's not
clearing the data and I can't seem to find how I go about doing it.

I did think of using .XLDOWN to delete the existing data and .INSERT
to insert the data but I have a feeling that there is something more
practicable to do this.

Any assistance would be appreciated.

<CODE

Sub import_salaries()
Dim conn As New Connection, rec As New Recordset
Dim ws As Worksheet
Dim sql$, i&

Set ws = ThisWorkbook.Worksheets("Sheet1")

conn.Open "Provider=microsoft.jet.oledb.4.0;" + _
"Data Source=" + ThisWorkbook.Path + "\HR_Occupancy_Table.mdb"

' T3Lookup = ws.Range("CCLOOKUP").Value

sql = "SELECT Employee_No, Employee_Name, Cost_Centre_Description,
Salary_Year, T3_CC_Lookup " & _
"FROM HR_OCCUPANCY WHERE T3_CC_Lookup = " &
ws.Range("CCLOOKUP").Value & " ORDER BY Cost_Centre_Description "

rec.Open sql, conn

While Not rec.EOF
i = i + 1
ws.[A5].Cells(i) = rec!Employee_No
ws.[B5].Cells(i) = rec!Employee_Name
ws.[C5].Cells(i) = rec!Cost_Centre_Description
ws.[D5].Cells(i) = rec!Salary_Year
ws.[E5].Cells(i) = rec!T3_CC_Lookup
rec.MoveNext
Wend
rec.Close: conn.Close


End Sub

</CODE



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code - Clear and Insert & If Statement VBA Noob[_27_] Excel Programming 0 July 20th 06 03:18 PM
Insert Decimal Places and Clear Zeros Gladys Excel Discussion (Misc queries) 2 May 5th 06 08:59 PM
Insert Next? Or insert a variable number of records...how? Tom MacKay Excel Discussion (Misc queries) 0 April 20th 06 10:44 PM
Access Recordset Rows to Excel Geoff[_11_] Excel Programming 1 May 25th 05 05:41 PM
Returning the Number of Rows in a Recordset Alex[_14_] Excel Programming 2 November 1st 03 11:14 AM


All times are GMT +1. The time now is 03:34 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"