Rabu, 08 Juni 2011

Buat ApliKAsi DG VB.Net

Kali ini kita akn membuat sebuah aplikasi input data, dimana aplikasi ini memakai sistem Login untuk masuk.
Langsung aja, pertama buat database sebagai penympan data, karena program ini sangat bergantung pada database, sangat penting untuk diperhatikan karena tanpa database pogram ini tidak akan jalan.

Langkap pertama buat database, sesuai isi Foem, pada kasus ini saya membuat database databarang dengan field yang disesuaikan oleh form. Saya menggunakan Visual Studio 2008, dan SQL Server 2008.

Pertama buatlah module untuk koneksi antara VB dan Database dengan source code

Imports System.Data
Imports System.Data.Sql

Module Module_UTS
Public database As New OleDb.OleDbConnection
Public tampil2 As New OleDb.OleDbCommand
Public tampilkan As OleDb.OleDbDataReader
Public hasilcek As String

Public Sub bukakoneksi()
database.Close()
Try
database.ConnectionString = "provider=sqloledb.1;integrated security=SSPI;persist security info=false; initial catalog=DataBarang;data source=."
database.Open()
Catch ex As Exception
MessageBox.Show("koneksi gagal")
End Try
End Sub

End Module

Kemudian test apakah koneksi sudah pas. lalu buat form - formny terlebih dahulu, sebagai contoh :



Form ini dibuat dengan source code :

Public Class Data_Supplier
Sub recording()
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = "Insert into supplier (kode_sup,nama_sup,alamat)values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "') "
tampil2.ExecuteNonQuery()
End Sub
Sub kodesama()
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = "select * from supplier where kode_sup='" & TextBox1.Text & "'"
tampilkan = tampil2.ExecuteReader()
If tampilkan.HasRows = True Then
hasilcek = "True"
Else
hasilcek = "False"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "INPUT" Then
Button1.Text = "SAVE"
Button2.Enabled = "false"
Button3.Enabled = "false"
Button4.Text = "BATAL"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
If TextBox3.Text = "" Then Exit Sub
Call kodesama()
If hasilcek = "True" Then
MsgBox("Data yang anda input telah ada, silahkan coba lagi")
Else
Call recording()
End If
Call bersih()
End If
End Sub
Sub normal()
Button1.Text = "INPUT"
Button1.Enabled = "true"
Button2.Text = "EDIT"
Button2.Enabled = "true"
Button3.Text = "HAPUS"
Button3.Enabled = "true"
Button4.Text = "KELUAR"
Button4.Enabled = "true"
End Sub


Sub bersih()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Button4.Text = "KELUAR" Then
Me.Close()
Else
Call bersih()
Call normal()
End If
End Sub
Sub edittable()
If Button2.Text = "EDIT" Then
Button2.Text = "SIMPAN"
Button1.Enabled = "false"
Button3.Enabled = "false"
Button4.Text = "BATAL"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
If TextBox3.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Update supplier set nama_sup='" & TextBox2.Text & "',alamat='" & TextBox3.Text & "' where kode_sup='" & TextBox1.Text & "'"
tampil2.ExecuteNonQuery()
Call bersih()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call edittable()
End Sub
Sub hapusrecord()
If Button3.Text = "HAPUS" Then
TextBox1.Text = " "
Button3.Text = "Ok"
Button1.Enabled = False
Button2.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
If TextBox3.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Delete from supplier where kode_sup='" & Trim(TextBox1.Text) & "'"
tampil2.ExecuteNonQuery()
Call bersih()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call hapusrecord()
End Sub
Private Sub Data_Supplier_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
Call bersih()
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Button1.Text = "INPUT" Then
Call recording()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode Sudah ada, silahkan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Informasi")
tampilkan = tampil2.ExecuteReader
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " select* from supplier where kode_sup='" & Trim(TextBox1.Text) & "'"
tampil2.ExecuteNonQuery()

If tampilkan.HasRows = True Then
While tampilkan.Read()
If (IsDBNull(tampilkan("kode_sup"))) Then
TextBox1.Focus()
Else
TextBox2.Text = tampilkan("nama_sup")
TextBox2.Focus()
End If
End While
Else
MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah Data")
End If
End If
End If
End If
End Sub

End Class



Keseluruhan form sesungguhnya hampir sama source codenya. Nah sekarang lihat pada form Login :




dengan source code :

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If UsernameTextBox.Text = "Rida" And PasswordTextBox.Text = "aku" Then
MDIParent1.Show()
Me.Visible = False
Else
MsgBox("login gagal, silahkan ulangi lagi")
End If
End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub

untuk koneksinya, jangan lupa untuk buat MDParent,



, dengan source code :
Imports System.Windows.Forms

Public Class MDIParent1

Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
' Create a new instance of the child form.
Dim ChildForm As New System.Windows.Forms.Form
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = Me

m_ChildFormNumber += 1
ChildForm.Text = "Window " & m_ChildFormNumber

ChildForm.Show()
End Sub

Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim FileName As String = OpenFileDialog.FileName
' TODO: Add code here to open the file.
End If
End Sub

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
Dim SaveFileDialog As New SaveFileDialog
SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim FileName As String = SaveFileDialog.FileName
' TODO: Add code here to save the current contents of the form to a file.
End If
End Sub


Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub

Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
End Sub

Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
End Sub

Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolBarToolStripMenuItem.Click
Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
End Sub

Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StatusBarToolStripMenuItem.Click
Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
End Sub

Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub

Private Sub TileVerticalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub

Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub

Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.ArrangeIcons)
End Sub

Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
' Close all child forms of the parent.
For Each ChildForm As Form In Me.MdiChildren
ChildForm.Close()
Next
End Sub

Private m_ChildFormNumber As Integer

Private Sub DatabSupplierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatabSupplierToolStripMenuItem.Click
Data_Supplier.Show()
End Sub

Private Sub DataMaterialToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataMaterialToolStripMenuItem.Click
Data_Material.Show()
End Sub

Private Sub DataPetugasToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataPetugasToolStripMenuItem.Click
Data_Petugas.Show()
End Sub

Private Sub DataPengecekanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataPengecekanToolStripMenuItem.Click
Data_Pengecekan.Show()
End Sub
End Class


Nah untuk form yang lain sejujurnya g jauh beda k..............^_^

Tidak ada komentar:

Posting Komentar