If Prim. Key Exist - Update

deadb0lt

New member
Local time
Today, 14:37
Joined
Jun 12, 2006
Messages
3
[RESOLVED] If Prim. Key Exist - How to Update??

Hello all. I'm trying to finish up a database for our farm and am running into issues as I'm pretty new to Access. I know VB6 but not a lot of VBA. We spray chemicals, and need to keep track of the acerage we spray for the date.

So here's my problem:
When I go to write my records, I need to search a table for a date. If the date exists already, then I need to update the total acreage related to the date. So first, how do I search the table and keep track of if the date was or wasnt found, and if found, calculate total acreage from value pulled from table, and the new acreage value, then write that back to the table.

Would really appreciate the help. Thanks.
 
Last edited:
You can use an if statement and the dcount function to check if the date already exists.
 
More...

I could probably use DCount and DLookup to find the date, but how would I then grab the value TOTAL ACRES value associated with the DATE, and put it into a variable so I could then calculate the new ACREAGE value with the then TOTAL ACRES value?

Lets try pseudo code if I'm still not clear:
DateExist as Boolean
tempAcres as Integer

DateExist = Search RECSbyDATE table for DATE

If DateExist = TRUE then THEN
tempAcres = Get TOTAL ACRES from RECSbyDATE table WHERE the date is DATE
TotalAcres = TotalAcres + tempAcres
Update TOTAL ACRES field in RECSbyDATE table with TotalAcres Value
Else
'THIS PART ALREADY WRITTEN
End If

That's about all I need, but I've got no book or MSDN resources...just you guys. I wrote the Pseudo Code (simple enough) now someone write the real code. haha

Here's an Image for Reference:
access.JPG
 
Last edited:
some thing like below should give you a good idea of what to do

PHP:
 If IsNull(DLookup("[Date]", "table5", "[Date]=#" & Dte & "#")) = False Then
        Acres = DLookup("[totalAcres]", "table5", "[Date]=#" & Dte & "#")
   End if
 
Works

Excellent! I appreciate the help. After a little more searching, I figured out how to use the rst.Edit command to *ahem* EDIT the existing data. :D
Wheee! All done. Thanks!
 

Users who are viewing this thread

Back
Top Bottom