Question Performing multiple database action on button

RetPrgmr

New member
Local time
Today, 11:00
Joined
Jul 9, 2012
Messages
3
I am writing an application for my personal use. I have 2 Access databases, one contains the data tables and the other has the forms, queries, reports, etc.

So far, I have been able to use the queries in the Row Source property of my form objects to accomplish what I want to do. Now I want to have a button that appends records to a table, the data in the table comes from several different controls on my form. After doing this the button should requery another control to display the new records added.

The OnClick event for the button allows me to use a Macro builder, Expression builder or Code builder. If I choose to use DAO in VBA code to append the records, do I have to define the database to create a database object to use even though the database is already open? Can I use a Macro to do this? Can a macro reference the data that is in the controls on my form?

I would appreciate it if someone would point me in the right direction.

Thanks,

Mary Jane
 
I think using SQL would be more efficient and easier to control.
You use the DoCmd.RunSQL method for that, so you can design your query in SQL and just pass it as a string to it.
Code:
Dim mySQL As String

mySQL = "UPDATE myTable etc." 'your update query

'DoCmd.SetWarnings False <- uncomment this after testing
DoCmd.RunSQL mySQL
'DoCmd.SetWarnings True
For referencing controls on a form in VBA use the Me. keyword followed by the name of the control. You also have the Controls collection which is useful when you need to repeat the same code on many contorls.
 
To avoid having to deal with turning on and off the Warnings, you can use this method:
Code:
CurrentDb.Execute "UPDATE myTable etc.", dbFailOnError
 
I don't mind having the popups with the number of records that will be affected at an initial stage; also, I remember reading somewhere there was a difference between the two on lower-level compilation and execution - although I wouldn't remember what it is (some research will surely bring it up).

But yeah, the final result should be the same :)
 
With the dbFailOnError parameter all changes will be rolled back if an error occurs.

Yep, they do the same thing.
 

Users who are viewing this thread

Back
Top Bottom