Hide button for new row in Access 2013 continuous form - ms-access

I have a continuous form in Access with Allow Additions set to Yes. I'm trying to enable or disable a deletion button depending on if the user has filled in data in that record. In other words, I want to hide the button for only the blank record at the bottom of the form.
I tried the following in the Form_Current event but it enables or disables all buttons at the same time, and it only runs when I click on a record. I need it to run immediately and update when I add a new row.
If Me.NewRecord Then
btnDelete.Visible = False
Else
btnDelete.Visible = True
End If
EDIT: with working code.
If Me.NewRecord Then
'show error message
MsgBox ("Unable to delete empty row.")
Else
'deletion code
End If

Continuous forms won't allow that - everything looks the same.
You might be able to add code to the btnDelete_Click event that checks for Me.NewRecord and just exits the sub

Here is the above code with handling when the user starts entering data in the "add" row and then clicks delete on that row.
If Me.NewRecord Then
'code to undo/clear partially entered new record
If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
End If
Else
If MsgBox("Are you sure you want to delete this xxxx record?", vbYesNo) = vbYes Then
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
End If
End If

Related

When click Cancel button on form, do not run On Exit code on a field

On my form, the first field has to be entered, so I have an On Exit command. Since it also checks to see if the value exists in my table, I want it to check right away - is it blank (thus require data to be put into field) OR does it already exist. My question has to do with the first part - is it blank.
On this form, I also have a Cancel button. When the user click Cancel, I want it to close the form without saving any of the data.
Problem - If the user doesn't enter anything in the first field and clicks Cancel, it's running the On Exit code requiring data to be entered into the first field - which they don't have to because they are cancelling. I'm getting a Run-time error '2585'.
Is there anyway to have the Cancel code stop the On Exit code from running? Any other ideas?
Here is my code for Cancel:
Private Sub CancelFormButton_Click()
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End Sub
Here is part of my On Exit code for the field:
If IsNull(Me.txtSSN) Then
strMsg = "Social Security Number Must Not Be Left Blank!" & vbCrLf
strMsg = strMsg & "Do you want to add new veteran's record?" & vbCrLf
If MsgBox(strMsg, vbQuestion + vbYesNo, "Go to Record?") = vbYes Then
Me.txtSSN.SetFocus
Exit Sub
Else
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End If
Else
'RUNS THE REST OF CODE
End if
OnExit events occur everytime you leave the textbox, even a simple tab or click to go to another combo box or select some text will trigger it.
You may want to try the code in the AfterUpdates.
and try a ,nosave after the close form command...
In addition, any checks to ensure a field is filled/has data is usually encouraged to be performed in the Form_BeforeUpdate section... Or so I have been advised

Change current read-only form record to be editable

I am using Microsoft Access 2013 to create a database where I have a form that the default view is read-only to prevent accidental editing. I am currently trying to include a button to enable editing for the current record only. I tried using DoCmd.OpenForm to open the record in editable mode (since I am using that command elsewhere to open specific records), but it seems like it can not open a record within the same form with that command.
I thank you in advance for any advice on how to solve this issue.
You cannot edit anything else than the current record.
Use this code line:
Me.AllowEditions = True
But it doesn't make much sense as you could just open the form this way.
I learned this from the first VBA book I ever read (Access 97 VBA for Dummies, I think), and I have never seen anyone use the Tag property since, which has no other purpose than for you to find something to use it for. Enter "Lockable" in the tag property for any control (text box, combobox etc.) that you want to protect. Add a button named btnEdit to your form.
When you enter a record, the Form Current event will unlock the controls if it is a new record, and lock the controls if it is an existing record.
The button will then unlock to allow edits, and re-lock once the record is exited. I've found this to be very effective in preventing inadvertent edits.
Private Sub Form_Current()
Dim ctlCurr As Control
'Lock the record if it is not new. Prevents inadvertent edits.
If Me.NewRecord = False Then
For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Lockable" Then
ctlCurr.Locked = True
End If
btnEdit.Enabled = True
Next
End If
'Unlock a new record for editing.
If Me.NewRecord = True Then
For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Lockable" Then
ctlCurr.Locked = False
End If
Next
btnEdit.Enabled = False
End If
End Sub
Private Sub btnEdit_Click()
Dim ctlCurr As Control
'Unlocks a record for editing. Requires the operator to make the decision to edit.
If Me.NewRecord = False Then
For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Lockable" Then
ctlCurr.Locked = False
End If
Next
End If
End Sub

access form won't update while combobox is being edited, must move to next record

I have a continuous form with a list of comboboxes, pic here:
As you can see there is a small pencil on the left side of the access form to display that the current record in the form is being edited. I need to change this to a triangle so the other forms update properly with a button. Is there VBA to "lose focus" or change the pencil to a black triangle? I tried move to next record but for some strange reason it crashed. In addition it was fairly complex because I had to add logic if the user was on the last record, since it would throw an error because it can't go to next record on the last record. My vba is below (currently crashes access for some reason)
with recordset
if .recordcount=1 then
docmd.gotorecord record:=acfirst
elseif .absoluteposition = .recordcount - 1 then
docmd.gotorecord record:=acprevious
docmd.gotorecord record:=acnext
else
docmd.gotorecord record:=acnext
docmd.gotorecord record:=acprevious
end if
end with
end sub
The usual way is to save:
If Me.Dirty Then
Me.Dirty = False
End If
Pick a suitable event.
If Access is crashing, it usually means you need to back-up, compact and repair, and decompile.

Access 2010 Form won't save record

I have a form with some fields on it. Form was created from the table in question.
I've tried to use the button wizard to create a save button, I've tried to use the following:
Private Sub saveRecord_Click()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Nothing works, any method I try to save the info doesn't work. I've made sure the form is bound, as far as I can tell it is, it was created off that table.
I have this line when the form loads to make sure the form is on a new record:
Me.Recordset.AddNew
From my limited understanding of the language, setting the form to a new record and then doing a save command should work?
RunCommand acCmdSaveRecord will only do a save if there is something to save. Simply adding a record to the form's recordset doesn't give you anything to save.
Make this change to saveRecord_Click() to confirm whether that is the explanation:
Private Sub saveRecord_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "nothing to save"
End If
End Sub
Some other suggestions ...
Since you want to go to the new record when the form loads, use GoToRecord instead of Me.Recordset.AddNew:
Private Sub Form_Load()
DoCmd.GoToRecord Record:=acNewRec
End Sub
The version of saveRecord_Click() I suggested earlier was only to explore the problem. But for routine use, it doesn't make sense to allow the user to click a saveRecord button and then tell them there is nothing to save. It is better to only make the button clickable when there is something to save.
So you can disable the command button in the form's On Current event and enable it in the On Dirty event (which means there is something to save).
Private Sub Form_Current()
Me.saveRecord.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me.saveRecord.Enabled = True
End Sub
Also, as the last step of saveRecord_Click(), you may wish to SetFocus on another control and disable the command button.

How do I not commit a record on MS-Access?

I have a bound form in microsoft access that allows a user to add data to a table. I want to place a "cancel" button on the form, that if clicked, will stop the record commitment.
What is the command I need to cancel the record commitment?
It's been a few years since I've had to work with Access, but you should be able to undo changes to the current record using the command:
DoCmd.RunCommand acCmdUndo
or
Me.Undo
?
I have a similar situation where I want the user to confirm whether to save changes, and have used the form's BeforeUpdate event to ask the user if they want to save changes. It seems to work pretty well, at least in my case.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Do you want to save changes?", vbYesNo, "Confirm change") = vbNo Then
Cancel = True
Else
'Do Nothing
End If
End Sub