RE: SQL insert record return value?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi, Paul.

> I have a loop that includes inserting a record to a datasbase. What I'd
> like to to is be able to tell if the new entry successfully wrote to the
> database.

Instead of using RunSQL for this action query, use Execute and the
dbFailOnError parameter to prevent any messages from being displayed to the
user unless there actually is an error, in which case your error handler can
tell the user exactly which record failed to be appended to the table, since
there's only one record at a time being appended. The error handler can quit
processing -- or return to processing -- the records. For example:

' Within a loop:

CurrentDb().Execute "INSERT INTO tblCourses (CourseName) " & _
"VALUES ('" & sValue & "');", dbFailOnError
nCount = nCount + 1

' More code in the loop.

CleanUp:

MsgBox "Successfully saved " & nCount & " courses."

Exit Sub

ErrHandler:

MsgBox "Error in SaveSelectionBtn_Click( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp
End Sub

.. . . where tblCourses is the name of the table, CourseName is the name of
the field, and sValue is the text string value being entered into the field,
and nCount is the counter for successful appends. If there's no error (and
no subsequent error message), then the record was successfully appended and
the counter will increment. When the processing stops (whether because there
are no more records to append or there is an error), the user will see how
many records were saved.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.


"pvdalen" wrote:

> Howdy,
>
> I have a loop that includes inserting a record to a datasbase. What I'd
> like to to is be able to tell if the new entry successfully wrote to the
> database. I have SetWarnings set to false because I don't want the user to
> have to deal with those mesages every time, but I'd like to report how many
> records were inserted. Is there a value that is set or returned from the
> DoCmd RunSQL statement that I can capture to conditionally increment a
> variable?
>
> Thanks,
> Paul
.



Relevant Pages

  • Re: Frustrated with ADO and reading Delimited files
    ... Inserting a 'wScript.echo ... objRecordSet.Fields.Count' statement just before the loop returns a ... Maybe some database or scripting engines needs to be updated? ...
    (microsoft.public.scripting.vbscript)
  • Re: RunCommand acCmdRecordsGoToNext
    ... Another thing that may come in handy if that doesn't is to loop through the ... The database I am utilizing this ... increasing intRecordCount on each loop. ... Dim strPassword As String ...
    (microsoft.public.access.forms)
  • Re: [PATCH 1/1] (v3) SYSVIPC - Fix the ipc structures initialization
    ... a simple testcase is concurrently running an infinite loop on ... So idr_get_newis inserting a pointer into the ... This patch moves the spin_lock_initbefore the call to ipc_addid. ... return err; ...
    (Linux-Kernel)
  • Re: [PATCH 1/1] (v3) SYSVIPC - Fix the ipc structures initialization
    ... a simple testcase is concurrently running an infinite loop on ... So idr_get_newis inserting a pointer into the ... This patch moves the spin_lock_initbefore the call to ipc_addid. ... return err; ...
    (Linux-Kernel)
  • Re: Sql Connection
    ... ..i dont like the code either....again a example of a loop ... making calls to the database .... ... what i did yesterday is just get the parsed data to a temp table on the ... the question is the open connection where the Datareader is used insid ...
    (microsoft.public.dotnet.languages.csharp)