andigirlsc
Registered User.
- Local time
- Today, 15:57
- Joined
- Jun 4, 2014
- Messages
- 59
Details:
I have a New User form that I inherited. The form adds a new user to the Users table. It works perfectly, but I want to set up validation rules for the form that require First Name, Last Name and Username to be entered BEFORE the Users table is updated. (The password field is set by default to a generic password, so I'm not worried about this field. Users are prompted to change it upon initial usage of their new login.)
Problem:
The way the code is written, the Users table is updated automatically when the user clicks the "Add User" button on the New User form and the code in the Before Update event is completely ignored altogether.
Question:
How do I get the Before Update event to run before the Users table is updated with the new user's record?
What I have tried:
I tried placing the Before Update code in the cmdAdd_Click code, but the form still updates the Users table without all the required fields being filled in even though it shows me the validation messages for the empty fields.
I have tried calling the Before Update event in the cmdAdd_Click event, but then I got a VBA error. The code I used to do this was:
Here is the New User form code to add a new user below. It was placed in the On Click event because it is actually a button called Add User. I did not write this code.
Here is the Before Update Code I wrote that doesn't seem to be working:
I have a New User form that I inherited. The form adds a new user to the Users table. It works perfectly, but I want to set up validation rules for the form that require First Name, Last Name and Username to be entered BEFORE the Users table is updated. (The password field is set by default to a generic password, so I'm not worried about this field. Users are prompted to change it upon initial usage of their new login.)
Problem:
The way the code is written, the Users table is updated automatically when the user clicks the "Add User" button on the New User form and the code in the Before Update event is completely ignored altogether.
Question:
How do I get the Before Update event to run before the Users table is updated with the new user's record?
What I have tried:
I tried placing the Before Update code in the cmdAdd_Click code, but the form still updates the Users table without all the required fields being filled in even though it shows me the validation messages for the empty fields.
I have tried calling the Before Update event in the cmdAdd_Click event, but then I got a VBA error. The code I used to do this was:
Code:
Call Form_BeforeUpdate
Code:
'---------------------------------------------------------------------------
' cmdAdd_Click
' This event will add a new user including the following information:
' First name, Last name and Username.
'
'----------------------------------------------------------------------------
Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblUsers", dbOpenDynaset)
With rst
.AddNew
![First Name] = Me.txtFirstName
![Last Name] = Me.txtLastName
![MI] = Me.txtMI
![Password] = Me.txtPassword
![UserID] = Me.txtUserID
![Active] = Me.chkActive
![AccessID] = Me.cboAccessID
.Update
End With
If IsLoaded("frmUsers") Then
Forms!frmUsers.Requery
Forms!frmUsers.lstUsers.Requery
End If
MsgBox "New user has been successfully added to the database.", vbInformation + vbOKOnly, "User Update"
Exit_cmdAdd_Click:
Exit Sub
Err_cmdAdd_Click:
MsgBox Err.Description
Resume Exit_cmdAdd_Click
End Sub
Code:
'---------------------------------------------------------------
' Form_BeforeUpdate
'
' This event will prompt a validation rule that requires a
' First Name, Last Name, Username, Access Level and
' View level for each new user.
'---------------------------------------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
' Please enter a First Name
If IsNull(Me.txtFirstName) Then
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Please enter a First Name."
Style = vbOK + vbExclamation + vbDefaultButton2
Title = "Empty Name Field"
Help = "DEMO.HLP"
Ctxt = 1000
Cancel = True
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
' Please enter a Last Name
ElseIf IsNull(Me.txtLastName) Then
Msg = "Please enter a Last Name."
Style = vbOK + vbExclamation + vbDefaultButton2
Title = "Empty Name Field"
Help = "DEMO.HLP"
Ctxt = 1000
Cancel = True
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
' Please enter a Username
ElseIf IsNull(Me.txtUserID) Then
Msg = "Please enter a Username."
Style = vbOK + vbExclamation + vbDefaultButton2
Title = "Empty Username Field"
Help = "DEMO.HLP"
Ctxt = 1000
Cancel = True
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
End If
End Sub