View Full Version : Insert and update


Picasso_ss
06-27-2007, 10:48 AM
Hello, (the code is at the end of the post)
I'm a total newbie with access and vba. Please help with a solution for my problem. This is the code i use in my application, but everytime I click the buton Save it creates a new line in the test table. Please help with a code that inserts a new record in the test table only when entering for the first time the form, and to perform the update whenever the value on Forms![Client]![Nume] is changed.
Example: When entering the form Client, the default value for Nume is 0. I must then enter a value for Nume. After i entered the value, I must click the buton Save, then the code insert a new line in table TEST, but when I modify the value and press for the second time the buton SAVE I don't want to create a new line in table test, but to update the last value entered. If the form is empty and want to enter a new value that is different from the last entered, the code must permit to create new lines, but if the value enered is already entered it must promt me about this.

Private Sub Save_Click()
DoCmd.SetWarnings (False)
DoCmd.RunSQL "INSERT INTO TEST ( [Nume1] ) SELECT Forms![Client]![Nume];"
DoCmd.SetWarnings (True)
MsgBox "Ai reusit", vbExclamation, "S-a facut salvarea"
End Sub


Thank you very much.

The_Doc_Man
06-27-2007, 11:00 AM
Please do not double-post. You, as author of a post, may delete the post by clicking on the edit button and, within that dialog, delete the extra copy.

Picasso_ss
06-29-2007, 03:03 AM
Nobody knows?

Dennisk
06-29-2007, 04:12 AM
you can test if a record is new

IF me.NewRecord then
Insert SQL here
else
Update SQl here
endif

geekay
06-29-2007, 06:22 AM
You may be using bound forms. If it is a bound form your action will either append as a new record or change the value in the existing record as the case may be. I dont understand what is the relevance of the 'Save' button then. Use unbound form and write code to populate the values from the table

Picasso_ss
06-29-2007, 06:49 AM
Thank you very much Dennisk! You solved my puzzle.
Geekay: my problem is that i want the values to be put in two different tables, so, if i enter one value this value should go in two tables.

Deniseobx
06-29-2007, 12:39 PM
You can probably link these two tables instead of saving the same value in two tables.

Picasso_ss
07-03-2007, 04:53 AM
Yes, I know, but what I want is to put the values in more than 2 tables. For instance I want the values to be writen on table 1 and 2 which are related but in the same time, the calculated values to be inserted in other tables Table 3 and Table 4. This is why i try to use vba.