Create a Log In button using VBA

G

Guest

I've created a form named User Accounts which consists of a Log In that
restrict access to unauthorized users. The form has a Username field and a
Password field.

I wanted to create a command button with VBA coding which can help me
restrict the access to those unauthorized users to the data modification or
elimination. The User Accounts form opens and ask the user for their username
and password. The user enters the data and clicks on the Log In button. With
the Log In button, I want VBA to check the values entered in the fields by
the user are in the Users table.

If the values the user entered are founded in the Username and Password
fields, I want VBA to show a MsgBox saying "Welcome to the system", something
like that and opens the Modify form. If the values aren't founded in the
Username and Password fields, I want VBA to show a MsgBox saying "Username
and/or password invalid. We're sorry, you are not authorized to enter to the
system..." and closes the User Accounts form.

I just have the idea of what I want the form to do, but I've no idea of how
I'm going to do it in VBA. Can anyone help me with this. I'll appreciate it.
 
G

Guest

The first point of interest is, this is not the best way to do this. You
should read up on and experiment with Access Security. It really is pretty
good.
Be aware, also, that the type of security you want to do is not really
secure at all. There are a lot of ways to get past your security. For
example, all I would have to do is link to your Users table from another mdb,
open it up and look at it. Now I have all your user names as their password.
If you don't have AllowBypassKey set off, I don't even have to do that, all
I have to do is hold down the shift key while I open your mdb, and I am in.
See what I mean? and those are only a couple of ways.

But, if this is what you want to do, I would suggest you don't need a
command button. All you need is two text boxes on a form. One for the User
Name and one for the password. This form should be the one identified in the
Startup Form/Page option. The form should have the Users table as its
recordsource. The following could go in the After Update event. It will
allow the user 3 tries:

Static intTries as Integer

If IsNull(DLookup("[UserName]", "Users", "[UserName] = '" & Me.txtUser _
& "' And [UserPwd] = '" & Me.txtPwd & "'")) Then
If intTries = 3 Then
MsgBox "Go Away Scumbag, You dont' belong here"
DoCmd.Quit
Else
If MsgBox("Incorrect User or Password Entered", _
vbRetryCancel+vbDefaultButton2, "login Error") =
vbCancel Then
DoCmd.Quit
Else
intTries = intTries + 1
Me.txtUser.SetFocus
End If
Else
MsgBox "Welcome to the System"
DoCmd.OpenForm "My Next Form"
DoCmd.Close acForm, Me.Name, acSaveNo
End If
 
G

Guest

I thought of using it with VBA before with a command button. The database is
going to be integrated as a web site using SQL and thought maybe I need a
command button in my log in form to continue to the modification part.

The part about editing and eliminating data, which is in the modification
part, is the part where i want to be secure by creating a log in form. The
viewing part is good since all is Read-Only data.


Klatuu said:
The first point of interest is, this is not the best way to do this. You
should read up on and experiment with Access Security. It really is pretty
good.
Be aware, also, that the type of security you want to do is not really
secure at all. There are a lot of ways to get past your security. For
example, all I would have to do is link to your Users table from another mdb,
open it up and look at it. Now I have all your user names as their password.
If you don't have AllowBypassKey set off, I don't even have to do that, all
I have to do is hold down the shift key while I open your mdb, and I am in.
See what I mean? and those are only a couple of ways.

But, if this is what you want to do, I would suggest you don't need a
command button. All you need is two text boxes on a form. One for the User
Name and one for the password. This form should be the one identified in the
Startup Form/Page option. The form should have the Users table as its
recordsource. The following could go in the After Update event. It will
allow the user 3 tries:

Static intTries as Integer

If IsNull(DLookup("[UserName]", "Users", "[UserName] = '" & Me.txtUser _
& "' And [UserPwd] = '" & Me.txtPwd & "'")) Then
If intTries = 3 Then
MsgBox "Go Away Scumbag, You dont' belong here"
DoCmd.Quit
Else
If MsgBox("Incorrect User or Password Entered", _
vbRetryCancel+vbDefaultButton2, "login Error") =
vbCancel Then
DoCmd.Quit
Else
intTries = intTries + 1
Me.txtUser.SetFocus
End If
Else
MsgBox "Welcome to the System"
DoCmd.OpenForm "My Next Form"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

Aya said:
I've created a form named User Accounts which consists of a Log In that
restrict access to unauthorized users. The form has a Username field and a
Password field.

I wanted to create a command button with VBA coding which can help me
restrict the access to those unauthorized users to the data modification or
elimination. The User Accounts form opens and ask the user for their username
and password. The user enters the data and clicks on the Log In button. With
the Log In button, I want VBA to check the values entered in the fields by
the user are in the Users table.

If the values the user entered are founded in the Username and Password
fields, I want VBA to show a MsgBox saying "Welcome to the system", something
like that and opens the Modify form. If the values aren't founded in the
Username and Password fields, I want VBA to show a MsgBox saying "Username
and/or password invalid. We're sorry, you are not authorized to enter to the
system..." and closes the User Accounts form.

I just have the idea of what I want the form to do, but I've no idea of how
I'm going to do it in VBA. Can anyone help me with this. I'll appreciate it.
 
G

Guest

Question I have:

the "[UserName]" represents the name of the field or the variables I create
in VBA. The same I'll ask you with Me.txtUser.

And in Me.Name I'll write the name of the form I want to close.
 
G

Guest

If you are saying that the login will be needed to get to the modification
form, then whereever you open the modification form, instead open the login
form and let it open the modification form once the login is complete.

Aya said:
I thought of using it with VBA before with a command button. The database is
going to be integrated as a web site using SQL and thought maybe I need a
command button in my log in form to continue to the modification part.

The part about editing and eliminating data, which is in the modification
part, is the part where i want to be secure by creating a log in form. The
viewing part is good since all is Read-Only data.


Klatuu said:
The first point of interest is, this is not the best way to do this. You
should read up on and experiment with Access Security. It really is pretty
good.
Be aware, also, that the type of security you want to do is not really
secure at all. There are a lot of ways to get past your security. For
example, all I would have to do is link to your Users table from another mdb,
open it up and look at it. Now I have all your user names as their password.
If you don't have AllowBypassKey set off, I don't even have to do that, all
I have to do is hold down the shift key while I open your mdb, and I am in.
See what I mean? and those are only a couple of ways.

But, if this is what you want to do, I would suggest you don't need a
command button. All you need is two text boxes on a form. One for the User
Name and one for the password. This form should be the one identified in the
Startup Form/Page option. The form should have the Users table as its
recordsource. The following could go in the After Update event. It will
allow the user 3 tries:

Static intTries as Integer

If IsNull(DLookup("[UserName]", "Users", "[UserName] = '" & Me.txtUser _
& "' And [UserPwd] = '" & Me.txtPwd & "'")) Then
If intTries = 3 Then
MsgBox "Go Away Scumbag, You dont' belong here"
DoCmd.Quit
Else
If MsgBox("Incorrect User or Password Entered", _
vbRetryCancel+vbDefaultButton2, "login Error") =
vbCancel Then
DoCmd.Quit
Else
intTries = intTries + 1
Me.txtUser.SetFocus
End If
Else
MsgBox "Welcome to the System"
DoCmd.OpenForm "My Next Form"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

Aya said:
I've created a form named User Accounts which consists of a Log In that
restrict access to unauthorized users. The form has a Username field and a
Password field.

I wanted to create a command button with VBA coding which can help me
restrict the access to those unauthorized users to the data modification or
elimination. The User Accounts form opens and ask the user for their username
and password. The user enters the data and clicks on the Log In button. With
the Log In button, I want VBA to check the values entered in the fields by
the user are in the Users table.

If the values the user entered are founded in the Username and Password
fields, I want VBA to show a MsgBox saying "Welcome to the system", something
like that and opens the Modify form. If the values aren't founded in the
Username and Password fields, I want VBA to show a MsgBox saying "Username
and/or password invalid. We're sorry, you are not authorized to enter to the
system..." and closes the User Accounts form.

I just have the idea of what I want the form to do, but I've no idea of how
I'm going to do it in VBA. Can anyone help me with this. I'll appreciate it.
 
G

Guest

[UserName] would be the name of the field in you Users table where you keep
user names. I made it up because I don't know the real field name. Change
it to match what your field name really is.
The same is true for Me.txtUser. Me. is shorthand for the form you are in.
It is the same as writing Forms!MyFormName
txtUser is a name I made up for the control on you form where the user would
enter his/her name.

The same would be true for the password field name and the password control
name.

You can leave Me.Name as is. Once you open your modification form, you want
to close the login form. Since in the previous line, you opened the
modification form, if you coded:
DoCmd.Close
it would close the modifcation form. The form of the Close method is
Object type - in this case a form, acForm
Object Name - in this case Me.Name will return the name of the current
(login) form and thus close the correct form.
 
A

Aya via AccessMonster.com

Here's what I did trying with your idea:

Private Sub Log_In_Click()
Static intTries As Integer

If IsNull(DLookup("[username]", "Cuentas Usuarios", "[username] = '" &
Forms!Cuentas_Usuarios!username.Text _
& "' And [password] = '" & Forms!Cuentas_Usuarios!password.Text &
"'")) Then
If intTries = 3 Then
MsgBox "Lo sentimos,usted no tiene acceso al sistema. Cualquier
duda o problema, comuníquese con Joann Molina a la ext. 1750"
DoCmd.Quit
Else
If MsgBox("Nombre y/o contraseña inválida.", vbRetryCancel +
vbDefaultButton2, "login Error") = vbCancel Then
DoCmd.Quit
Else
intTries = intTries + 1
Forms!Cuentas_Usuarios!username.Text.SetFocus
End If
Else
MsgBox "Bienvenido a Modificar ISA."
DoCmd.OpenForm "Modificar ISA"
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End Sub

When I run it, an error appears saying:

"The expression On Click you entered as the event property setting produced
the following error: Else without If. " I think maybe it should be written:
Else If. What do you think?
 
A

Aya via AccessMonster.com

I made some little corrections here in the coding:

Private Sub Log_In_Click()
Static intTries As Integer

If IsNull(DLookup("[username]", "Cuentas Usuarios", "[username] = '" &
Forms!Cuentas_Usuarios!username.Text _
& "' And [password] = '" & Forms!Cuentas_Usuarios!password.Text &
"'")) Then
If intTries = 3 Then
MsgBox "Lo sentimos,usted no tiene acceso al sistema. Cualquier
duda o problema, comuníquese con Joann a la ext. 1750"
DoCmd.Quit
Else
If MsgBox("Nombre y/o contraseña inválida.", vbRetryCancel +
vbDefaultButton2, "login Error") = vbCancel Then
DoCmd.Quit
Else
intTries = intTries + 1
Forms!Cuentas_Usuarios!username.Text.SetFocus
End If
End If
Else
MsgBox "Bienvenido a Modificar ISA."
DoCmd.OpenForm "Modificar ISA"
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End Sub

I got corrected the If...then statement, but now another error appears. The
error says:

"Run-time error '2450':
Impact Safety Analysis can't find the form 'Cuentas Usuarios' referred to in
a macro or Visual Basic code."

Impact Safety Analysis is the name of my database, just in case you don't
know what it is.

What's missing in the coding that I should add to make it work?
 
G

Guest

I tried to reply earlier this morning, but there seems to be some troulbe
within our network. I see you found and correct the End If problem. I
believe you did it correctly.
I don't see what the problem with the form name is. The message is puzzling
because in your code you call it Cuentas_Usuarios, but the message is saying
Cuentas Usuarios (no underline). Which is correct? If the code is in this
from, then you don't need to use Forms!Cuentas_Usuarios, use can just use Me.

One other thing that may be a problem is that you are using the Text
property for your controls:
Forms!Cuentas_Usuarios!username.Text
This will probably cause an error. You can only use the Text property when
the control has the focus. In any case, it is not necessary, because using
Forms!Cuentas_Usuarios!username will return the current value.

I would try chaning it to Me.username.
 
G

Guest

Finally, it worked. The whole Log In form is working the way I wanted. Here's
what I wrote basing of your comments:

Private Sub Log_In_Click()
Static intTries As Integer

If IsNull(DLookup("[username]", "Cuentas Usuarios", "[username] = '" &
Me.username _
& "' And [password] = '" & Me.password & "'")) Then
If intTries = 3 Then
MsgBox "Lo sentimos,usted no tiene acceso al sistema. Cualquier
duda o problema, comuníquese con Joann Molina a la ext. 1750"
DoCmd.Quit
Else
If MsgBox("Nombre y/o contraseña inválida.", vbRetryCancel +
vbDefaultButton2, "login Error") = vbCancel Then
DoCmd.Quit
Else
Me.username.SetFocus
End If
End If
Else
MsgBox "Bienvenido a Modificar ISA."
DoCmd.OpenForm "Modificar ISA"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

I appreciate very much for your help. Gosh, I want to celebrate, lol. Thank
youuu.
 
G

Guest

de nada

Aya said:
Finally, it worked. The whole Log In form is working the way I wanted. Here's
what I wrote basing of your comments:

Private Sub Log_In_Click()
Static intTries As Integer

If IsNull(DLookup("[username]", "Cuentas Usuarios", "[username] = '" &
Me.username _
& "' And [password] = '" & Me.password & "'")) Then
If intTries = 3 Then
MsgBox "Lo sentimos,usted no tiene acceso al sistema. Cualquier
duda o problema, comuníquese con Joann Molina a la ext. 1750"
DoCmd.Quit
Else
If MsgBox("Nombre y/o contraseña inválida.", vbRetryCancel +
vbDefaultButton2, "login Error") = vbCancel Then
DoCmd.Quit
Else
Me.username.SetFocus
End If
End If
Else
MsgBox "Bienvenido a Modificar ISA."
DoCmd.OpenForm "Modificar ISA"
DoCmd.Close acForm, Me.Name, acSaveNo
End If

I appreciate very much for your help. Gosh, I want to celebrate, lol. Thank
youuu.


Klatuu said:
I tried to reply earlier this morning, but there seems to be some troulbe
within our network. I see you found and correct the End If problem. I
believe you did it correctly.
I don't see what the problem with the form name is. The message is puzzling
because in your code you call it Cuentas_Usuarios, but the message is saying
Cuentas Usuarios (no underline). Which is correct? If the code is in this
from, then you don't need to use Forms!Cuentas_Usuarios, use can just use Me.

One other thing that may be a problem is that you are using the Text
property for your controls:
Forms!Cuentas_Usuarios!username.Text
This will probably cause an error. You can only use the Text property when
the control has the focus. In any case, it is not necessary, because using
Forms!Cuentas_Usuarios!username will return the current value.

I would try chaning it to Me.username.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top