PDA

View Full Version : Solved: skip header when dumping data into access via excel?



debauch
10-03-2008, 12:45 PM
Hi, Im using the following code :


Sub DAO_Skill_Upload()
Sheet1.Activate
' exports data from the active worksheet to a table in an Access database
Dim db As Database, rs As Recordset, r As Long
Set db = OpenDatabase("C:\Documents and Settings\xxxx\My Documents\Development\xxx_DB.mdb") '2003 DB
' open the database
Set rs = db.OpenRecordset("LOG", dbOpenTable)
' get all records in a table
r = 1 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
.Fields("STAR") = Range("A" & r).Value
.Fields("END") = Range("B" & r).Value
.Fields("ORIG") = Range("C" & r).Value
.Fields("NEW= Range("D" & r).Value
.Fields("DUR") = Range("E" & r).Value
.Fields("USER") = Range("F" & r).Value
.Fields("REVERT") = Range("G" & r).Value
.Fields("REASON") = Range("H" & r).Value
' add more fields if necessary...

.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

'Call CountData 'count records loaded
ScreenUpdating = True
End Sub


how can I skip what's in Row 1?

Carl A
10-03-2008, 01:27 PM
r = 2 ?

debauch
10-03-2008, 04:39 PM
no, R = R +1 because that is how it loops through each row ...

.Fields("STAR") = Range("A" & r).Value

A1, A2, A3 etc

If R was 2, then it would skip every second row?

Carl A
10-04-2008, 10:18 AM
no, R = R +1 because that is how it loops through each row ...

.Fields("STAR") = Range("A" & r).Value

A1, A2, A3 etc

If R was 2, then it would skip every second row?

No! Setting r=2 sets your start row at A2 then your loop increments by 1 so you get the desired results.

debauch
10-06-2008, 11:58 AM
No! Setting r=2 sets your start row at A2 then your loop increments by 1 so you get the desired results.

Hmm...I had tried different variations, are you able to post a snippet or sample demonstrating this? If R = 2, then wouldn't it skip every second row?

You can't do R = R = 2 + 1???

Update, I tried R = 2 + 1 and it added 65K records (when I was only testing w. 26 rows).

WOw, what a blonde moment ... you meant the first " r = " I even have it commented as such!