My Receipts table has these fields: receiptID, userID, cost, stateID, workerID
receiptID - Primary key
userID - Foreign key that points to the user this receipt is linked with
cost - shows the total cost of all bookings connected to this receipt
stateID - shows the current state of this receipt(1-in creation,2-created, 3-paid, 4-cancelled)
workerID - points to the accountant that signs the receipt
What i want to do is to fill the receiptID field on the form this way that if the selected user has any receipts in "in creation" state then the receiptID of that receipt is put there, but if there is no such receipt then new receipt is created and its receiptID is put there. I think I have to use SQL query in VBA to find the receiptID that satisfies the given conditions and if there aren't any results then new record will be created into Receipts table. I managed to create new record using VBA but at the moment every time I make a booking new record is created into Receipts table and that's not what I intended to do.
This is what I have right now for creating new record in Receipts table.
Expand|Select|Wrap|Line Numbers
- Set dbTeater = CurrentDb
- Set rsReceipts = dbTeater.OpenRecordset("Receipts")
- rsReceipts.AddNew
- rsReceipts("userID").Value = Me!Client
- rsReceipts("stateID").Value = 1
- rsReceipts.Update
Expand|Select|Wrap|Line Numbers
- SELECT Receipts.receiptID
- FROM Receipts
- WHERE Receipts.userID=Forms!BookingsF!ClientBox AND Receipts.stateID=1