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

Does Jet/ACE use indexes with a LIKE BeginsWith query?
Printing parameters in an Access parameter query
How do you set the required property when adding a column in Access using ADOX?
Export From Access to Excel
access 2010 - parameters button is disabled
Several If Statements in an access 2007 vba subroutine. Curious if there is another way?
reliable way of overwriting access database on live website
How to specify page width and length for printed (or html) report?
How to bind the contents of a combo box to the value of another
Access VBA: DLookUp function giving type mismatch error
Microsoft Access - If Null then
How do i reference the value of a control in a rowsource query?
How to query number of attachments from Attachment field in Microsoft Access?
Problem adding a row into a typed dataset
Efficient use of calculations in MS Access
Access VBA: Set record source of form on button click

Categories

HOME
ibm-bluemix
yii2
openlayers
atom-editor
azure-data-factory
push-notification
sd-card
razor
twitter-bootstrap-4
serverspec
mouse
swagger-ui
synchronization
maude-system
windows-10-universal
nano-server
zend-framework3
moonmail
dynamics-crm-online
progressive-web-apps
fatal-error
text-rendering
paging
facebook-instant-articles
sqlcipher
opennlp
semantic-analysis
chromebook
windows-error-reporting
tasklet
unboundid
instant-messaging
galsim
textmate
http-referer
main
restlet
gesture
c11
hue
particles.js
sfdc
botbuilder
estimote
cubic-spline
outlook-2013
paxos
datastax-startup
togetherjs
aurelia-validation
mplayer
jxcore
worker-thread
diagnostics
snmptrapd
mediaelement
gnome-shell-extensions
topbeat
migradoc
yt-project
xcb
passport-google-oauth
bonobo
sts-springsourcetoolsuite
url-masking
jwplayer7
whois
phpcas
computer-algebra-systems
android-listview
proj4js
system.reflection
website-monitoring
asp.net-dynamic-data
android-nested-fragment
id3v2
contenttype
reactfx
braille
srs
xsockets.net
dbconnection
opcache
ftps
rabl
spring-validator
simba
isnullorempty
quickdialog
nsnetservice
hosts-file
removeclass
spyware
deobfuscation
rfc1123
calling-convention
inotifycollectionchanged
icanhaz.js
perfect-hash
database-management
exchange-server-2003
javap
ti-dsp
swing-app-framework
text-coloring
gacutil
defensive-programming
document-conversion

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