Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

visual basic code

The document provides a step-by-step guide for creating a user credentials file and a login form in VB6, including code for login authentication and exit functionality. It also details how to create a text file as a database for student records, along with a registration form and code for inserting, searching, updating, and deleting student records. The approach utilizes text file operations for managing user and student data.

Uploaded by

Virendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

visual basic code

The document provides a step-by-step guide for creating a user credentials file and a login form in VB6, including code for login authentication and exit functionality. It also details how to create a text file as a database for student records, along with a registration form and code for inserting, searching, updating, and deleting student records. The approach utilizes text file operations for managing user and student data.

Uploaded by

Virendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Create a User Credentials File (users.

txt)
1. Open Notepad.

2. Save the file as users.txt in the same folder as your VB6 project.

3. Add login credentials in this format:

admin,12345

user1,password

Each line contains a username and password, separated by a comma (,).

2. Create the Login Form (frmLogin)


Form Design

Textboxes: txtUsername, txtPassword (Set txtPassword.PasswordChar = "*")

Buttons: cmdLogin, cmdExit

Labels: "Username:", "Password:"

Form Name: frmLogin


---

3. Add Code for Login Authentication

Private Sub cmdLogin_Click()

Dim fileNum As Integer

Dim lineData As String

Dim userInfo() As String

Dim loginSuccess As Boolean

fileNum = FreeFile

Open App.Path & "\users.txt" For Input As #fileNum

loginSuccess = False

' Read file line by line

Do While Not EOF(fileNum)

Line Input #fileNum, lineData


userInfo = Split(lineData, ",") ' Split by comma

' Check if username and password match

If Trim(userInfo(0)) = Trim(txtUsername.Text) And Trim(userInfo(1)) =


Trim(txtPassword.Text) Then

loginSuccess = True

Exit Do

End If

Loop

Close #fileNum

If loginSuccess Then

MsgBox "Login Successful!", vbInformation, "Success"

frmLogin.Hide

frmRegister.Show ' Open the Student Registration Form

Else

MsgBox "Invalid Username or Password!", vbCritical, "Login Failed"

End If

End Sub

This code checks the entered credentials against the records in users.txt.

---

4. Exit Button Code

Private Sub cmdExit_Click()


Unload Me

End Sub

This closes the login form.

we will use text files (.txt) as a database. This method reads, writes, updates, and deletes
records using text file operations.

---

1. Create a Text File as a Database

1. Open Notepad.

2. Save the file as students.txt in the same folder as your VB6 project.

3. Use a format like this for storing student records:

S001,John Doe,20,Computer Science

S002,Jane Smith,22,Mathematics

Each line represents a student's record, with fields separated by commas (,).

2. Create the VB6 Registration Form (frmRegister)


Form Design

Textboxes: txtID, txtName, txtAge, txtCourse

Buttons: cmdInsert, cmdUpdate, cmdDelete, cmdSearch

Labels: "ID:", "Name:", "Age:", "Course:"


---

3. Add Code for File-Based Operations


(a) Insert New Student Record

Private Sub cmdInsert_Click()

Dim fileNum As Integer

Dim studentData As String

' Open file for appending new records

fileNum = FreeFile

Open App.Path & "\students.txt" For Append As #fileNum

' Create a record in CSV format

studentData = txtID.Text & "," & txtName.Text & "," & txtAge.Text & "," & txtCourse.Text

' Write data to file

Print #fileNum, studentData

Close #fileNum
MsgBox "Record Inserted Successfully!", vbInformation, "Success"

' Clear textboxes

txtID.Text = ""

txtName.Text = ""

txtAge.Text = ""

txtCourse.Text = ""

End Sub

This appends a new student record to students.txt.

(b) Search for a Student Record

Private Sub cmdSearch_Click()

Dim fileNum As Integer

Dim lineData As String

Dim studentInfo() As String

Dim found As Boolean

fileNum = FreeFile

Open App.Path & "\students.txt" For Input As #fileNum

found = False

' Read file line by line

Do While Not EOF(fileNum)


Line Input #fileNum, lineData

studentInfo = Split(lineData, ",") ' Split by comma

If studentInfo(0) = txtID.Text Then

txtName.Text = studentInfo(1)

txtAge.Text = studentInfo(2)

txtCourse.Text = studentInfo(3)

found = True

Exit Do

End If

Loop

Close #fileNum

If Not found Then

MsgBox "Student Not Found!", vbExclamation, "Search"

End If

End Sub

This searches for a student by ID.

---

(c) Update Student Record

Private Sub cmdUpdate_Click()

Dim fileNum As Integer, tempFile As Integer

Dim lineData As String


Dim studentInfo() As String

Dim updated As Boolean

fileNum = FreeFile

tempFile = FreeFile + 1

Open App.Path & "\students.txt" For Input As #fileNum

Open App.Path & "\temp.txt" For Output As #tempFile

updated = False

Do While Not EOF(fileNum)

Line Input #fileNum, lineData

studentInfo = Split(lineData, ",")

If studentInfo(0) = txtID.Text Then

' Write updated data

Print #tempFile, txtID.Text & "," & txtName.Text & "," & txtAge.Text & "," &
txtCourse.Text

updated = True

Else

' Write original data

Print #tempFile, lineData

End If

Loop

Close #fileNum

Close #tempFile
If updated Then

Kill App.Path & "\students.txt"

Name App.Path & "\temp.txt" As App.Path & "\students.txt"

MsgBox "Record Updated Successfully!", vbInformation, "Success"

Else

Kill App.Path & "\temp.txt"

MsgBox "Student Not Found!", vbExclamation, "Update"

End If

End Sub

This updates a student record by rewriting the file.

(d) Delete Student Record

Private Sub cmdDelete_Click()

Dim fileNum As Integer, tempFile As Integer

Dim lineData As String

Dim studentInfo() As String

Dim deleted As Boolean

fileNum = FreeFile

tempFile = FreeFile + 1

Open App.Path & "\students.txt" For Input As #fileNum

Open App.Path & "\temp.txt" For Output As #tempFile


deleted = False

Do While Not EOF(fileNum)

Line Input #fileNum, lineData

studentInfo = Split(lineData, ",")

If studentInfo(0) = txtID.Text Then

deleted = True ' Skip writing this record (delete)

Else

Print #tempFile, lineData

End If

Loop

Close #fileNum

Close #tempFile

If deleted Then

Kill App.Path & "\students.txt"

Name App.Path & "\temp.txt" As App.Path & "\students.txt"

MsgBox "Record Deleted Successfully!", vbInformation, "Success"

Else

Kill App.Path & "\temp.txt"

MsgBox "Student Not Found!", vbExclamation, "Delete"

End If

End Sub

This removes a student record.

You might also like