I have a continuous form bound to a DAO recordsource. My conditional formatting rule looks like this:
Expression Is: [DateTimeDeleted] IS NOT NULL (fill textbox backgrounds with bright pink)
And then I have a delete button that shows on each record. The code for it is:
Private Sub cmdDelete_Click()
If Me.NewRecord = False Then
If IsNull(Me!DateTimeDeleted) = True Then
Me!DateTimeDeleted = Now()
DoCmd.RunCommand acCmdSaveRecord
Else
Me!DateTimeDeleted = Null
DoCmd.RunCommand acCmdSaveRecord
End If
Else
Me.Undo
End If
End Sub
I'm well aware that there's no code here to actually delete the record and I'm OK with that. My complaint is that the conditional formatting appears to be applied only when the form loads. Clicking the button above doesn't make the boxes turn pink for the record being "deleted". Is there some misunderstanding on my part here about how conditional formatting works?
FYI, I'm using MS Access 2013 32bit on Windows 8.1. Maybe that's the source of my problems. It isn't really my choice as the MS Design and Development Action Pack Subscription doesn't allow using Office 2010 anymore.
When dealing with issues like this, I like to use the Refresh command. Your modified code would look like this.
Private Sub cmdDelete_Click()
If Me.NewRecord = False Then
If IsNull(Me!DateTimeDeleted) = True Then
Me!DateTimeDeleted = Now()
DoCmd.RunCommand acCmdSaveRecord
Else
Me!DateTimeDeleted = Null
DoCmd.RunCommand acCmdSaveRecord
End If
Me.Refresh 'This is the inserted code
Else
Me.Undo
End If
End Sub
You can also use Me.Recalc instead of Me.Refresh. Recalc does less overhead.
Related
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
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.
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.
Does anyone have an idea what could prevent either of these commands from actually saving a form? Is there a setting I'm missing that could make the form "read-only"? I'm toggling the control.enabled property on the form on load, but I can't seen to make it stick once I've closed the form.
DoCmd.Close acForm, Me.Name, acSaveYes
DoCmd.Save acForm, Me.Name
Edit:
Some psuedo code to clarify.
This function runs on form load to enable/disable certain controls and works 100% as expected.
Private Sub Form_Load()
If IsNull(TempVars.Item(Me.Name)) Then
TempVars.Add Me.Name,1
If somecondition = true then
Me.Controls.Item("somecontrol").enabled = True
Else
Me.Controls.Item("somecontrol").enabled = False
End If
End If
End Sub
This click event closes the form and opens another form. The problem is, the control.enabled settings I set on form load do not save.
Private Sub button_OnClick()
DoCmd.OpenForm "someotherform", acnormal
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
The next time I open the first form, the control properties will not be set, and I need to set the conrol.enabled property again.
I've not been able to find any documentation, but it appears that you can not save design changes that are made in Normal mode. I ended up declaring a global collection type variable. I stored concatenated strings in this collection variable of the convention "Form.Name & Control.Name". If the form had never been opened, I ran the long check on which controls should be enabled, and stored them in this collection global. If the form had been opened at least once, I looped through the controls on the form checking to see if they were in my global collection of "authorized" controls. It feels really hackish, but it works pretty well.
The code you have (Close, then Save) fails on my testing because the Me.Name is empty after executing the Close. Although I don't know what other code you may have, I was able to get the Form to close once I switched the order.
I suggest you place the code in the 'Open' event as follows:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Save acForm, Me.Name
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
Do you want to change the enabled property on a condition? If you just need that control disabled IF the record ... then you'll want to move the enabling the form's Current event, which fires at every record navigation. Load and Open are too early and don't repeat at navigation.
Private Sub Form_Current()
If Me!X = True Then
Me.Controls("somecontrol").Enabled = False
Else
Me.Controls("somecontrol").Enabled = True
End If
End Sub
My question is fairly straight forward and hopefully simple.
I've got a database that has a long list of part numbers, and some items without part numbers. What i'd like to do is have a command button or Radio button that when activated, hides these null records, and when pressed again, shows them once more.
I can give more detail if needed
Thanks for any help!
Here is an example of using the Filter (same as using of out-of-the-box filtering capability from the Access/Office menu):
Setup:
Table (e.g. Parts):
Form (PartsForm):
Event handler for the Checkbox (OnClick):
Private Sub DisplayAllFilter_Click()
Me.Filter = ""
If Me.DisplayAllFilter.Value = False Then
Me.Filter = "[PartNumber] is not null"
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End Sub
Result would be something like these: