Up to 87% savings on Geeks.com!

SECRET CAMCORDER Prices too low to show! Expires 8/31
This sale includes 22 camcorders from Sony, JVC, Samsung, and Canon!

Take 10% Off All MCM Electronics Purcases!

HP Store search box: Computers, Electronics, Hardware

Search:

Password Keeper. Visual Basic 6.0 - Complete VB code

   

By Sergey Skudaev

VB password keeper is a Visual Basic version of Password Keeper application which was implemented earlier in MS ACESS. here you will find a complete VB code.

Please read Start Visual Basic tutorial before creating Password Keeper Application in VB.

In MS Access create mypassword.mdb database with one table passwords. Save it in VB directory, VBPassword subdirectory. Please see tutorial how to create database in MS Access

Visual Basic Code example

Figure 1. Passwords table fields

Create VB project with two forms and one module. Enter WINMAIN name for module, frmMain name for main form and frmLogin name for login form.Save it in VB directory, VBPassword subdirectory.

Select Project, References from main menu and mark Microsoft ActiveX Data Objects library

Visual Basic, Project, References windows

Figure 2. Project. References.

Click OK button.

Select Project, Project Properties, General tab and enter project name "Password Keeper". Set Startup Object to Sub Main.

Visual Basic Code example Select Project, Project Properties

Figure 3. Project. Properties.

Click OK button

Select Tools, Options, Editor tab and mark all check boxes

Visual Basic Code example Tools, Options

Figure 4. Tools. Options.

Click OK button

Select frmLogin form. On proporties window change form caption to Login. Select icon and click a button next to it. Go to "Common\Graphics\Icons\Misc" folder and select a key or lock icon. It will display on the form title bat.

Place on frmLogin form two text controls and one command button control. In properties window enter txtLogin name for the first text control and txtPassword name for the second text control. Visual Basic, Properties window

Select text proporty and delete default "text1" text in both text controls.

Enter cmdLogin name for command button. Enter Login caption for command button.

Place on the form three label controls: one for title "Password Keeper" and two for text field names. Change one label caption to "Login", another to "Password".

Visual Basic Login example

Figure 5. frmLogin form with controls.

Select WINMAIN module click Tools, Add Procedure and create public main subroutine.

VB Add Procedure and create public main subroutine

Figure 6. Sub Main procedure

Add public centre sub. Enter the following code in WINMAIN procedures:

'Declare global ADODB connection object
Public Conn As ADODB.Connection

Public Sub main()

Dim strConn As String

Set Conn = New ADODB.Connection

'Connect to database. One connection object will be used for all connections
Conn.Provider = "Microsoft.Jet.Oledb.4.0"
Conn.Open App.Path & "\mypasswords.mdb"

'Display frmLofin form
frmLogin.Show

End Sub

Public Sub cetre(myForm As Form)

'Place form in center of screen
myForm.Left = (Screen.Width - myForm.Width) / 2
myForm.Top = (Screen.Height - myForm.Height) / 2

End Sub

Select frmLogin form in project view and click right mouse button. Select View code from drop down list. Add the following code to the login form:

Private try As Integer

Option Explicit

Private Sub cmdLogin_Click()

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim sql As String

sql = "select * from passwords
where target='this' and login='" & txtLogin.Text & "' and password='" & txtPassword.Text & "'"
'MsgBox sql
rs.Open sql, Conn, adOpenKeyset, adLockPessimistic

If rs.BOF And rs.EOF Then
'If record is not found message displays
MsgBox "Incorrect login or password! Please try again.", vbOKOnly, "Password Keeper"
'count number of tries
try = try + 1
'Allows only 3 tries, then application stops
If try >= 3 Then
End
End If

Exit Sub

Else

'If password correct, Main form is loaded and Login form is unloaded
frmmain.Show
Unload frmLogin

End If

End Sub

Private Sub Form_Load()

'initialize try variable when login form is loaded

try = 0

End Sub

Private Sub Form_Activate()

'Place form in center of the screen
cetre Me

End Sub

Create frmMain form and controls. Place on the frmMain form one combobox, four text controls and three command buttons.

Figure 7. frmMain form and controls

Enter cboTarget in combobox name property. Enter txtTarget name for the first text box. Enter txtLogin name for the second text box and txtPassword name for the third text box. Enter txtNote name for text area. Select text area and set its MultiLine property to true.

VB text area set its MultiLine property to true

Figure 8. Text Control MultiLine Property

Set name of the first command button to cmdUpdate and caption to Update.
Set name of the second command button to cmdClear and caption to Clear
Set name of the third command button to cmdSave and caprion to Save. See Figure 7

Select frmMain form and in properties window click button next to icon property. Find any icon you like and insert it in form title bar. Place on the form labels for text field names and for form title. Form caption set to Password Keeper

Enter code in Sub on form activate event. Form activate event occurs when form becomes active. (Its title bar becomes blue.)

Private Sub Form_Activate()

'Place form in center
cetre Me

End Sub

'Declare and open recordset on form load event using connection object created in sub main.

Private Sub Form_Load()

Dim sql As String

Set rsPass = New ADODB.Recordset
sql = "select * from passwords"
rsPass.Open sql, Conn, adOpenKeyset, adLockPessimistic

Do Until rsPass.EOF
'populate combobox with target field values.
cboTarget.AddItem rsPass.Fields("target")
rsPass.MoveNext
Loop

End Sub

When user click combobox button and select a target, login and password for the target shall be edisplayed in login and password text boxes. Notes shall be displayed in notes text area. It can be achieved with the code placed in combobox click event.

Private Sub cboTarget_Click()
'Start from the first record.
rsPass.MoveFirst
'Find a record in the database with selected target value.
rsPass.Find "Target ='" & cboTarget.Text & "'", 0, adSearchForward

If rsPass.EOF Or rsPass.BOF Then
'If record not found message displays.
MsgBox "No record found!", vbOKOnly, "Find Target"
Exit Sub

Else

'If record found display login, password and notes in text fields.
txtLogin.Text = rsPass!login
txtTarget.Text = rsPass!Target
txtPassword.Text = rsPass!password
txtNote.Text = rsPass!note
End If

End Sub

When user wants to edit target, login, password, or notes system shall save in database changes made in these fields by user. Enter the following code in cmdUpdate click event sub

Private Sub cmdUpdate_Click()

Dim sql As String

'If any of the field is empty warning message displays
If txtLogin.Text = "" Or txtPassword.Text = "" Or txtNote.Text = "" Or (txtTarget.Text = "" And cboTarget.Text = "") Then
MsgBox "Please select or enter target, enter login and password", vbOKOnly, "Update"
Exit Sub
End If

'If all fields have data load field values to recordset

rsPass!login = txtLogin.Text
rsPass!password = txtPassword.Text

'If user did not enter a new target in target text field the use combobox selected value.
'Or use new target entered in txtTarget field.

If (txtTarget.Text = "") Then
rsPass!targer = cboTarget.Text
Else
rsPass!Target = txtTarget.Text
End If

rsPass!note = txtNote.Text
rsPass.Update
rsPass.Close

'Open recordset again and populate combobox with updated list
sql = "select * from passwords"
rsPass.Open sql, Conn, adOpenKeyset, adLockPessimistic

Do Until rsPass.EOF
cboTarget.AddItem rsPass.Fields("target")
rsPass.MoveNext
Loop

End Sub

If user wants to add new record, he shall empty all fields. Clear command button is used to do the job. On its click event enter the following code:

Private Sub cmdClear_Click()

txtLogin.Text = ""
txtTarget.Text = ""
txtPassword.Text = ""
txtNote.Text = ""

End Sub

Entered new values are saved in the database using recorset addnew method on click event the Save command button.

Private Sub cmdSave_Click()

'If any of the field is empty warning message displays
If txtLogin.Text = "" Or txtPassword.Text = "" Or txtNote.Text="" Or (txtTarget.Text = "" And cboTarget.Text = "") Then
MsgBox "Please select or enter target, enter login and password", vbOKOnly, "Update"
Exit Sub
End If

'If all fields have data load field values to recordset

rsPass.AddNew
rsPass!login = txtLogin.Text
rsPass!Target = txtTarget.Text
rsPass!password = txtPassword.Text
rsPass!note = txtNote.Text
rsPass.Update
rsPass.Close

'Reopen recordset to populate target combobox with new records
sql = "select * from passwords"
rsPass.Open sql, Conn, adOpenKeyset, adLockPessimistic

Do Until rsPass.EOF
cboTarget.AddItem rsPass.Fields("target")
rsPass.MoveNext
Loop

End Sub

We are done!. If you have any question or comments please post them in my forum

Please rate the tutorial

1 2 3 4 5


Sumo Robot is ready for action.