MEMBUAT PROGRAM FORM MAHASISWA
(VB + MySQL)
Nah, saat ini saya menjelaskan tentang
langkah-langkah membuat program yang berhubungan dengan nilai mahasiswa. Disini
saya menggunakan wamp5 dan MySQL ODBC 3.51. Bagi yang belum ada, silakan di
install dulu yah..bagi yang udah ada ayo belajar mencobanya.
Berikut ikuti ya…..
1.
Buat
dulu database dengan 3 tabel yaitu table biodata, table matakuliah, dan table
nilai. Masing-masing table terdiri dari 3 kolom.
Pada table biodat entry kan sbb dan
jadikan nim sbg primary key:
Field
|
Type
|
Nim
|
Varchar(20)
|
Nama
|
Varchar(30)
|
Alamat
|
Varchar(50)
|
Pada table matakuliah entry kan sbb dan
jadikan kodemtk sbg primary key:
Field
|
Type
|
Kodemtk
|
Varchar(10)
|
Namamtk
|
Varchar(20)
|
Jumsks
|
Int(11)
|
Pada table nilai entry kan sbb dan jadikan
nim serta kodemtk sbg primary key:
Field
|
Type
|
Nim
|
Varchar(20)
|
Kodemtk
|
Varchar(20)
|
Nilai
|
Int(11)
|
2. Koneksikan database tsb melalui connector ODBC
3,51. Langkah2nya :
Þ Control panel > system and security
> administrative tool > data source (ODBC) > add > MySQL ODBC 3.51
Driver > finish.
Þ Kemudian isi DSN nya terserah yang
penting di ingat eaaahhh
Þ Servernya isi dengan localhost
Þ Usernya isi dengan root
Þ Pilih apa nama database anda buat tadi.
3. Silakan dibuka Microsoft Visual Basic
nya. Open 1 form dan 1 module. Pada form di buat 4 buah label, 4 textbox, 6
buah commondbuttom, 1 buah datagrid, dan 1 buah adodc. Seperti pada table
dibawah ini:
Tool
|
caption
|
name
|
text
|
Textbox
|
|
txtnim
|
dikosongkan
|
Textbox
|
|
txtnama
|
dikosongkan
|
Textbox
|
|
txtalamat
|
dikosongkan
|
Textbox
|
|
txtfind
|
dikosongkan
|
Label
|
NIM
|
|
|
Label
|
NAMA
|
|
|
Label
|
ALAMAT
|
|
|
Label
|
BIODATA MAHASISWA
|
|
|
commondbuttom
|
FIND
|
cmdfind
|
|
commondbuttom
|
SAVE
|
cmdsave
|
|
commondbuttom
|
EDIT
|
cmdedit
|
|
commondbuttom
|
EXIT
|
cmdexit
|
|
commondbuttom
|
DELETE
|
cmddelete
|
|
4. Pada module di ketikan ini yaaa….
Public conn As ADODB.Connection
Public rsbiodata As ADODB.Recordset
Public rsmatakuliah As ADODB.Recordset
Public rsnilai As ADODB.Recordset
Public sqlstr As String
Sub bukakoneksi()
Set conn = New ADODB.Connection
If conn.State = 1 Then conn.Close
conn.Open
"Provider=MSDASQL.1;Persist Security Info=False;Data Source=biodatku"
End Sub
Sub bukarecordset(sqlstr As String)
If rsbiodata.State = 1 Then
rsbiodata.Close
rsbiodata.Open sqlstr, con,
adOpenKeyset, adLockOptimistic, adCmdText
If rsmatakuliah.State = 1 Then
rsmatakuliah.Close
rsmatakuliah.Open sqlstr, conn,
adOpenKeyset, adLockOptimistic, adCmdText
If rsnilai.State = 1 Then rsnilai.Close
rsnilai.Open sqlstr, conn,
adOpenKeyset, adLockOptimistic, adCmdText
End Sub
Sub koneksisql(sqlstr As String)
conn.Execute sqlstr
End Sub
5. Pada form codingnya sbb:
Private Sub cmdcancel_Click()
bersih
End Sub
Private Sub cmddelete_Click()
X = MsgBox("Yakin Data di
Hapus?", vbYesNo, "Hapus Data")
If X = vbYes Then
koneksisql "delete from biodata
where nim='" & txtnim.Text & "'"
bersih
tampildata
Else
Exit Sub
bersih
End If
End Sub
Private Sub cmdedit_Click()
X = MsgBox("Yakin Data di
Edit..?", vbYesNo, "Edit Data")
If X = vbYes Then
koneksisql "update biodata set
nim='" & txtnim.Text & "',nama='" & txtnama.Text
& "',alamat='" & txtalamat.Text & "' where
nim='" & txtnim.Text & "'"
tampildata
bersih
End If
End Sub
Private Sub cmdfind_Click()
sqlstr = "select * from biodata
where nim='" & LTrim(txtfind.Text) & "'"
Set rsbiodata = conn.Execute(sqlstr)
If rsbiodata.EOF Then
MsgBox "Maaf Data Tidak Ada...!"
bersih
Else
txtnim.Text = rsbiodata!nim
txtnama.Text = rsbiodata!nama
txtalamat.Text = rsbiodata!alamat
End If
End Sub
Private Sub cmdsave_Click()
X = MsgBox("Yakin Data Akan di
Simpan?", vbYesNo, "Save Data")
If X = vbYes Then
koneksisql "insert into biodata
values('" & txtnim.Text & "','" & txtnama.Text &
"','" & txtalamat.Text & "')"
tampildata
bersih
Else
bersih
End If
End Sub
Sub bersih()
txtnim.Text = ""
txtnama.Text = ""
txtalamat.Text = ""
End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub
Private Sub DataGrid1_Click()
txtnim.Text = DataGrid1.Columns(0)
txtnama.Text = DataGrid1.Columns(1)
txtalamat.Text = DataGrid1.Columns(2)
End Sub
Private Sub Form_Load()
bukakoneksi
tampildata
End Sub
Sub tampildata()
conn.CursorLocation = adUseClient
sqlstr = "select * from
biodata"
Set rsbiodata = conn.Execute(sqlstr)
Set DataGrid1.DataSource = rsbiodata
End Sub
JANGAN LUPA MENYIMPANNYA YA…..
6. Ulangi lagi langkah 3 tadi untuk
membuat tampilan form matakuliah…
7. Pada form code nya ketikan sbb:
Private Sub cmdcancel_Click()
bersih
End Sub
Private Sub cmddelete_Click()
X = MsgBox("Yakin Data di
Hapus?", vbYesNo, "Hapus Data")
If X = vbYes Then
koneksisql "delete from matakuliah
where kodemtk='" & txtkode.Text & "'"
bersih
tampildata
Else
Exit Sub
bersih
End If
End Sub
Private Sub cmdedit_Click()
X = MsgBox("Yakin Data di
Edit..?", vbYesNo, "Edit Data")
If X = vbYes Then
koneksisql "update matakuliah set kodemtk='"
& txtkode.Text & "',namamtk='" & txtnama.Text &
"',jumsks='" & txtjumsks.Text & "' where kodemtk='"
& txtkode.Text & "'"
tampildata
bersih
End If
End Sub
Private Sub cmdfind_Click()
sqlstr = "select * from matakuliah
where kodemtk='" & LTrim(txtfind.Text) & "'"
Set rsmatakuliah = conn.Execute(sqlstr)
If rsmatakuliah.EOF Then
MsgBox "Maaf Data Tidak Ado...!"
bersih
Else
txtkode.Text = rsmatakuliah!kodemtk
txtnama.Text = rsmatakuliah!namamtk
txtjumsks.Text = rsmatakuliah!jumsks
End If
End Sub
Private Sub cmdsave_Click()
X = MsgBox("Yakin Data Akan di
Simpan?", vbYesNo, "Save Data")
If X = vbYes Then
koneksisql "insert into matakuliah
values('" & txtkode.Text & "','" & txtnama.Text
& "','" & txtjumsks.Text & "')"
tampildata
bersih
Else
bersih
End If
End Sub
Sub bersih()
txtkode.Text = ""
txtnama.Text = ""
txtjumsks.Text = ""
End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub
Private Sub DataGrid1_Click()
txtkode.Text = DataGrid1.Columns(0)
txtnama.Text = DataGrid1.Columns(1)
txtjumsks.Text = DataGrid1.Columns(2)
End Sub
Private Sub Form_Load()
bukakoneksi
tampildata
End Sub
Sub tampildata()
conn.CursorLocation = adUseClient
sqlstr = "select * from
matakuliah"
Set rsmatakuliah = conn.Execute(sqlstr)
Set DataGrid1.DataSource = rsmatakuliah
End Sub
JANGAN LUPA DISIMPAN LAGIIIII
YA…..
Selanjutnya
kita mau buat form ketiga dengan nama NILAI MAHASISWA,dimana form ini merupakan
gabungan dari kedua form sebelumnya kira2 bentuknya sprti ne dech…
8.
Pada
form code nya coba dech diketikan j yang ini:
Private
Sub cbokode_Click()
sqlstr
= "select * from matakuliah where kodemtk='" & cbokode.Text &
"'"
Set
rsmatakuliah = conn.Execute(sqlstr)
If
Not rsmatakuliah.EOF Then
txtnamamtk.Text = rsmatakuliah!namamtk
txtjumsks.Text = rsmatakuliah!jumsks
End
If
End
Sub
Private
Sub cbonim_Click()
sqlstr
= "select * from biodata where nim='" & cbonim.Text &
"'"
Set
rsbiodata = conn.Execute(sqlstr)
If
Not rsbiodata.EOF Then
txtnama.Text = rsbiodata!nama
txtalamat.Text = rsbiodata!alamat
End
If
txtangka.SetFocus
End
Sub
Private
Sub cmdcancel_Click()
bersih
End
Sub
Private
Sub cmddelete_Click()
X
= MsgBox("Yakin Data di Hapus?", vbYesNo, "Hapus Data")
If
X = vbYes Then
koneksisql
"delete from nilai where nim='" & cbonim.Text & "' and kodemtk='"
& cbomtk.Text & "'"
bersih
tampildata
Else
Exit
Sub
bersih
End
If
End
Sub
Private
Sub cmdedit_Click()
X
= MsgBox("Yakin Data di Edit..?", vbYesNo, "Edit Data")
If
X = vbYes Then
koneksisql
"update nilai set nim='" & cbonim.Text &
"',kodemtk='" & cbomtk.Text & "',nilai='" &
txtangka.Text & "' where nim='" & cbonim.Text & "'
and kodemtk='" & cbokode.Text & "'"
tampildata
bersih
End
If
End
Sub
Private
Sub cmdfind_Click()
sqlstr
= "select * from nilai where nim='" & cbonim.Text & "'
and kodemtk='" & cbokode.Text & "'"
Set
rsnilai = conn.Execute(sqlstr)
If
Not rsnilai.EOF Then
cbonim.Text = rsnilai!nim
cbokode.Text = rsnilai!kodemtk
txtangka.Text = rsnilai!nilai
Else
MsgBox "Data Tidak Ada...!"
bersih
End
If
End
Sub
Private
Sub cmdsave_Click()
X
= MsgBox("Yakin Data Akan di Simpan?", vbYesNo, "Save
Data")
If
X = vbYes Then
koneksisql
"insert into nilai values('" & cbonim.Text & "','"
& cbokode.Text & "','" & Val(txtangka.Text) &
"')"
tampildata
bersih
Else
bersih
End
If
End
Sub
Private
Sub cmdexit_Click()
Unload
Me
End
Sub
Private
Sub Form_Load()
bukakoneksi
isicbonim
isicbokode
tampildata
End
Sub
Sub
bersih()
cbonim.Text
= ""
txtnama.Text
= ""
txtalamat.Text
= ""
cbokode.Text
= ""
txtnamamtk.Text
= ""
txtjumsks.Text
= ""
txtangka.Text
= ""
txthuruf.Text
= ""
txtket.Text
= ""
End
Sub
Sub
tampildata()
conn.CursorLocation
= adUseClient
sqlstr
= "select * from nilai"
Set
rsnilai = conn.Execute(sqlstr)
Set
DataGrid1.DataSource = rsnilai
End
Sub
Sub
isicbonim()
sqlstr
= "select * from biodata"
Set
rsbiodata = conn.Execute(sqlstr)
If
Not rsbiodata.EOF Then 'jika ada
rsbiodata.MoveFirst
While Not rsbiodata.EOF
cbonim.AddItem rsbiodata!nim
rsbiodata.MoveNext
Wend
End
If
End
Sub
Sub
isicbokode()
sqlstr
= "select * from matakuliah"
Set
rsmatakuliah = conn.Execute(sqlstr)
If
Not rsmatakuliah.EOF Then
rsmatakuliah.MoveFirst
While Not rsmatakuliah.EOF
cbokode.AddItem rsmatakuliah!kodemtk
rsmatakuliah.MoveNext
Wend
End
If
End
Sub
Private
Sub txtangka_Change()
If
Val(txtangka.Text) >= 80 Then
txthuruf.Text = "A"
txtket.Text = "Lulus"
ElseIf
Val(txtangka.Text) >= 70 Then
txthuruf.Text = "B"
txtket.Text = "Lulus"
Else
txthuruf.Text = "C"
txtket.Text = "Gagal"
End
If
End
Sub
Hmm…. Sekian dulu panduannya
ea..kapan-kapan ditambah lagi dech..
Mudah2an berhasil ngerjain program yang
satu ini ya…Amiieennnnn.
0 comments:
Post a Comment