ms-access


How to insert “Entire” DAO recordset into a table with VBA


I have a DAO recordset that gets created fine and I can transfer the records from the set to a table, this is done row by row and works well but I am transfering a large amount of data at once so this can take a very long time row by row.
Is there a way to transfer the ENTIRE recordset in one go, rather than row by row
See below for current code in use -
Dim SendE1 As DAO.Recordset
Set SendE1 = CurrentDb.OpenRecordset("SELECT TBL_ImportTable.* FROM TBL_ImportTable", dbOpenDynaset)
SendE1.MoveLast
Do Until SendE1.EOF
sqlinsert = "INSERT INTO TBL_E1Jobs (StartDate, StartTime, EndDate, EndTime, Location, UserID, WorkStationID, DocumentNumber, E1Shift, OperSeq, Facility, AdjustedforShifts, WeekNum)" & _
" VALUES ('" & SendE1("StartDate") & "', '" & SendE1("StartTime") & "', '" & SendE1("EndDate") & "', '" & SendE1("EndTime") & "', '" & SendE1("Location") & "', '" & SendE1("UserID") & "', '" & SendE1("WorkstationID") & "', '" & SendE1("DocumentNumber") & "', '" & SendE1("E1Shift") & "', '" & SendE1("OperSeq") & "', '" & SendE1("Facility") & "', '" & SendE1("AdjustedforShifts") & "', '" & SendE1("WeekNum") & "') "
DoCmd.RunSQL (sqlinsert)
SendE1.MoveNext
Loop
SendE1.Close
Set SendE1 = Nothing
#cularis is correct. The right way to do this is in a SQL query. Having read your comments to his answer, there are a few steps you can take to avoid wiping out data that has not been copied:
Dim db As DAO.Database, RecCount As Long
'Get the total number of records in your import table to compare later
RecCount = DCount("*", "TBL_ImportTable")
'This line is IMPORTANT! each time you call CurrentDb a new db object is returned
' that would cause problems for us later
Set db = CurrentDb
'Add the records, being sure to use our db object, not CurrentDb
db.Execute "INSERT INTO TBL_E1Jobs (StartDate, StartTime, ..., WeekNum) " & _
"SELECT StartDate, StartTime, ..., WeekNum " & _
"FROM TBL_ImportTable", dbFailOnError
'db.RecordsAffected now contains the number of records that were inserted above
' since CurrentDb returns a new db object, CurrentDb.RecordsAffected always = 0
If RecCount = db.RecordsAffected Then
db.Execute "DELETE * FROM TBL_ImportTable", dbFailOnError
End If
Please note that if you run those queries on linked ODBC tables, you will need to include the dbSeeChanges option (ie, dbFailOnError + dbSeeChanges).
Not a DAO, but a SQL solution, that does what you need:
INSERT INTO TBL_E1Jobs (StartDate, StartTime, EndDate ...)
SELECT StartDate, StartTime, EndDate ... FROM TBL_ImportTable
INSERT INTO ... SELECT MSDN

Related Links

ms-access remove fails on the same date from the same person when there is a pass
Access 2016 vba to send email based on query results and record date email sent back to same table
Count unique values in Access
If ComboBox on Form is blank, Then
Trying to do a calculation with data from a date and the data from the day before
MS Access Front-end: Does each user need their own copy?
trying to filter subform based on contents of field on main form
How to use MS access on a server where MS Office is not installed
How do i import and eliminate first two row of website in msaccess while importing html pages
Multi criteria dlookup issue
Count all checkboxes in ms-access form…what am I doing wrong?
How do I set a default selection to a Listbox in MS Access
Microsoft Access not overwriting when exporting a query to excel
Using ms-access command-line tag /x but database opens read-only
Criteria with TempVars - but prompt user if tempvars is null
Crazy Cursor in TextBox on Table from ODBC database

Categories

HOME
vim
date
amazon-ec2
gremlin
objectgears
datastax-java-driver
constraint-programming
floating-action-button
static-libraries
quickfix
try-catch
after-effects
invantive-sql
introduction
emulator
swingx
restful-authentication
icloud-api
maxmind
elasticsearch-net
atlassian-crucible
yadcf
catch-all
openoffice.org
y86
google-api-nodejs-client
picasso
overriding
vxworks
web-mining
hockeyapp
az-application-insights
android-fingerprint-api
ansible-playbook
elmah
reactive-cocoa-5
node-sass
qwt
angular-resource
outlook-2013
rails-routing
hls.js
return-value
mplayer
nodebb
media-player
knpmenubundle
jspdf-autotable
merge-conflict-resolution
bstr
pagedlist
oauth2client
goose
jfugue
wireshark-dissector
quicklisp
rgeo
responsive-images
p2
umbraco6
nsmutabledictionary
typekit
camanjs
tt-news
codeigniter-routing
has-many-through
tld
p4java
marmalade
quartz-graphics
ora-00911
ember-app-kit
venn-diagram
automount
appconkit
wse3.0
windows-phone-7.1.1
ohm
cufon
transactionscope
nsobject
subgurim-maps
xmlspy
hamachi
xmemcached
file-comparison
google-friend-connect
wise
revisions
filtered-index
preference
rescale
multi-tier
phonon
pascal-fc
text-coloring
geneva-server
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App