Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2008

Tip: Looking for answers? Try searching our database.

INSERT INTO table from recordSet??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
juvi - 29 Mar 2008 17:28 GMT
Hello,

Is it possible to run an INSERT INTO table command and pass over a recordset?

Now I must write all field-names:
INSERT INTO tbl_test([Field1], [Field2]......) VALUES ( .. rs ! Field1 .... )

this works fine but it is hard work if columns are deleted or added.
Something like the following would be nice if possible:
INSERT INTO tbl_test FROM rs??????

thank you in advance
juvi
Ken Snell (MVP) - 29 Mar 2008 23:50 GMT
You cannot do it directly with rs in the SQL statement, but something like
this might work (this assumes that you have already created the rs object
and that its fields are in the same order as in the tbl_test table) -- NOTE
that this code assumes that the fields in rs recordset all are numeric -- if
some are text or date, then the code would need to be modified to test the
datatype of each rs field and then use the appropriate delimiters around the
value based on the datatype:

Dim strSQL As String
Dim lngFields As Long
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tbl_test")
strSQL = "INSERT INTO tbl_test ("
For lngField = 0 to tdf.Fields.Count - 1
   strSQL = strSQL & tdf.Fields(lngField).Name & ", "
Next lngField
strSQL = Left(strSQL, Len(strSQL) - 2) & ") VALUES ("
For lngField = 0 to rs.Fields.Count - 1
   strSQL = strSQL & rs.Fields(lngField).Value & ", "
Next lngField
strSQL = Left(strSQL, Len(strSQL) - 2) & ")
dbs.Execute strSQL, dbFailOnError
Set tdf = Nothing
dbs.Close
Set dbs = Nothing

Signature

       Ken Snell
<MS ACCESS MVP>

> Hello,
>
[quoted text clipped - 11 lines]
> thank you in advance
> juvi
Ken Snell (MVP) - 30 Mar 2008 02:03 GMT
Forgot a trailling " character:

Dim strSQL As String
Dim lngFields As Long
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tbl_test")
strSQL = "INSERT INTO tbl_test ("
For lngField = 0 to tdf.Fields.Count - 1
   strSQL = strSQL & tdf.Fields(lngField).Name & ", "
Next lngField
strSQL = Left(strSQL, Len(strSQL) - 2) & ") VALUES ("
For lngField = 0 to rs.Fields.Count - 1
   strSQL = strSQL & rs.Fields(lngField).Value & ", "
Next lngField
strSQL = Left(strSQL, Len(strSQL) - 2) & ")"
dbs.Execute strSQL, dbFailOnError
Set tdf = Nothing
dbs.Close
Set dbs = Nothing

Signature

       Ken Snell
<MS ACCESS MVP>

> You cannot do it directly with rs in the SQL statement, but something like
> this might work (this assumes that you have already created the rs object
[quoted text clipped - 39 lines]
>> thank you in advance
>> juvi
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.