Update if exist else insert

steve87bg

Registered User.
Local time
Tomorrow, 00:42
Joined
Jan 14, 2013
Messages
19
I have a form with some ID. I have made update query and add query. Now if ID from my form exist in table1 for example, i want to run update query, and if ID from my form doesn't exist in my table1 then run add query.
:banghead: Any help guys :confused:
 
Typically, you would use a form bound to the table and the user would scroll through the records to find the one they need to update. They can update the data in any of the controls and it will be saved to the underlying table. You can help the user out by adding a search function to the form which typically is done with a combo box (of the IDs) in the form header. When the user finds the ID they want and select it, the main form moves to that record. You can also add functionality to move to a new record if the ID is not there.


Of course, you could use an unbound form (i.e. not tied to a table), do the checking of whether the ID exists. You would have to provide controls on that form for the user to enter the new information (for a new ID) & then run the append query as you mentioned. For an existing ID, the user would not be able to see the existing information, so how would they know what information needs to be updated? Are you going to force them to retype all the information? Once you know which information needs to be updated, then you would run the update query. You would have to use code to determine whether the ID exists. That is fairly easy & would look like this

Dim mySQL as string

IF DCount("*", "tablename","ID=" & me.NameOfControlOnFormThatHoldsID)>0 THEN
'record exists, construct the update query

mySQL= "UPDATE tablename SET ..."

ELSE

'new record needed, construct the append query

mySQL= "INSERT INTO tablename..."

END IF

'execute the constructed query
currentDB.execute mySQL, dbfailonerror
 
THANKS JZWP22. Thats exactly what i need :)
 

Users who are viewing this thread

Back
Top Bottom