MS-Access Check box IF statements - ms-access

How would i add a check("chk3") that will be ticked when ("Customer order Number") field has been inputted. when an order number is entered, then the check box will tick..
i also would like to know if it is possible that When the value field ("Value") is under £10,000 then the check box ("chk2") will tick. if it is over £10,000 then it will not tick

You may use AfterUpdate events for such validations.
Nz function checks for null value and converts them to empty string & 0 respectively
For Customer Order Number:
Private Sub txtCustomerOrderNo_AfterUpdate()
If Nz(Me.txtCustomerOrderNo, "") <> "" Then
Me.chk3 = True
Else
Me.chk3 = False
End If
End Sub
For Project value is less than 10K:
Private Sub txtProjEstimate_AfterUpdate(Cancel As Integer)
If Nz(Me.txtProjEstimate, 0) < 10000 Then
Me.chk2 = True
Else
Me.chk2 = False
End If
End Sub

Depends on your version of Access. In older versions, it was the AfterUpdate event. In newer ones, I think it's OnExit.
In whichever event of Customer Order Number, just set its value to True.
Private Sub CustomerOrderNumber_Exit(Cancel As Integer)
Me.chk3 = true
End Sub

It sort of depends what you're trying to do with the chk3 validation. If you're looking for a specific structure. For instance, a 10 digit number, then you would want a validation along those lines within the IF Statement. Something like:
(Using AfterUpdate)
If Me.[Customer order Number] LIKE "##########" Then
Me.chk3 = True
End If
Similiarly with the validation on the ck2 > £10,000 (assuming this is an appropriately designated currency field):
If Me.[Value] <= 10000 Then
Me.chk2 = True
End If

Related

option button (radio button) on each record of continuous form

This is for a project in MS Access 2016. I'd like to implement a radio button toggle across all records displaying in a continuous form. only one record can have the toggle "on" so when it's clicked it has to reset the previous record's flag to off. I'm only finding help on radio button usage to select from multiple values of a field for one record, usually on single form. Can this be done?
Radio button must be bound to a yes/no field then use UPDATE action SQL to make sure all records except current have field set to 0. Need a unique record identifier field such as autonumber.
Private Sub Option29_Click()
CurrentDb.Execute "UPDATE tablename SET fieldname = 0 WHERE ID <>" & Me.ID
End Sub
Be aware that in a multi-user database users can conflict with each other and another solution will be needed. Depends what you need to do with the selected record.
Use the RecordSetClone of the form:
Private Sub Active_AfterUpdate()
Dim Records As DAO.Recordset
Me.Dirty = False
If Me!Active.Value = True Then
Set Records = Me.RecordsetClone
Records.MoveFirst
While Not Records.EOF
If Records!Id.Value <> Me!Id.Value Then
If Records!Active.Value = True Then
Records.Edit
Records!Active.Value = False
Records.Update
End If
End If
Records.MoveNext
Wend
Records.Close
End If
End Sub
You can do it in easy way:
1- Add to your table a new Yes/No Field ( e.g named CurrentRec)
2- Put the Radio Button and link it to this field ( e.g named rdCurrRec)
3- For the radio button Set 'Enabled' property to False and 'Locked' property to 'True'. This will disable Click - Double Click and Hover response.
4- Put the following code
Dim PreBookmark(4) As Byte 'Save old bookmark
Private Sub Form_Current()
If Not Me.NewRecord Then 'Make sure not on new record
Me.rdCurrRec = True
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
PreBookmark(0) = Me.Bookmark(0)
PreBookmark(1) = Me.Bookmark(1)
PreBookmark(2) = Me.Bookmark(2)
PreBookmark(3) = Me.Bookmark(3)
Else 'If new record remove bullet from previous record
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
End If
End Sub
Private Sub Form_Close()
'Remove the mark before exiting to avoid it appear next time to open the form in two places
If Not Me.NewRecord Then
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
End If
End Sub
This will not iterate all the data so for large number of records it will not delay the response as it works only on two records

VBA: need to default control value if only 1 possible result or show list if more than possible result

I'm trying to write a bit of code, in VBA, that would restrict the possible values in a select list if certain criteria are met or, if only one possible value, default the value in the control to that one possible value or return a null list if no possible values.
To be more specific, in a form, I have an EventDate and ProtocolID. The protocols have a BeginImplementationDate and EndImplmentationDate. If the EventDate falls between the BeginImplementationDate and EndImplementationDate (which can be null, if the protocol is still active) of more than 1 protocol, I need to show all the ProtocolIDs for those protocols in the drop-down list. If only 1 protocol meets that criteria, the field needs to default to the ProtocolID that qualifies. If zero protocols meet the criteria, the drop-down list will be empty.
You could set the DefaultValue of the combobox with a small subfunction which you, for example, could call when opening the form:
Private Sub Form_Load()
Call SetDefaultProtocol
End Sub
Private Sub SetDefaultProtocol()
Dim DefaultValue As String
With Me!ComboProtocol
If .ListCount = 1 + Abs(.ColumnHeads) Then
DefaultValue = .ItemData(Abs(.ColumnHeads))
End If
.DefaultValue = DefaultValue
End With
End Sub
The following is what I did to resolve the problem.
Private Sub txtEventDate_AfterUpdate()
Dim intProtocolCount As Integer
Dim intProtocolID As Integer
intProtocolCount = DCount("ID", "lu_Protocol", "BeginImplementationDate<=#" & Me.EventDate & "# and Nz([EndImplementationDate],Date())>=#" & Me.EventDate & "#")
If intProtocolCount = 1 Then
intProtocolID = DLookup("ID", "lu_Protocol", "BeginImplementationDate<=#" & Me.EventDate & "# and Nz([EndImplementationDate],Date())>=#" & Me.EventDate & "#")
Me.cmbProtocol = intProtocolID
Me.cmbProtocol.Requery
ElseIf intProtocolCount = 0 Then
MsgBox ("No Protocol is active for this Event Date. If Event Date is correct, please contact the Data Manager to update the Protocol list in the database.")
Me.cmbProtocol.Requery
Else
Me.cmbProtocol.Requery
End If
End Sub

Select Case with Radio Buttons - Having trouble w/ Null Value

Having a bit of trouble. I have a simple situation: 2 Radio buttons, Accept or Reject. Instead of doing IF statements I feel an obligation to do Select statements.
As I walk through my code, it gets to the Case rdbApprove.Value and complains that the value is null. I figure it isn't true, thus it should pass to the next Case. Nope - it hangs on the fact that it is null.
In an effort to try and get rid of the null value I tried the NZ(rdbApprove.Value, False) to try and tell it that it was false but that still isn't working.
Select Case True
Case rdbApprove.Value
Case rdbReject.Value
Case Else
MsgBox "ERROR!"
End Select
The usual way this is done is to include your radio buttons in an option group. Then your code can reference the option group .Value.
My form includes an option group named Frame15 which includes an "Approve" radio button whose Option Value is 1, and a "Reject" button whose Option Value is 2.
I also added a command button to inspect the option group .Value. Here is its click event procedure.
Private Sub cmdOptionValue_Click()
Dim strPrompt As String
Select Case Me.Frame15.value
Case 1
strPrompt = "approved"
Case 2
strPrompt = "rejected"
Case Else
If IsNull(Me.Frame15.value) Then
strPrompt = "Null"
Else
strPrompt = "this should not happen"
End If
End Select
MsgBox strPrompt
End Sub

OldValue and Value same on radio button change Access 2007 VB

I have an access 2007 front-end app. On a particular form, there are 2 radio buttons in a radio button group. I am trying to detect when the radio button group is changed and capture the old and new values, but my OldValue and Value properties are = in the save event, even if I have changed it. The OldValue is equal to the New radio button value, not what it was originally.
I tried coding this in the form's Save subroutine. The intent was to compares the RB value with the original dataset value to force setting the old value, but it doesn't like the 'SET' statements
If fraResistOption.Value = 1 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 2 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "Y"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "Y"
End If
End If
End If
End If
Could someone suggest a way to do this? Please and thank you.
The .OldValue property of the Option Group (sometimes referred to as "Frame") does work. I have a table named [optValues]:
[ID] - AutoNumber
[optValue] - Numeric (Long Integer)
It contains one record:
ID optValue
1 3
My form's Record Source is the [optValues] table. The form has an Option Group named "Frame0" whose Control Source is the [optValue] field. It contains three Option buttons
label: "foo", value: 1
label: "bar", value: 2
label: "baz", value: 3
The After Update event handler for Frame0 is:
Private Sub Frame0_AfterUpdate()
MsgBox "Old value: " & Me.Frame0.OldValue & ", New value: " & Me.Frame0.Value
End Sub
When I open the form, "baz" is selected (because [optValue] is 3 in the table):
When I click on "foo" I immediately see the (correct) old and new values:
The only way I can think of to detect and capture changes to Option-Group values is to handle the Form_Current event (save the Option Group value), then also handle the Option-Group After event. Although you can change the Option-Group.Value, OldValue is likely a protected (read-only) property. Hope something like the following helps:
Dim OldValue As Byte
Dim CurrentValue As Byte
Private Sub Form_Current()
OldValue = Frame0.Value
End Sub
Private Sub Frame0_AfterUpdate()
CurrentValue = Frame0.Value
Debug.Print "AFTER: OldValue=" & OldValue & "' CurrentValue=" & CurrentValue
End Sub
I don't know why but .oldvalue doesn't work for me.
If you are in the same boat as me, you can use the BeforeUpdate event of the optionGroup and set a static variable.
Then read the static variable in afterUpdate event and reset it for the next change.

Determine whether a Access checkbox is checked or not

Such a simple question, but I cannot find the answer (Google, MS help, SO):
How can I check by VBA whether an unbound checkbox on an Access form is checked by the user or not? Can't find the right property.
UPDATE:
I used this code after the suggestions of #HansUp and #RC:
Private Sub CmdTest_Click()
MsgBox "Check1.Value = " & Me.Check1.Value
MsgBox "Check2.Value = " & Me.Check2.Value
End Sub
Private Sub Form_Load()
Me.Check1.Value = 25
Me.Check2.Value = 50
End Sub
2nd UPDATE:
The code should be this (thanks to #David-W-Fenton):
Private Sub CmdTest_Click()
If Me.Check1 = True Then
MsgBox "Check1 IS CHECKED"
Else
MsgBox "Check1 IS NOT CHECKED"
End If
If Me.Check2 = True Then
MsgBox "Check2 IS CHECKED"
Else
MsgBox "Check2 IS NOT CHECKED"
End If
End Sub
Private Sub Form_Load()
' set first checkbox default checked
Me.Check1.Value = True
' set second checkbox default unchecked
Me.Check2.Value = False
End Sub
Checkboxes are a control type designed for one purpose: to ensure valid entry of Boolean values.
In Access, there are two types:
2-state -- can be checked or unchecked, but not Null. Values are True (checked) or False (unchecked). In Access and VBA, the value of True is -1 and the value of False is 0. For portability with environments that use 1 for True, you can always test for False or Not False, since False is the value 0 for all environments I know of.
3-state -- like the 2-state, but can be Null. Clicking it cycles through True/False/Null. This is for binding to an integer field that allows Nulls. It is of no use with a Boolean field, since it can never be Null.
Minor quibble with the answers:
There is almost never a need to use the .Value property of an Access control, as it's the default property. These two are equivalent:
?Me!MyCheckBox.Value
?Me!MyCheckBox
The only gotcha here is that it's important to be careful that you don't create implicit references when testing the value of a checkbox. Instead of this:
If Me!MyCheckBox Then
...write one of these options:
If (Me!MyCheckBox) Then ' forces evaluation of the control
If Me!MyCheckBox = True Then
If (Me!MyCheckBox = True) Then
If (Me!MyCheckBox = Not False) Then
Likewise, when writing subroutines or functions that get values from a Boolean control, always declare your Boolean parameters as ByVal unless you actually want to manipulate the control. In that case, your parameter's data type should be an Access control and not a Boolean value. Anything else runs the risk of implicit references.
Last of all, if you set the value of a checkbox in code, you can actually set it to any number, not just 0 and -1, but any number other than 0 is treated as True (because it's Not False). While you might use that kind of thing in an HTML form, it's not proper UI design for an Access app, as there's no way for the user to be able to see what value is actually be stored in the control, which defeats the purpose of choosing it for editing your data.
Check on yourCheckBox.Value ?