INSERT INTO ---- force Yes

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
 
D

Dirk Goldgar

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.
 
F

fredg

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.
 
M

Marshall Barton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top