Confirmation prompt for change to value on an Access form - ms-access

I am trying to revert back to the original value if, at the MsgBox prompt, the user chooses "No". However, this is not happening. Can you please have a look to see where I am going wrong?
Private Sub txtNov2_AfterUpdate()
Dim x As Integer
Dim NewValue As Integer
Dim OrigValue As Integer
NewValue = Me.txtNov2.Value
OrigValue = [Forms]![DataEntry]![txtNov2].OldValue 'Me.txtNov2.OldValue
If NewValue <> OrigValue Then
x = MsgBox("Value Has Changed Do you Want to Update?", vbQuestion + vbYesNo, "Value Change")
If x = vbYes Then
MsgBox ("Please Press Update Button")
btnUpdateData2.SetFocus
Else
txtNov2.SetFocus
'Me.Undo
txtNov2.Value = OrigValue
'Cancel = True
End If
End If
End Sub

It appears to me that your code is working, as far as it goes. I open the form...
...and change the value from 123 to 124...
...then when I hit the [Tab] key so the control loses the focus I get the MsgBox...
...and if I choose "No" then the value reverts to 123
The only difference between that state and the initial state (when I first opened the form) is that the form is still "dirty", as indicated by the "pencil" icon in the record selector on the left side of the form. If you want to completely undo all changes to the form then try replacing...
Else
txtNov2.SetFocus
'Me.Undo
txtNov2.Value = OrigValue
'Cancel = True
End If
...with...
Else
DoCmd.RunCommand acCmdUndo
End If

Related

MsgBox to confirm exit from form in Access

I have a single record form with contract details and a subform where I enter some additional information. The main form has a command button to close it.
I need to check 2 controls on closing(one in main form and one in subform) and if some conditions are met a Yes/No MsgBox should appear. If the user presses "Yes", the form closes, if he presses "No" the form remains open.
So far I have this:
Private Sub Form_Unload(Cancel As Integer)
Dim Response As Integer
Response = MsgBox("It looks like this contract is fixed. Would you like to edit the final price?", vbYesNo, "Database Information")
If Me![fixed price] = 0 And Me![Fixation Orders Subform1].Form!Text38 = "Final Fixed Price" Then
Cancel = True And Response
If Response = vbYes Then
Cancel = True
Else
Cancel = False
End If
Else
Cancel = False
End If
End Sub
Problems: Before I defined "response" the MsgBox appeared correctly but the form closed anyway. After I defined it to use it in the second "If":
The message box appears no matter what
The form closes either I press "Yes" or "No"
Additionally, after I solve this, on which event should I put this code to chack the conditions also when I go to next record? Thanks in advance.
I believe it should be something like this:
Private Sub Form_Unload(Cancel As Integer)
If Me![fixed price] = 0 And Me![Fixation Orders Subform1].Form!Text38 = "Final Fixed Price" Then
If MsgBox("It looks like this contract is fixed. Would you like to edit the final price?", vbYesNo, "Database Information") = vbYes Then
Cancel = True
End If
End If
End Sub

Find next record based on certain criteria

I am trying to use the .FindNext (and .FindPrevious) function on an update form "next button" to find the record that meets certain criteria.
Private Sub NextRecord_Click()
Dim foundmatch As Boolean
For x = 0 To 3 Step 1
With Me.RecordsetClone
.FindNext "[Sensitivity] = " & [TempVars]![AccessLevel] + x
If .NoMatch Then
foundmatch = False
Else
Me.Bookmark = .Bookmark
foundmatch = True
Exit For
End If
End With
Next
If foundmatch = False Then
MsgBox "No More Records"
End If
End Sub
Upon a user entering the database the users accesslevel is assigned to a temp variable (1 to 4), and each project has a sensitivity rating of 1 to 4. The code below was used and worked for both next and previous only in finding records when the sensitivity and accesslevel were equal but not for sensitivities below the users access level which they are qualified to see.
Private Sub PrevRecord_Click()
Dim Stringy As String
Stringy = "[Sensitivity] = " & [txtaccess]
With Me.RecordsetClone
.FindPrevious Stringy
If .NoMatch Then
MsgBox "No More Records"
Else
Me.Bookmark = .Bookmark
End If
End With
End Sub
Note: The form is pulled from a query with Sensitivity one of the fields, and [txtaccess] is a text box on the field with the default value set at [TempVars]![AccessLevel]. I've also tried changing it to:
Stringy = "[Sensitivity] >= " & [txtaccess]
but that doesn't work either
I was able to fix the problem by setting applying a filter for sensitivity on the actual forms On_Load event rather than the command button. It now works using a next record command button added with the default code/settings!

Access VBA - All checkboxes on form have been checked

I am relatively new to Access VBA and have a form that has around 30 checkboxes on it. When saving the form I want to ensure that all checkboxes have been ticked (set to true). The tickboxes have all got names SC1, SC2....SCN Is there a way to loop through each control and see if it has been set to true?
This is what I have tried but it doesnt seem to read the tickbox -
Private Sub Validate_Data(rstTop)
Dim n As Integer
Dim count As Integer
count = 0
For n = 1 To rstTop
If Form.Controls("SC" & n).Value = False Then
count = count + 1
End If
Next
If count <> 0 Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Else
End If
End Sub
Give this a try, it worked for me.
Option Compare Database
Option Explicit
Private Function Validate_Data() As Boolean
Dim ctl As Control
Validate_Data = True 'initialize
For Each ctl In Me.Form.Controls
If ctl.ControlType = acCheckBox Then
If (ctl.Name Like "SC*") And (ctl.Value = False) Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Validate_Data = False 'something isnt checked
Exit Function
End If
End If
Next ctl
End Function
Private Sub cmdGo_Click()
Validate_Data
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.

ms Access runtime error '2115" when setting ListIndex to -1

I have Find Forms that are basically a large list box with some filter, sorting and action buttons and some text boxes to implement the filter.
I have been using these forms for quite a while without problem. The SELECT button is disabled unless an item from the list box is selected. I am trying to unselect any listbox entry when the filtering or sorting is changed.
To do this I set the focus to the listbox (no multi selection option) to -1. There are a number of equivalent actions and I have tried most of them. Once ListIndex is set to -1 I get the '2115' runtime error if I execute a requery action on the listbox or a refresh action on the containing form. In addition, I cannot set the focus to any of the text boxes or buttons on the form getting a variety of runtime errors that say that the field must be saved.
Anybody have an ideas about how this is supposed to function?
In the code snippet the actual code getting the error is the .requery line near the bottom after the Unselect listbox comment
Here is a code snippet of one of the ways I have had this occur:
With Me.lbxFoundItems
If strCurrentSearchText = vbNullString Then 'Nothing to search for
.Visible = False
Call ButtonSettings
Exit Sub
End If
strQry = strSql & strWhere & strOrderBy
If Nz(.RowSource, vbNullString) = vbNullString Then 'First time through
Set qry = Nothing
On Error Resume Next
Set qry = CurrentDb.CreateQueryDef(strQname, strQry)
If qry Is Nothing Then
Set qry = CurrentDb.QueryDefs(strQname)
qry.sql = strQry
End If
colGarbage.Add CurrentDb.QueryDefs(qry.Name), qry.Name
.RowSource = qry.Name
Else
CurrentDb.QueryDefs(qry.Name).sql = strQry
End If
' Unselect the listbox entry
.SetFocus
.Selected(.ListIndex + 1) = False
.Requery
Me.Refresh
Me.tbxListCount = .ListCount - 1
.Visible = True
End With