Access allow addings but no edits in certain columns in a subform - vba

I got question if it is possible to allow addings but no edits in certain columns in a subform? I can do this for the whole form, but i can't do this for single columns.
For example:
I want to be able to allow edits in [Type], and in [Omschrijving] I only want to allow adds and no edits.
I can lock certain columns. For example:
Private Sub Form_Load()
Me.Omschrijving.Locked = True
End Sub
But then I'm not able to make any adds. Is there another way to make this possible?

Only lock if not a new record. Use form Current event instead of Load.
If Not Me.NewRecord Then
Me.Omschrijving.Locked = True
End If
Might use Enabled property instead so control shows as unavailable for edit, in which case could use Conditional Formatting for textboxes and comboboxes instead of VBA. Rule something like:
Expression is: Not Forms!formname.NewRecord
Then click the Enable/Disable button so if this condition is met, control is disabled.

Related

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

Enabling only one checkbox in a row in Access Multiple Item Form

I have a multiple item form, where most of the checkboxes are disabled unless the user checks the box before it.
Currently I'm using:
If Me.Field1 Then
Me.chxField2.Enabled = True
Else
Me.chxField2.Enabled = False
End If
But if I check the box, it enables the Field2 checkbox for all of them, I only want it to enable the checkbox for one particular row.
Is this possible to do? If it isn't, that's ok but I just had to ask.
Edit: Thanks to Erik I did this in the BeforeUpdate like he described, and it gives the effect I wanted:
Private Sub chxField2_BeforeUpdate(Cancel As Integer)
Cancel = Not Field1
chxField2.Undo
End Sub
Unfortunately, it's not (as far as I know).
On a continuous form, there's actually only one instance of each control. This means that you can't have one of those Field2 checkboxes enabled, and another disabled.
Things you can do:
Use the Checkbox_BeforeUpdate event to check if Field1 is checkded. If it isn't checked, then rollback the change:
Private Sub Field2_BeforeUpdate(Cancel As Integer)
Cancel = Not Field1
End Sub
Also, if you weren't using a checkbox, you would be able to use conditional formatting to give the control a "disabled look". Unfortunatlely, conditional formatting doesn't apply to checkboxes.

MS Access - Disable Update on a Certain Field or Column

How do we disable update or edit on a certain column for a record. For example I have Product table with fields of ID, Description, Count. I want to disable the changes on Description only. I know how to do this in sql, but how about in a program for Access or VBA code?
If you're using a form to present the data using a text box control bound to the field, you can lock the field for edits in the field properties. The property is called "locked".
You can also use vba code to lock and unlock the field under certain conditions.
Sub form_current ()
If x = "superuser" then
Me!SalaryField.enabled=true
Me!SalaryField.locked=false
Else
Me!Salaryfield.enabled=false
Me!Salaryfield.locked=true
End if
End Sub

Word - how to uncheck checkboxes?

I have 4 checkboxes but we need to restrict selection to just a single one, meaning if you check the first, the other 3 will go unchecked. I know we could use ActiveX radio buttons but we'd prefer to avoid ActiveX if possible, plus with check boxes we have more control over the layout.
I've set the name of the checkbox appropriately to Check1:
And then I've put this very basic script into the Visual Basic section:
Private Sub Check1_Click()
Check1.Enabled = True
Check2.Enabled = False
Check3.Enabled = False
Check4.Enabled = False
End Sub
But unfortunately checking the first box doesn't uncheck the next 3.
Any ideas please? Thank you!
If these are Content Controls, as you indicate, then they do not have a CLICK event. Nor can they be identified by VBA by their Title property. The code you show us is for ActiveX controls, which you say you don't want to use...
Working with content control events is not as simple and intuitive as with ActiveX controls. Similar to form fields, Content Controls only have "editing" events that trigger on the user entering and exiting the content control. These events are available in the ThisDocument module, in the Document category.
The same ContentControlOnExit event triggers for ALL content controls in the document, so you need a Select Case or If conditional to query the ContentControl argument in order to know which content control was exited.
In order to address the other checkboxes you need to use the Document.SelectContentControlsByTitle (or ...ByTag) method which returns an array of all content controls with that title (or tag).
If you really want to emulate a "click" event then you need to insert a Custom XML Part in the document with nodes linked to the content controls. When the user changes the state of the control the ContentControlBeforeStoreUpdate event will trigger, letting you take action.
The property you need is Value, not Enabled.
The purpose of property Enabled is to prevent a control from being changed by user.
Additionaly, you need to prevent it from the events cascade. It means that when you change programatically the value of Check2, this will trigger Private Sub Check2_Click() and so on.
In order to make it work you should change your code like that:
Private Sub Check1_Click()
If Check1.Value Then
Check1.Value = True
Check2.Value = False
Check3.Value = False
Check4.Value = False
End If
End Sub
and similarly for the other check boxes.
For your purpose radio buttons will be better choice. Radio buttons have built-in functionality to uncheck currently selected button if other one is checked.

Disable checkboxes after selection is finished

I have a sheet, where one of the columns has 10 checkboxes in one where user can select multiple options.
Is there any way to keep this set of checks unchangeable after user has checked on needed options? Or any other way for user to select 1 or more options (not with list drop-downs)
My situation is like this:
For each row, the user has to select a range of checkboxes (labels of checkboxes tell about type of documents attached for that specific record) There is no further action, just check and save. Is there any macro, or any way to do this beside the actual way (with checkboxes)?
Private Sub CheckBox1_Click()
CheckBox1.Enabled = False
End Sub
I will explain you the complete process.
Add a checkbox Then
Make sure the Design Mode is Selected
Right Click on the CheckBox and click on View Code
And there place this code Make sure the CCheckbox name is according to your checkbox
Now whenever the user Checks the checkbox it will get disabled.