Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Log in for my VB Applicaiton

Status
Not open for further replies.

BSG75

Technical User
Mar 18, 2005
23
US
I have a Visual Basic application that many users are using over a network. I want to creat a form that opens first before the main application form and where the users have to enter in a specified id or password. I have created at MS database that holds the users id and the information i want to use for passwords. In VB i have created a new form and have a label and text box on the form. How do i setup the user to input the id or password and then validate that from the database and then load the application.
 
First, you will want to make your login form be the 'first' form they see. To do this...

Click Project -> Project Properties
On the general tab, set the startup object to your login form.

On the login form, you will need to have a login button. When the user clicks the login button, you will have to validate the password. If acceptable, then load your existing main form and unload the login form.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
A function to give the Ok
Code:
Function EnterOk(strUserID As String, strPassword As String ) As Boolean
Dim bEnterOk As Boolean

'Use a connection object to your mdb

    Dim App_Cnn As ADODB.Connection
    Set App_Cnn = New ADODB.Connection
    With App_Cnn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("Data Source") = FullPathAndNameofMDBFile
        .Properties("Mode") = adModeShareDenyNone
        .Open
    End With

'Use a recordset to read from the table

Dim rst As ADODB.Recorset
Dim strSQL As String
strSQL = "SELECT ThePasswordField " & _
         "FROM yourPasswordsTableName " & _
         "WHERE TheUserIDField='" & strUserID & "'"
Set rst = App_Cnn.Execute (strSQL)
bEnterOk = False
If rst.EOF And rst.BOF Then
   MsgBox "No such user"
Else
    If Trim(UCase(strPassword)) = UCase(rst.Fields(0)) Then
        MsgBox "Welcome my dear user"
        bEnterOk = True
    Else
        MsgBox "Wrong Password"
    End If
End If
EnterOk = bEnterOk 
End Function
You need to add a reference to your version for the Microsoft ActiveX Date Objects 2.x Library

Of course you should set some sort of security on the mdb...
And do some text replacing for chr(39), chr(34), chr(32) with NULL for the text box they are typing the password.
 
Or make "Sub Main" your start up and then
Code:
Sub Main()

frmLogin.Show

If LoginSuccess Then
   frmMain.Show
End If

End Sub
Where [blue]LoginSuccess[/blue] is some variable set by frmLogin that determines if the user supplied a valid userid and password.
 
I will try it out and let you know what happens.
 
To JerryKlmns:

Why do you UCase in comparison? This was "PaSSwoRd" will be equal to "pASsWOrD".

I think making password case sensitive - by skipping UCase in code - would be a better choice.

---- Andy
 
Do i put this information on the button for the log in or in the load form?
 
You need to have the log in logic in a LogIn command button.

In Form_Load you do not have any password from the user yet (but you may have user name already from Windows, if you want to go that route).

---- Andy
 
i keep getting a compiler error expected end sub

Private Sub cmdOK_Click()

Function EnterOk(strUserID As String, strPassword As String) As Boolean
Dim bEnterOk As Boolean

'Use a connection object to your mdb

Dim App_Cnn As adoLOGIN
Set App_Cnn = New adoLOGIN
With App_Cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Passwords") = "C:\Documents and Settings\lp630cb\Desktop\Positions.mdb"
.Properties("Mode") = adModeShareDenyNone
.Open
End With

'Use a recordset to read from the table

Dim rst As adoLOGIN.Recorset
Dim strSQL As String
strSQL = "SELECT Password " & _
"FROM Passwords " & _
"WHERE Log in ID ='" & strUserID & "'"
Set rst = App_Cnn.Execute(strSQL)
bEnterOk = False
If rst.EOF And rst.BOF Then
MsgBox "No such user"
Else
If Trim(UCase(strPassword)) = UCase(rst.Fields(0)) Then
MsgBox "Welcome my dear user"
bEnterOk = True
Else
MsgBox "Wrong Password"
End If
End If
EnterOk = bEnterOk
End Function
End Sub
 
You have included your function definition in the cmdOK_click() event.

try this
Code:
Private Sub cmdOK_Click()
[COLOR=red]End Sub[/color]
Function EnterOk(strUserID As String, strPassword As String) As Boolean
Dim bEnterOk As Boolean

'Use a connection object to your mdb

    Dim App_Cnn As adoLOGIN
    Set App_Cnn = New adoLOGIN
    With App_Cnn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("Passwords") = "C:\Documents and Settings\lp630cb\Desktop\Positions.mdb"
        .Properties("Mode") = adModeShareDenyNone
        .Open
    End With

'Use a recordset to read from the table

Dim rst As adoLOGIN.Recorset
Dim strSQL As String
strSQL = "SELECT Password " & _
         "FROM Passwords " & _
         "WHERE Log in ID ='" & strUserID & "'"
Set rst = App_Cnn.Execute(strSQL)
bEnterOk = False
If rst.EOF And rst.BOF Then
   MsgBox "No such user"
Else
    If Trim(UCase(strPassword)) = UCase(rst.Fields(0)) Then
        MsgBox "Welcome my dear user"
        bEnterOk = True
    Else
        MsgBox "Wrong Password"
    End If
End If
EnterOk = bEnterOk
End Function
[s]End Sub[/s]

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Andrzejek

UCase: Depends on how strong security you want to enforce.

-------------
BSG75

These are the definitions needed for the code I posted!
Code:
Dim App_Cnn As [b]ADODB[/b].Connection
Dim rst As [b]ADODB[/b]ADODB.Recorset
Plus
Code:
strSQL = "SELECT Password " & _
         "FROM Passwords " & _
         "WHERE [b][[/b]Log in ID[b]][/b]='" & strUserID & "'"


Although you can use spaces or other non-letter characters as a field name or an object name (ie table) but it is very often suggested, to avoid such a name. Instead, use the underscore _ for a space or something like this LogInID. Otherwise you need to include the name in [ & ]
 
<Dim rst As adoLOGIN.Recorset

I'm unfamiliar with a component calle adoLOGIN. Furthermore, I doubt very much it exposes an object called "Recorset".

Bob
 
the adologin is the adodc that i created to connect to the mircosoft access database. i named it adologin.
 
Oh, I see. Thanks. A few things, then:

If you are going to use the ADODC, then you're going to a lot of unnecessary trouble with your code.

From simplest to most complex:

1. You can just set all of the properties at design time.
2. You can directly reference the adoLOGIN control at runtime.
3. You can do as you have done: create an object that references your adoLOGIN control and set properties via the object.

I don't understand why you are doing this. As nearly as I can tell, you only need to do number 1.

Now, what Jerry's suggesting is that you not use the Data Control, but rather the ADO object library. This is considered better practice for the most part. I think you're confusing jerry's code with code that's necessary for the data control.

I would suggest that you dump your object references (app_cnn, rst), set all of your properties on the data control at design time, then do what Golom suggests.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top