Access Form VBA: double record when saving - ms-access

In my Access Form, I have a Save button whose aim is to add the new record to the table called Data Processing List.
The problem is that pushing the button, the record entered is saved into the table twice. I mean, two identical records.
I can't understand why, because the code is really simple:
Private Sub Save_Click()
On Error GoTo Save_Click_Err
Me.Today.SetFocus
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
With CurrentDb.OpenRecordset("Data Processing List")
.AddNew
!Department = Me.Department.Value
.Update
End With
Save_Click_Exit:
Exit Sub
Save_Click_Err:
MsgBox Error$
Resume Save_Click_Exit
End Sub
This is an example when I try to save the record with the word "prova".

You are saving twice:
DoCmd.RunCommand **acCmdSaveRecord**
...
With CurrentDb.OpenRecordset("Data Processing List")
.AddNew
!Department = Me.Department.Value
.**Update**
End With

Related

Open form and set unbound ComboBox to specific value

ISSUE
I have two forms:
frmForms
frmDockRental
I have two controls associated with this issue:
lstOwners on frmForms (unbound)
cboOwner on frmDockRental (unbound)
The second form (frmDockRental) is opened using different listboxes located on frmForms (See Image). I have one such listbox that's giving me grief. It is a filtered list of contacts that when double-clicked it should be opening frmDockRental to a NEW record and set cboOwner, which is unbound, to a specific item in the list. The same item listed in lstOwners from frmForms.
CODE
After a lot of fiddling, I came up with this - except nothing happens at all.
Private Sub lstOwners_DblClick(Cancel As Integer)
On Error GoTo lstOwners_DblClick_Err
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
Exit Sub
DoCmd.OpenForm "frmDockRental", acNormal, "", "", acFormAdd, acDialog, Me.lstOwners
DoCmd.Close acForm, Me.Name
lstOwners_DblClick_Exit:
Exit Sub
lstOwners_DblClick_Err:
MsgBox Error$
Resume lstOwners_DblClick_Exit
End Sub
And on frmDockRental, this:
Private Sub Form_Load()
On Error GoTo Form_Load_Err
DoCmd.MoveSize , , 5.3 * 1440, 5.2 * 1440
' If (IsNull(OpenArgs)) Then
' Exit Sub
' End If
If Me.OpenArgs <> vbNullString Then
Me.cboOwner = Me.OpenArgs
End If
If (Not IsNull(OpenArgs)) Then
DoCmd.GoToRecord , "", acNewRec
End If
Form_Load_Exit:
Exit Sub
Form_Load_Err:
MsgBox Error$
Resume Form_Load_Exit
End Sub
I figured OpenArgs would be the best method to accomplish this, but it just doesn't work. Nothing at all happens. No errors, just nothing.
EDIT:
Here's an image of the step debugging.

Recordsetclone loop

The code below works BUT only to the first record in the continuous form its not looping threw the records. So if the first record is checked then I get the message "You need to select a RELEASE check box before proceeding." and if its not I get the other message.
What I need it to do is run down all the check boxes which is "ReleaseProduct" and exit sub at the first one it reaches if there is one and give a message and and if there isn't any check boxes checked run the Cancel = fncRequiredReleaseSelectedEmail(Me) and exit sub. Im messing up the loop somewhere...
Here is the code I have in my forms button....
Dim Cancel As Integer
Dim rs As Recordset
Set rs = Me.frmsub_ProductHoldData.Form.RecordsetClone
With rs
.MoveFirst
Do While Not .EOF
If rs.Fields("ReleaseProduct") = False Then
MsgBox "You need to select a RELEASE check box before proceeding.", vbInformation, "Selection Error"
Exit Sub
Else
Cancel = fncRequiredReleaseSelectedEmail(Me)
Exit Sub
End If
.MoveNext
Loop
End With
I'm having a hard time telling if this is exactly what you are looking for or not but this will look through all those records and only take action if one is checked.
The warning about selecting a Release check box only shows up if no boxes are checked.
Dim Cancel As Integer
Dim rs As Recordset
Set rs = Me.frmsub_ProductHoldData.Form.RecordsetClone
With rs
.MoveFirst
Do While Not .EOF
If rs.Fields("ReleaseProduct") = True Then
Cancel = fncRequiredReleaseSelectedEmail(Me)
Exit Sub
End If
.MoveNext
Loop
MsgBox "You need to select a RELEASE check box before proceeding.", vbInformation, "Selection Error"
End With

How can I remove the pesky "The runCommand action was canceled" dialog box after a "cancel = true" statement

On the Form_beforeupdate() event of my form "Alias" I have this...
If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If
Anytime this cancels a runCommand code such as...
DoCmd.RunCommand acCmdSaveRecord
a dialog appears telling me that it was canceled. Is there a way to suppress those dialogs?
You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord
The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.
Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().
Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!
Private Sub cmdSave_Click()
'DoCmd.RunCommand acCmdSaveRecord
SaveRecord
End Sub
Private Sub SaveRecord()
Dim strMsg As String
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 ' The RunCommand action was canceled.
' do nothing --> just ignore the error
Case Else
' notify user about any other error
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure SaveRecord"
MsgBox strMsg
End Select
Resume ExitHere
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtBlock_start.Value) Then
MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
Cancel = True
End If
End Sub
I ended up doing this on a 'Save and Open the Receipts form' button.
Private Sub btnSaveOpenReceipts_Click()
'''''SaveRecord'''''
If (Me.Dirty) Then
SaveRecord
End If
'''''OpenReciepts'''''
If (Me.Dirty) Then 'unsure it saved
Exit Sub
Else
'blah blah blah
End If
End Sub

ms access form closing ask save yesnocancel

I have put the VBA in Unload Form Event in ms access 2010
Private Sub Form_Unload(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
' Check the user's response.
If iResponse = vbYes Then
' Undo the change.
DoCmd.RunCommand acCmdSave
End If
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo
End If
If iResponse = vbCancel Then
' Undo the change
Cancel = True
End If
End Sub
If data is changed then the above code is working fine, then yes to save & close, No to undo & close and cancel to cancel event and remain on the form
but when the data is unchanged then Yes button is working fine, but NO button do not close the form
Where I m mistaking ?
Your question is too vague because you simply state "NO button do not close the form."
You can do:
DoCmd.Close
See here:
Private Sub cmdCloseForm_Click()
On Error GoTo Err_cmdCloseForm_Click
DoCmd.RunCommand acCmdUndo 'OR Me.Undo - test which works best for your situation
DoCmd.Close
Exit_cmdCloseForm_Click:
Exit Sub
Err_cmdCloseForm_Click:
MsgBox Err.Description
Resume Exit_cmdCloseForm_Click
End Sub
On Me.Undo from Allen Browne:
Me.Undo cancels the edits in a specific form, so that it is no longer dirty.
Once the form is no longer dirty, no further undo is possible, because it
has reached the desired state. However, Me. represents the form that is in focus, so your form must have focus to perform the Me.Undo command.

Access: Move to next record until EOF

I need to loop through a form by moving to the next record in the recordset.
I am using the Form_Current event to loop thru.
I have used a couple of statements and have different outcomes.
This one sometimes crashes and gives the error message: "You can't go to the specified record."
DoCmd.GoToRecord , , acNext
This one only goes upto 72 records and stops.
DoCmd.RunCommand acCmdRecordsGoToNext
This one only goes upto 129 records and stops.
Me.Recordset.MoveNext
Trying to find an instruction that will go to the next record untill it reaches the End of File.
I am using Access 2010 (Access 2002 -2003 file format mdb) as the front end. The recordsource is a SQL Server 2008 linked View.
To loop from current record to the end:
While Me.CurrentRecord < Me.Recordset.RecordCount
' ... do something to current record
' ...
DoCmd.GoToRecord Record:=acNext
Wend
To check if it is possible to go to next record:
If Me.CurrentRecord < Me.Recordset.RecordCount Then
' ...
End If
If (Not IsNull(Me.id.Value)) Then
DoCmd.GoToRecord , , acNext
End If
Hi,
you need to put this in form activate, and have an id field named id...
this way it passes until it reaches the one without id (AKA new one)...
I have done this in the past, and have always used this:
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If Me.Dirty Then
Me.Dirty = False
End If
.MoveNext
Me.Bookmark = .Bookmark
Loop
End With
Some people would use the form's Recordset, which doesn't require setting the bookmark (i.e., navigating the form's Recordset navigates the form's edit buffer automatically, so the user sees the move immediately), but I prefer the indirection of the RecordsetClone.
Set rs = me.RecordsetClone
rs.Bookmark = me.Bookmark
Do
rs.movenext
Loop until rs.eof
If you want cmd buttons that loop through the form's records, try adding this code to your cmdNext_Click and cmdPrevious_Click VBA.
I have found it works well and copes with BOF / EOF issues:
On Error Resume Next
DoCmd.GoToRecord , , acNext
On Error Goto 0
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
On Error Goto 0
Good luck! PT
Keeping the code simple is always my advice:
If IsNull(Me.Id) = True Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acLast
End If
Add This Code on Form Close Event whether you add new record or delete, it will recreate the Primary Keys from 1 to Last record.This code will not disturb other columns of table.
Sub updatePrimaryKeysOnFormClose()
Dim i, rcount As Integer
'Declare some object variables
Dim dbLib As Database
Dim rsTable1 As Recordset
'Set dbLib to the current database (i.e. LIBRARY)
Set dbLib = CurrentDb
'Open a recordset object for the Table1 table
Set rsTable1 = dbLib.OpenRecordset("Table1")
rcount = rsTable1.RecordCount
'== Add New Record ============================
For i = 1 To rcount
With rsTable1
rsTable1.Edit
rsTable1.Fields(0) = i
rsTable1.Update
'-- Go to Next Record ---
rsTable1.MoveNext
End With
Next
Set rsTable1 = rsTable1
End Sub