on.change text box to confirm update of information - ms-access

I have a database form that is linked to a table.
Users create records where data is inputted into a field called "seq.".
The users will need to change selected fields within the record, but the "seq." needs to remain unchanged, under most circumstances.
I want to have an on.change event where a message box comes up stating "you are about to change the seq. field, please confirm change" that makes the user aware that they may be changing this information.I need help creating this.

You need to make use the Control's Before Update event. It should disallow any changes, if it is meant to be.
Private Sub controlName_BeforeUpdate(Cancel As Integer)
If MsgBox("You are about to change the seq. field, please confirm change" _
vbYesNo) = vbNo Then
Cancel = True
Else
MsgBox "Changes have been confirmed."
End If
End Sub

Related

BeforeUpdate event of a linked subform control firing multiple times

Good afternoon,
I have to maintain an Access form which contain a linked subform (using Master and Child fields).
Basically, in the main form, the user choose a value in a combobox and the subform is automatically updated.
My issue is that I have a BeforeUpdate event on one field of my subform which is preventing to update the field (Cancel=true) when it does not meet the criteria. The alert msgbox should appear once if there is any error in the field but the BeforeUpdate event is always fired 3 times for unknown reason.
I have created a simple accdb file which reproduce my issue. It is located here: https://www.hightail.com/download/bXBhU2V0Q1JxRTFsQXNUQw.
Open the Form1, choose a value in the combobox and then try to update one of the letters in the subform by X and you will get the msgbox appearing multiple times.
Thanks in advance for your help on this issue as it's driving me crazy.
Sylvain
This happens because you do not change the value of Text0 back to what it was.
Try this
Private Sub Text0_BeforeUpdate(Cancel As Integer)
If Me.Text0 = "X" Then
MsgBox "Wrong value"
Cancel = True
Me.Text0.Undo
End If
End Sub
OK finally found the solution.
It was related to the text box used for storing the Link master fields used by my subform. This control contains the value of my combobox in order to filter out my subform.
The control source definition of this link master field used to be:
=myCombobox.column(0)
By replacing the definition by just:
=myCombobox
The beforeUpdate event in my linked subform is only triggered once and behaves as expected;
Private Sub Text0_BeforeUpdate(Cancel As Integer)
If Me.Text0 = "X" Then
MsgBox "Wrong Value"
Cancel = True
End If
End Sub
I can not explained the magic of why it is behaving that way but this is how I made it worked.
Sylvain

MS Access 2010 login form with access levels

My management wants an access database that can have a way to control which users can see specific fields on a form. For example, if a manager logs in, a form will display the performance rating field. For any other user, the performance rating field will not be visible.
So far the below are some of my options:
1) Use VBA to detect the User Name of the access application and if its Manager's name, then the textbox is visible.
2) Use a username reference table that requires users to login. Users with special access will have the textbox visible.
3) Have a special little button on the form that, if someone clicks, will load a small password dialog and then set the text visible.
Which option would be the most difficult to implement?
The idea of using a Login form for user specific information is something that is available all over the internet. I would suggest a combination of all three of your ideas would be just the perfection solution.
For either of the two methods you have (1 & 2) you first need a Table that will hold the information. The table does not need to be complex (at least not right away). The table should be of the following structure,
tbl_Employees
-------------
EmpID - Auto Number - PK
EmpNam - Text
EmpUserName - Text
EmpPassword - Text - Input Mask (If required)
IsManager - Yes/No - Default - No
Then you will have to create a Form, a basic form that will have three controls, two Text Boxes and one Button. The first text box is where you will enter the Employee User name and the second for Password. Finally a button, behind which the magic happens. The code (simplified) behind the button would be something along the lines of.
Private Sub LogInBtn_Click()
On Error GoTo errOccured
Dim eQryStr As String, empRS As DAO.Recordset
eQryStr = "SELECT EmpID, EmpUserName, EmpPassword, IsManager FROM tbl_Employees WHERE EmpUserName = '" & Me.UserNameTxt & "'"
Set empRS = CurrentDb.OpenRecordset(eQryStr)
If empRS.RecordCount <> 0 Then
If Me.PwdTxt = empRS!EmpPassword Then
If empRS!IsManager Then
DoCmd.OpenForm "ManagerForm"
Else
DoCmd.OpenForm "EmployeeForm"
End If
Me.Visible = False
Else
wrongEntry "Password"
End If
Else
wrongEntry "User Name"
End If
exitOnError:
Set empRS = Nothing
Exit Sub
errOccured:
wrongEntry "User Name/Password"
Resume exitOnError
End Sub
Private Sub wrongEntry(entityStr As String)
MsgBox entityStr & " is incorrect (OR) omitted, please check again.", vbCritical
End Sub
As you can see, I have used
(a) A Recordset object than a simple DLookup. I prefer recordset object, you can use a DLookup, but you have to make sure you handle Null (if the criteria is not met).
(b) A separate Form for Managers and Employees. I imagined there would be a lot more on a managers form that is not available on a Employee form. If you do not wish to go this way, but use one form you can, but you need to inform the opening form of who is logging in - Using the OpenArgs property of the OpenForm method.
Just if you feel to simply avoid all the hassle and use an Password box, to get the access of the TextBox. Simply follow the instructions on this thread - Password Box - A variant of Input Box. Then create a button on the form you currently have, then on click of the button, you simply code the following.
Private Sub AllowAccessButton_Click()
If Call_Password_Box = "yourPassword" Then
Me.yourHiddenTextBox.Visible = True
Else
MsgBox "Sorry you are not authorised to vies this information (OR) Incorrect Password", vbCritical
End If
End Sub
PS: The Hidden text control should be set to invisible, preferable in the Form current event.
I hope this helps.
I've implemented option nr 1 and 2, they we're quite easy to build.
option nr 3 seems equally difficult.
The question within my work environment would be which option would be low in (account)maintenance and which would demand little effort from the users.
From that view option nr.1 has been more successfull.
(And i would rather build different forms in stead of turning the view setting of field ON/OFF)

Make fields visible in MS Access form as certain fields are completed

I am building a form In MS Access for users to input data but there are too many possible fields. Most of the time only about half the fields will be used.
I thus would like to have certain fields appear only depending on what the user inputted on a prior given field.
Ex: user enters project number, title, then he checks a "yes/no" engineering. since he checked it this means engineering is impacted so a dozen fields that the user will have to fill out appear.
Is this possible:
1)without VBA
2)with VBA
Probably not possible without VBA.
With VBA for example:
Ensure your form is in Design view
Right click on your Combo Box, Build Event, Code Builder
This opens the code behind your form. It drops you into the default code for the BeforeUpdate event. We want the Change event instead, so at the top right change the drop down from BeforeUpdate to Change. This will give you a bit of code like this:
Private Sub Field1_Change()
End Sub
Inside here, you want to check the value of the combo box and hide fields as required:
Assuming the name of your combo box is Field1 (yours of course will be different), you add some code so it looks like this to hide Field2:
Private Sub Field1_Change()
If Field1.Value = "Yes" Then
Me.Field2.Visible = False
End If
End Sub
Note you need to know the names of all your fields - this is in the Other tab, Name field in the properties box (F4). You should give all of your fields sensible names so you can understand what is going on in the code.
For a check box, follow exactly the same procedure, but you probably need to use the Click event. Just experiment.
Sample check box code:
Private Sub Check5_Click()
' Note: vbTrue = -1
If Me.Check5 = vbTrue Then
MsgBox ("Ticked")
Else
MsgBox ("Not Ticked")
End If
End Sub
I have a form that will show certain fields after a list box value is selected. I use the AfterUpdate function. It has worked so far. My code is below. ProjectName and ProjectNumber are fields you only want displayed if Engineering is selected. OtherName and OtherNumber are fields you only want to show if it is a "NotEngineering" project.
Insert this code by clicking on the Field that selects the project type, go to the Event tab on the property sheet, and click "After Update" and choose code builder and paste in VBA.
Private Sub ProjectType_AfterUpdate()
If ProjectType.Value = "Engineering" Then
Me.ProjectName.Visible = True
Me.ProjectNumber.Visible = True
Else
Me.ProjectName.Visible = False
Me.ProjectNumber.Visible = False
End If
If ProjectType.Value = "NotEngineering" Then
Me.OtherName.Visible = True
Me.OtherNumber.Visible = True
Else
Me.OtherName.Visible = False
Me.OtherNumber.Visible = False
End If
End Sub
There is a way to do not-quite-this without VBA. I'd definitely recommend VBA though, because you can do a lot more with it.
Rather than hiding, try disabling the unnecessary fields with conditional formatting by expression.
right-click on the control you want disabled.
go down and click on 'Conditional Formatting'
Add a new rule
Select 'Expression is'
example:
[fld1]="yes"
hit the disabled box
click ok
click ok
now the control you've selected will disable if field 1 has "yes" selected.
I have the same problem and I did the following:
Private Sub Field1_Click()
If Field1 = "Yes" Then
Me.Field2.Visible = True
Else: Me.Field2.Visible = False
End If
End Sub
but now I have other problem, when I change record, the field that I choosen to be visible in the last record is now visible on the current record, although I have not choosen any option.
Thank you,

Allow input on an auto-number field in form to create new record in Access

I have a form that will display the last record of a table, with a few additional fields from other tables. The PK of the main table is auto-numbering. I need to be able to allow the user to make a change to the auto-numbering field, then determine if the record exists or not, if so - update, if not - insert new. I tried adding an event to the field on field change but whenever I try to click in the field on the form, it just beeps at me. Here is the code:
Private Sub JobID_Change()
'Check Bid#, if already exists open selected record for editing
rstOpenOrder.FindFirst "JobID = " & Me![JobID]
Do Until rstOpenOrder.NoMatch
With rstOpenOrder
'Add new record to end of Recordset object.
.Edit
'Edit data.
!LocationID = Me![LocationID]
!Description = Me![JobName]
!BaseBid = Me![BaseBid]
!GrossMargin = Me![GrossMargin]
!MDs = Me![ManDays]
!BidDate = Me![BidDate]
!ShortDate = Me![ShortDate]
!EmployeeID = Me![EmployeeID]
!GC = Me![GCs]
.Update
.FindNext "JobID = " & Me![JobID]
'Save changes.
End With
Loop
End Sub
If anyone can help, I would greatly appreciate it!
It's not generally a good idea to expose autonumber fields in the user interface. However, if you still want to provide the interface you've described you'll need to do the following:
Add an unbound text box (I'll call it IDLookup)
Add an AfterUpdate event to the text box to check for an existing record
If the record exists, filter the form to display that one record
If not, move the form to a new record (Me.Recordset.AddNew)
The key here is that your lookup must be done via an unbound text box. Access won't let you edit anything inside a textbox bound to an AutoNumber field (for good reason!).

Password protecting submit button on MS Access form still results in button submission

I'm trying to password protect a submit button on a form I'm building in Microsoft Access 2003. The button, when clicked, will add a new record to my database. The idea of the password protection is that when the user clicks the button, a prompt will appear asking for a password. They can either enter the password and click OK to proceed with verifying it has been entered properly, or they can click Cancel and close the prompt window (after which they will receive an confirmation alert). If the password matches what is hardcoded, the record will be added. If the password is not a match, an error message will display.
This should be easy enough. However, the record will ALWAYS be added to the database, no matter if the password is entered incorrectly, no password is entered, or the user cancels out of the password window. What am I doing wrong with the below code?
Private Sub AddLeadServerButton_Click()
Dim strPasswd
strPasswd = InputBox("Enter Password", "Restricted Form")
'Check to see if there is any entry made to input box, or if
'cancel button is pressed. If no entry made then exit sub.
If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInformation, "Required Data"
Exit Sub
End If
'If correct password is entered open Employees form
'If incorrect password entered give message and exit sub
If strPasswd = "thepassword" Then
DoCmd.GoToRecord , , acNewRec
Me.Parent!NewInstallation.Form!InstallationLeadServerComboBox.Requery
Me.Parent!NewReport.Form!LeadServerFilterComboBox.Requery
Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If
End Sub
To accomplish the behavior you want, you will have to set the form to prevent new records from being added. Then, ask the user for the password, set the form back to enable adding new records, and move to the new record.
If the property sheet is not
displayed, on the View menu, click
Properties to display the form's
property sheet.
In the Form property sheet, click
the Data tab, and then set the
AllowAdditions property to No.
Add a command button to the
form. Set the command
button's OnClick property to [Event
Procedure], and then click the Build
button to the right of the OnClick
property box. Type the following
statement in the Form_Customers
module:
Forms!Customers.AllowAdditions = True
http://support.microsoft.com/kb/208586
I don't recommend checking for the password after the user has already entered data. It gets frustrating when you fill out a form, only to find out at the end that you don't have rights to save your work.
Hook the BeforeInsert event on your form, and add the password check there. You can set Cancel=True if they don't provide the correct password, and that will cause the addition of the record to be abandoned.
Example:
Private Sub Form_BeforeInsert(Cancel As Integer)
If MsgBox("Insert new record here?", _
vbOKCancel) = vbCancel Then
Cancel = True
End If
End Sub
Try to avoid using bound forms where you can, this will give you greater control over your data with a little extra work, the best way to think of it is addressesing your data from opposite ends.
Bound forms are very much about telling the database to prevent updates that you don't wish to make whereas unbound forms are more about not making any changes until you are absolutely happy.
Just a personal preference but I think unbound are worth the time and effort.