Change the value of the Checkbox - ms-access

I add a new line of data into the table through the form. On the form I also have checkbox which is also integrated into the table. Ticking the checkbox leads to getting a value of -1. Is it possible to change -1 into a user defined value such as 'x'?

Simple answer: No.
More complex answer: You can use a hidden bound field, an unbound checkbox, VBA and default values to make a checkbox that behaves just like a bound checkbox that returns a different value.
Consider two fields, myUnboundCheckbox and myBoundTextfield. myBoundTextfield holds "X" for True, "Y" for False.
myUnboundCheckbox is an unbound checkbox, myBoundTextfield is a hidden bound text field
Then you can use the following:
Private Sub myUnboundCheckbox_AfterUpdate()
If myUnboundCheckbox Then
myBoundTextfield = "X"
Else
myBoundTextfield = "Y"
End If
End Sub
Private Sub Form_Current()
myUnboundCheckbox = myBoundTextfield = "X"
End Sub

Related

VBA enabling text and combo box in MS Access

I'd like to make a text box (Text28 hereafter) and a combo box (combo43 hereafter) enabled when another combo box (Combo26) takes the value "Resolved"
So this is the code, written in VBA, that should do that :
Private Sub Combo26_AfterUpdate()
If Combo26.Value = "Resolved" Then
Me.Text28.Enabled = True
Me.Combo43.Enabled = True
Else
Me.Text28.Enabled = False
Me.Combo43.Enabled = False
End If
End Sub
Unfortunatly, nothing happens when I try it. Would you have a idea ?
Thx
If you are pulling two columns as your row source (as indicated in your comment above), it looks like the first one that is displayed is the ID. You should target the second column to do your comparison, e.g.,
If Combo26.Column(1) = "Resolved" Then
...
That works for me!
Combo26.Column(0)
will give you the ID.

Why won't the ForeColor of this label visibly change when altered in a loop?

I am making a form that has groups of controls that I want to visibly enable/disable with a related toggle button (let's call them Group Toggles). Each group has a different variety of control types, so I made a common procedure to handle the toggling:
'constants for control ForeColors
Public Enum LabelForeColor
Default = 8355711
Off = 14277081
End Enum
Public Enum ListForeColor
Default = 4210752
Off = 12566463
End Enum
Public Sub EnableControl(Ctrl As Control, Enabled As Boolean)
With Ctrl
Select Case Ctrl.ControlType
Case acLabel
If Enabled Then .ForeColor = LabelForeColor.Default Else .ForeColor = LabelForeColor.Off
Debug.Print "LABEL", .ForeColor
Case acListBox
If Enabled Then .ForeColor = ListForeColor.Default Else .ForeColor = ListForeColor.Off
.Enabled = Enabled
Debug.Print "LIST", .ForeColor
Case acCommandButton
.Enabled = Enabled
Debug.Print "BUTTON", "NA"
Case acCheckBox
.Enabled = Enabled
Debug.Print "CHECK", "NA"
Case Else
Debug.Print "Control [" & .Name & "] is not of a type that EnableControl can handle."
End Select
End With
End Sub
Each group of controls is represented by a collection. When the form is loaded, every control with a particular tag property is added to the corresponding collection. The Group Toggles are not added to any collection and instead have event procedures that look like this:
Private Sub ToggleGroup1_AfterUpdate()
Dim State As Boolean
'a public function that converts the toggle button's value to a boolean
State = FormCommon.ToggleButtonState(ToggleGroup1.Value)
Dim iCtrl As Control
For Each iCtrl In Controls_ByPlant
FormCommon.EnableControl iCtrl, State
Next iCtrl
End Sub
When I click on a GroupToggle, all of the controls in the corresponding group visibly change appropriately, except for the labels. After an hour of troubleshooting, here's what I know:
The ForeColor property of the label does change, but not visibly.
When I call EnableControl on a label outside of a loop, the label visibly changes.
It doesn't matter if I pass the label object specifically to the subroutine or if I pass it from its group collection; the change is visible in both cases
If I toggle-disabled a label as part of the Group Toggle event and then call EnableControl specifically on that label to try to disable it again, there is no visible change (probably because the ForeColor property is already set to the "off" color)
Turning screen updating off with Application.Echo while the Group Toggle event runs and then turning it back on at the end of the event does not make a difference.
Making the Group Toggle event run with a For i = 1 to .Count instead of a For Each does not make a difference.
This problem also occurs when changing a different visual property instead, such as ForeTint.
(Per comments) Repaint does not make a difference
(Per comments) DoEvents does not make a difference
Why is this happening?
(First ever question, so apologies if I messed something up in the post)
This was interesting, but somewhat anticlimactic.
Your code does work for the labels, but what happens is this:
All labels are associated with input controls (as it is usual)
When you deactivate a group, you disable the input controls (.Enabled = Enabled)
This automatically sets the associated labels to a (system defined) light gray text color which cannot be changed.
This "disabled label" color is very similar to your LabelForeColor.Default color, so it is hard to see the change when toggling. But it does change.
Change your color constants to make the effect more visible:
Public Enum LabelForeColor
Default = vbRed ' 8355711
' the "Off" color is never visible, unless you add an un-associated label to a group
Off = vbBlue ' 14277081
End Enum
Edit: your test code FormCommon.EnableControl iCtrl, False works, because it only affects the label, but doesn't disable its associated list box.

Button click unclick ms-access

In access form, I'm hoping to click on a button and add information to an existing record using an update query. Ideally, when this happens, the button will change colors and appear 'activated'.
Then, if the user decides that the information that has been added needs to be removed, they can click the same button again. This removes the previously added information from the table and changes the button appearance to 'inactive'
You can't change the colour of a command button if you are using Accesss 2003 or earlier, but you can simulate a button using a label, and obviously you can change a labels caption and colours.
If you are using 2007 on-wards then substitute the Label name to your Command button name.
Using the On_current property of your form use something like
If Me.AddData = 1 Then
Me.YourLabel.Caption = "Remove Data"
Me.YourLabel.BackColor = VbRed
Else
Me.YourLabel.Caption = "Add Data"
Me.YourLabel.BackColor = VbGreen
End If
Then use a similar logic to run your update code from the On_click property of the label, based on the value of AddData.
If you're willing to have -1 and 0 in AddDate these can be easily converted to TRUE/FALSE.
This is taking a few liberties with the info you've given, but it's getting late in the day.
Your form has a command button (Command7) and a textbox (Text8).
Text8 has a control source linked to AddData (so it shows -1 or 0).
This code executes whenever you move to a different record. It checks the value in Text8 and changes the colour of the command button accordingly:
Private Sub Form_Current()
With Me
Select Case .Text8
Case -1
.Command7.BackColor = RGB(0, 255, 0)
Case Else
.Command7.BackColor = RGB(0, 0, 255)
End Select
End With
End Sub
This code on the click event of the command button will change the value in Text8 from True to False and vice versa.
It will then requery the form, forcing the Form_Current event to fire.
A requery moves the recordset back to the first record, so the bookmark moves it back to the record you were looking at.
Private Sub Command7_Click()
Dim bkmrk As String
bkmrk = Me.Bookmark
With Me
.Text8 = Not CBool(.Text8)
.Requery
Me.Bookmark = bkmrk
End With
End Sub
Edit:
Scrap that -1 and 0 malarkey....
Change the Select Case to Case 1 in the Form_Current event.
Change the .Text8 = Not CBool(.Text8) in the Command7_Click event to
.Text8 = Abs(Not CBool(.Text8 * (-1)))

VBA Add combo box values together

I have 3 combo boxes that contain numbers:
Me.Authorized
Me.Officer
Me.Enlisted
What I'm trying to do is add the values of Me.Officer and Me.Enlisted and make sure it equals Me.Authorized. I have all the other statements/commands figured out, but I can't wrap my head around this one.
The combo box selected value is a string, even when that string contains only digits. You can use Val() to convert that string to a number.
So your required condition can be expressed as ...
Val(Me.Officer) + Val(Me.Enlisted) = Val(Me.Authorized)
You can enforce that requirement in the form's Before Update event ...
Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Val(Me.Officer) + Val(Me.Enlisted) <> Val(Me.Authorized)) Then
MsgBox "Officer plus Enlisted must be equal to Authorized."
Cancel = True
End If
End Sub
That event procedure will abort a record save when your requirement is not satisfied.

ms access vba code I can disable a field based on content of another field, but my code to re-enable is not working

I have a simple form where I have a field InstrumentTypes_ID and a field DGLenders_ID.
When InstrumentTypes_ID equals "Mortgage" or "Modification", I want DGLenders_ID to stay enabled, but any other value should make DGLenders_ID become disabled.
I have the following code in an AfterUpdate for the InstrumentTypes_ID textBox, which to me seems would be sufficient to get this effect (However, when testing, I can type in "Warranty Deed" (or anything that's not "Mortgage" or "Modification"), which disables the DGLenders_ID field, but if I go back and update it with "Mortgage" or "Modification", it won't re-enable that field):
Private Sub InstrumentTypes_ID_AfterUpdate()
Me.DGLenders_ID.Enabled = True
If Me.InstrumentTypes_ID = "Mortgage" Then
Me.DGLenders_ID.Enabled = True
ElseIf Me.InstrumentTypes_ID = "Modification" Then
Me.DGLenders_ID.Enabled = True
Else
Me.DGLenders_ID.Enabled = False
End If
End Sub
You should try to put your code in a subroutine and call it on the current event of the form and in the lost focus event of the field.