INSERT INTO ---- force Yes

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

I have a command button that inserts data into a table and it works great.
I want to have that command button also have it automatically click Yes when
it prompts the user "You are about to append 1 row...". How can I do that?
What's the code?

Ben
 
Ben said:
I have a command button that inserts data into a table and it works great.
I want to have that command button also have it automatically click Yes
when
it prompts the user "You are about to append 1 row...". How can I do
that?
What's the code?


You could have code like this:

'------ start of example code #1 -----
Private Sub cmdInsert_Click()

CurrentDb.Execute "INSERT INTO ...", dbFailOnError

End Sub
'------ end of example code #1 -----

or else you could use code like this:

'------ start of example code #2 -----
Private Sub cmdInsert_Click()

On Error GoTo Err_Handler

DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO ..."

Exit_Point:
DoCmd.SetWarnings True
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point

End Sub
'------ end of example code #2 -----

The second example is more involved than the first, because it's very
important that you not let the procedure complete without turning the
warnings back on.
 
I have a command button that inserts data into a table and it works great.
I want to have that command button also have it automatically click Yes when
it prompts the user "You are about to append 1 row...". How can I do that?
What's the code?

Ben

Just turn the warnings off (then on again).
Look up the SetWarnings method in VBA elp.
Also look up the Execute method in VBA help.

Code the command button Click event:

DoCmd.SetWarnings False
DocCmd.OpenQuery "Your Query Name"
or..
DoCmd.RunSQL "Insert .... etc..."
DoCmd.SetWarnings True

Or... instead of DoCmd.OpenQuery (or RunSQL) use

CurrentDb.Execute "Your Query Name", dbFailOnError
or..
CurrentDb.Execute "Insert ... etc...", dbFailOnError

You need to set a reference to the Microsoft DAO 3.6 Object Library.
You will not receive any warning prompts at all.
 
Ben said:
I have a command button that inserts data into a table and it works great.
I want to have that command button also have it automatically click Yes when
it prompts the user "You are about to append 1 row...". How can I do that?
What's the code?


Use the Execute method to insert a new record:

CurrentDb.Execute "INSERT INTO table . . .

Or use a recordset:

With CurrentDb.OpenRecordset("table")
.AddNew
!fielda = ...
!fieldb = ...
. . .
.Update
.Close
End With
 
Back
Top