Computer Programming web Web programming Tips



Visual Basic Password Keeper Application. Part 2 - Complete VB code

By Sergey Skudaev

This page continues a Visual Basic Password Keeper Application tutorial.

<<Part 1<<

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

VB text area set its MultiLine property to true

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 then use the combobox selected value.
'Or use a 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 the recordset again and populate the combobox with the 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 a 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 (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!

Doanload the last version of VB Application Visual Basic Programming By Examples


You can find resources on Internet to start learning VB. Visit link http://www.appdev.com/visual_basic_training.asp

Start Visual Basic: Part I

My eBooks on Amazon.com



Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
What is the day after Friday?
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics