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 / Forms Programming / April 2008

Tip: Looking for answers? Try searching our database.

How to write an append query on a recordset

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
magicdds- - 30 Apr 2008 23:59 GMT
I have some code running on an event procedure. At one point in the code I
need to run an append query. If the data to be appended to the table
LETTERHISTORY was in another table, I would just have an append query and
DoCmd.OpenQuery "QueryName".

However, my data is in a recordset that was created in the code of the event
procedure.

The name of the recordset is "rstDayFile"

The Query, if made by the QUERY BUILDER would look like this:

INSERT INTO LetterHistory ( PatientID, LetterName, LetterDate )
SELECT PatientID, LetterName, Date() AS LetterDate
FROM rstDayFile;

I don't know how to type this query into the VBA code of the event procedure
or how to get it to run to append the single record to the LETTERHISTORY
table.
There are 3 fields in the LETTERHISTORY table: PatientID, LetterName,
LetterDate.

Thanks for your help.
Mark
Graham Mandeno - 01 May 2008 00:40 GMT
Hi Mark

You declare a string variable in your VBA code and then construct the SQL
string in your variable.  Then assign CurrentDb to a database object
variable and use the Execute method to run the SQL command.  For example:

Dim sSQL as String, db as DAO.Database
sSQL = "INSERT INTO LetterHistory ( PatientID, LetterName, LetterDate ) " _
   & "SELECT PatientID, LetterName, Date() AS LetterDate " _
   & "FROM rstDayFile;"
Set db = CurrentDb
db.Execute sSQL, dbFailOnError
MsgBox db.RecordsAffected & " letters have been recorded."
Signature

Good Luck  :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

>I have some code running on an event procedure. At one point in the code I
> need to run an append query. If the data to be appended to the table
[quoted text clipped - 22 lines]
> Thanks for your help.
> Mark
Mr B - 01 May 2008 00:41 GMT
In your event proceedure, define a string type variable:

Dim strSql as String

Then assign the sql statement (your query) to the string type variable:

strsql = "INSERT INTO LetterHistory ( PatientID, LetterName, LetterDate ) " _
        & "SELECT PatientID, LetterName, Date() AS LetterDate " _
        & "FROM rstDayFile;"

(please note that I have used string contatinating characters in the sql
statement so that hopefully it would not word warp.)

Then you cand use the following statement to run the query:

CurrentDb.Execute strSql

That should do it.

Signature

HTH

Mr B
askdoctoraccess dot com

> I have some code running on an event procedure. At one point in the code I
> need to run an append query. If the data to be appended to the table
[quoted text clipped - 20 lines]
> Thanks for your help.
> Mark
 
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.