Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write an error-routine to check the results of an insert

Status
Not open for further replies.

gwward

Programmer
Dec 5, 2003
11
0
0
US
How do I write an error-routine to check the results of an insert to an access database?

If the Insert was good continue on else post a message back
to the user stating there was an error.

The code listed below currently does the insert. Could someone please add the Error-routine to this code based
on the criteria I stated above?

Secondly. I would also like to programatically supply
a password to the code listed below. I already have
placed a password on the gradapp.mdb file. It is 1220bw,
all lowercase.

Thanks for your time and cooperation in this.

Here's the code:

Thanks Bill Ward b.ward@vanderbilt.edu


'build insert statement
Dim sInsertCommand
sInsertCommand = "INSERT INTO GradApp (name, ssn, housing_plans," & _
" custody, custody_ages, custody_cost, summer_2002, fall_2002," & _
" spring_2003, summer_2003, plan_program) VALUES ('" & sName & "', '" & _
sSSN & "', '" & sHousingPlans & "', '" & sCustody & "', '" & sCustodyAges & "', '" & _
sCustodyCost & "', '" & sSummer2002 & "', '" & sFall2002 & "', '" & sSpring2003 & "', '" & _
sSummer2003 & "', '" & sPlanProgram & "')"

'Response.Write sInsertCommand

'create and open connection object
Dim sConnectString
sConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("gradapp.mdb") & ";Persist Security Info=False"

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open sConnectString

'create and execute command
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = cn
cmd.CommandText = sInsertCommand
cmd.Execute

'close connection and destroy objects
cn.Close
Set cmd = Nothing
Set cn = Nothing
 
if cmd.Execute executes without a VB error you can assume the record was inserted.

Use normal VB error handling like
On Error Goto ErrHndlr
and write a error handler into your procedure.

If you are only worried about this one error then you could do

err.clear
On Error Resume Next
cmd.Execute
If err.Number <> 0 Then
'your error handling code goes here
End if
 
For information and examples on error handling see faq222-1694 and faq222-4063.

Take Care,

zemp

&quot;Show me someone with both feet on the ground and I will show you someone who can't put their pants on.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top