Sebelumnya saya telah membuat tutorial Cara Membuat Form Login, namun form login sebelumnya (versi 1) tidak menggunakan database, sehingga data login seperti username dan password, tidak bisa di edit ketika program di jalankan atau tidak bisa di ganti, kali ini kita akan membuat sebuah Form Login dengan menggunakan database, database yang saya gunakan di sini adalah Notepad, baiklah berikut tutorialnya :
1. Jalankan VB dengan project Standar Exe
Cara Menambahkan Module
2. Sebelum ke form, tambahkan terlebih dahulu module caranya :
Klik Menu Project > Add Module > Pilih Module > Klik Open
lalu tuliskan script berikut :
1. Jalankan VB dengan project Standar Exe
Cara Menambahkan Module
2. Sebelum ke form, tambahkan terlebih dahulu module caranya :
Klik Menu Project > Add Module > Pilih Module > Klik Open
lalu tuliskan script berikut :
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function WriteIni(KeySection As String, KeyKey As String, KeyValue As String) As Boolean
Dim lngResult As Long, strFileName As String, AppPath As String
AppPath = App.Path
If Right(AppPath, 1) = "\" Then
strFileName = AppPath & "\bluespedia.txt"
Else
strFileName = AppPath & "\bluespedia.txt"
End If
lngResult = WritePrivateProfileString(KeySection, KeyKey, KeyValue, strFileName)
If lngResult = 0 Then
WriteIni = False
Else
WriteIni = True
End If
End Function
Public Function ReadIni(KeySection As String, KeyKey As String) As String
Dim lngResult As Long
Dim strFileName
Dim strResult As String * 50
AppPath = App.Path
If Right(AppPath, 1) = "\" Then
strFileName = AppPath & "\bluespedia.txt"
Else
strFileName = AppPath & "\bluespedia.txt"
End If
lngResult = GetPrivateProfileString(KeySection, KeyKey, strFileName, strResult, Len(strResult), strFileName)
If lngResult = 0 Then
ReadIni = "error"
Else
ReadIni = Left(strResult, lngResult)
End If
End Function
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function WriteIni(KeySection As String, KeyKey As String, KeyValue As String) As Boolean
Dim lngResult As Long, strFileName As String, AppPath As String
AppPath = App.Path
If Right(AppPath, 1) = "\" Then
strFileName = AppPath & "\bluespedia.txt"
Else
strFileName = AppPath & "\bluespedia.txt"
End If
lngResult = WritePrivateProfileString(KeySection, KeyKey, KeyValue, strFileName)
If lngResult = 0 Then
WriteIni = False
Else
WriteIni = True
End If
End Function
Public Function ReadIni(KeySection As String, KeyKey As String) As String
Dim lngResult As Long
Dim strFileName
Dim strResult As String * 50
AppPath = App.Path
If Right(AppPath, 1) = "\" Then
strFileName = AppPath & "\bluespedia.txt"
Else
strFileName = AppPath & "\bluespedia.txt"
End If
lngResult = GetPrivateProfileString(KeySection, KeyKey, strFileName, strResult, Len(strResult), strFileName)
If lngResult = 0 Then
ReadIni = "error"
Else
ReadIni = Left(strResult, lngResult)
End If
End Function
Di program ini saya menggunakan 2 form, jika anda belum tahu cara membuat 2 form / menambahkan form, anda bisa membacanya disini :3. Untuk Form1, Tambahkan 2 label, 2 text box dan 2 command button, atur tampilan dan lebarnya sehingga seperti ini :
Membuat Program Menggunakan Lebih dari 1 Form
Tool Box | Properties | Description |
Form1 | Start Up | Center Screen |
Label 1 | Caption | Username |
Label 2 | Password | |
Textbox 1 | Text | (Kosong) |
Textbox 2 | (Kosong) | |
Command 1 | Caption | LOGIN |
Command 2 | END | |
All | Font Size | 10 |
dan untuk form 2 sama dengan form 1, tambahkan 2 label, 2 text box, 2 command button, atur seperti ini :
Tool Box | Properties | Description |
Form1 | Start Up | Center Screen |
Label 1 | Caption | CHECK DATA |
Label 2 | TRY LOGIN | |
Textbox 1 | Text | (Kosong) |
Textbox 2 | (Kosong) | |
Command 1 | Caption | Login |
Command 2 | End | |
All | Font Size | 10 |
4. Jika sudah tuliskan script berikut untuk form1
Private Sub Command2_Click()
'Script untuk menghentikan program
End
End Sub
Private Sub Command1_Click()
'Proses yang akan terjadi jika data login di database sama dengan data yang di input
If Text1.Text = ReadIni("DATA", "Username") And Text2.Text = ReadIni("DATA", "Password") Then
form1.Hide
form2.Show
form2.Text1.Enabled = False
form2.Text2.Enabled = False
form2.Text1 = ""
form2.Text2 = ""Command1.Caption = "CHECK DATA"
'Pesan jika text1 dan 2 kosong ketika command1 "Login" di klik
ElseIf Text1.Text = "" Or Text2.Text = "" Then
MsgBox "Please, Complete The Form", vbInformation, "Blues Pedia"
'Pesan jika salah memasukan data login
Else
MsgBox "Username or Password Invalid, Please Try Again", vbCritical, "Blues Pedia"
Call Form_Load
End If
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
'Script untuk menghentikan program
End
End Sub
Private Sub Command1_Click()
'Proses yang akan terjadi jika data login di database sama dengan data yang di input
If Text1.Text = ReadIni("DATA", "Username") And Text2.Text = ReadIni("DATA", "Password") Then
form1.Hide
form2.Show
form2.Text1.Enabled = False
form2.Text2.Enabled = False
form2.Text1 = ""
form2.Text2 = ""Command1.Caption = "CHECK DATA"
'Pesan jika text1 dan 2 kosong ketika command1 "Login" di klik
ElseIf Text1.Text = "" Or Text2.Text = "" Then
MsgBox "Please, Complete The Form", vbInformation, "Blues Pedia"
'Pesan jika salah memasukan data login
Else
MsgBox "Username or Password Invalid, Please Try Again", vbCritical, "Blues Pedia"
Call Form_Load
End If
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
dan tuliskan script berikut di form2
[DATA]
Username=Blues Pedia
Password=qwerty
Ganti kata Blues Pedia dengan kata untuk login, dan qwerty untuk password
save file
6. Jalankan program
Sekian tutorial kali ini semoga bermanfaat
Private Sub Command1_Click()
'Jika caption command1 "CHECK DATA" maka
If Command1.Caption = "CHECK DATA" Then
Text1.Text = ReadIni("DATA", "Username")
Text2.Text = ReadIni("DATA", "Password")
Command1.Caption = "EDIT"
'Jika caption command1 "EDIT" maka
ElseIf Command1.Caption = "EDIT" Then
Text1 = ""
Text2 = ""
Text1.Enabled = True
Text1.SetFocus
Text2.Enabled = True
Command1.Caption = "SAVE"
'Jika caption command1 "SAVE" maka
ElseIf Command1.Caption = "SAVE" Then
'Pesan Jika text1 dan text2 dalam keadaan kosong ketika di klik command1
If Text1 = "" Or Text2 = "" Then
MsgBox "Please, Complete The Form !", vbInformation, "BLUES PEDIA"
'Jika tidak kosong, maka program akan menyimpan data login
Else
Text1.Text = WriteIni("DATA", "Username", Text1.Text)
Text2.Text = WriteIni("DATA", "Password", Text2.Text)
MsgBox "Data Successfully Changed", vbInformation, "BLUES PEDIA"
Call Form_Load
End If
End If
End Sub
Private Sub Command2_Click()
form2.Hide
form1.Show
form1.Text1 = ""
form1.Text2 = ""
form1.Text1.SetFocus
End Sub
Private Sub Form_Load()
Text1.Enabled = False
Text2.Enabled = False
Text1 = ""
Text2 = ""
Command1.Caption = "CHECK DATA"
End Sub
5. Save program, lalu buatlah sebuah file txt / notepad dengan nama bluespedia.txt, dan satukan letaknya dengan letak program login ini di simpan, dan tuliskan di file bluespedia.txt'Jika caption command1 "CHECK DATA" maka
If Command1.Caption = "CHECK DATA" Then
Text1.Text = ReadIni("DATA", "Username")
Text2.Text = ReadIni("DATA", "Password")
Command1.Caption = "EDIT"
'Jika caption command1 "EDIT" maka
ElseIf Command1.Caption = "EDIT" Then
Text1 = ""
Text2 = ""
Text1.Enabled = True
Text1.SetFocus
Text2.Enabled = True
Command1.Caption = "SAVE"
'Jika caption command1 "SAVE" maka
ElseIf Command1.Caption = "SAVE" Then
'Pesan Jika text1 dan text2 dalam keadaan kosong ketika di klik command1
If Text1 = "" Or Text2 = "" Then
MsgBox "Please, Complete The Form !", vbInformation, "BLUES PEDIA"
'Jika tidak kosong, maka program akan menyimpan data login
Else
Text1.Text = WriteIni("DATA", "Username", Text1.Text)
Text2.Text = WriteIni("DATA", "Password", Text2.Text)
MsgBox "Data Successfully Changed", vbInformation, "BLUES PEDIA"
Call Form_Load
End If
End If
End Sub
Private Sub Command2_Click()
form2.Hide
form1.Show
form1.Text1 = ""
form1.Text2 = ""
form1.Text1.SetFocus
End Sub
Private Sub Form_Load()
Text1.Enabled = False
Text2.Enabled = False
Text1 = ""
Text2 = ""
Command1.Caption = "CHECK DATA"
End Sub
[DATA]
Username=Blues Pedia
Password=qwerty
Ganti kata Blues Pedia dengan kata untuk login, dan qwerty untuk password
save file
6. Jalankan program
Sekian tutorial kali ini semoga bermanfaat
0 Response to "Membuat Form Login Menggunakan Data Base + Module"
Post a Comment