Save button

  • Thread starter Thread starter EMILYTAN via AccessMonster.com
  • Start date Start date
E

EMILYTAN via AccessMonster.com

Here are the following code:

When I click the save button, it will saved for once. After saving, when I
want to click next record, I will show message like creating duplicate record.
..
Besides, I can't even edit my record. The same message appear...
Private Sub cmdSave_Click()

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("select * from Quantity")

rst.Edit
rst!Bil = Bil
rst!PartNumber = PartNumber
rst!InRemarks = InRemarks
rst!InDate = InDate
rst!OutRemarks = OutRemarks
rst!OutDate = OutDate
rst!Comment = Comment
rst!InQty = InQty
rst!OutQty = OutQty
rst.Update
rst.Close


Set rst = Nothing



End Sub
 
Here are the following code:

When I click the save button, it will saved for once. After saving, when I
want to click next record, I will show message like creating duplicate record.

First: Your code would only update one random record in your Quantity table ... normally you'd include a Where clause to
insure that you're working with the correct record:

Set rst = CurrentDb.OpenRecordset("select * from Quantity WHERE YourIDField=" & SomeIDValue)

Next: How do you determine a duplicate record? Is it a combination of ALL fields, or just one or two fields? I would
assume a person could add a record with identical PartNumber, InRemarks, etc but with a different InDate (or something
of that nature).

If this code is in the form (and I'm assuming it is), you could add a module-level flag to indicate when the save
occurs:

[General Declarations]

Private mflgSaved As Boolean
Private mflgDirty As Boolean

Then add this line AFTER your cmdSave command is successfully run:

mflgSaved = True
mflgDirty = False

Now check for that BEFORE running the code in cmdSave_Click"

Private Sub cmdSave_Click()

If mflgSaved And Not mflgDirty Then
Msgbox "The data has already been saved"
Exit Sub
End If

<your other save code here>

mflgSaved=True
mflgDirty=FAlse
End Sub

You'll have to reset the variables in the form's Current event:

Sub Form_Current()
mflgDirty = False
mflgSaved = False
End sub

Finally, you'd need to set the mflgDirty variable to True when ANY piece of data is changed ... so in your various form
Textboxes, combos, etc:

Sub YourTextbox_AfterUpdate()
mflgDirty = True
End Sub

Besides, I can't even edit my record. The same message appear...

What message appears? Are you getting an error? If so, please post the exact error message you're receiving, and also
let us know where this error occurs.

Are you running this code on a bound form? If so, attempting to update the record via VBA code on a bound form (if the
form's data has also been modified) can result in various error messages. Generally, if you're going to use a bound form
then use the form's methods for saving.


Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
Here are the following code:

When I click the save button, it will saved for once. After saving, when I
want to click next record, I will show message like creating duplicate record.
.

Why are you using code to save the record AT ALL? With a bound form, Access
saves the record for you automatically, with no code at all.

What's the purpose of doing it the hard way?

John W. Vinson [MVP]
 
Back
Top