Rabu, 11 Mei 2011

Koneksi Database

Cara koneksi database visual basic 2010 dengan ms SQL Server 2008
Pertama kita buat modul koneksi, lalu buat tombol untuk test koneksi

Imports System.Data
Imports System.Data.Sql


Module konek
    Public Database As New OleDb.OleDbConnection
    Public Tampil As New OleDb.OleDbCommand
    Public Tampil2 As New OleDb.OleDbCommand
    Public Tampilkan As OleDb.OleDbDataReader
    Public Hasiljur, jur As String




    Public Sub Koneksi()
        Try
            Database.Close()
            Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=ptabc; Data Source=MUJAN-COMP\SQLEXPRESS;"
            Database.Open()
            'Integrated Security=True
            MsgBox("konek sukses")
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub


    Public Sub jurusan()
        Call Koneksi()
        Tampil2.Connection = Database
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from table_1 where kdjur='" + Trim(jur) & "'"
        Tampilkan = Tampil2.ExecuteReader
        If Tampilkan.HasRows = True Then
            While Tampilkan.Read
                If IsDBNull(Tampilkan("kdjur")) Then
                    Hasiljur = "_"
                Else
                    Hasiljur = Tampilkan("namajur")


                End If
            End While
        End If


    End Sub
End Module


'kode program di form



Public Class Form1


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Koneksi()
        jur = TextBox2.Text
        jurusan()
        TextBox1.Text = Hasiljur
    End Sub
End Class


Hasil:

Kamis, 21 April 2011

Latihan

Menampilkan data kedalam Listbox
Hasil output yang diharapkan seperti tampak pada gambar:


Listing programnya:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b, i As Integer

        'bersihkan list box
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()

        'beri nilai default
        b = 0
        'looping dari nilai di textbox1 sampai textbox2
        For i = Val(TextBox1.Text) To Val(TextBox2.Text)
            'untuk kolom A
            'hasil yg diharapkan 7,21,63,189
            'atau dengan kata lain 7 * (3^0),7*(3^1),7*(3^2),7*(3^3),7*(3^4), dst, yang warna merah diganti dengan variable b
'set nilai a
            a = 7 * 3 ^ b
'jika a < i maka naikkan nilai b
'set lagi nilai a
            If a < i Then
                b = b + 1
                a = 7 * 3 ^ b
            End If

            If i Mod (a) = 0 Then
                ListBox1.Items.Add(i)
            End If
            
            'untuk kolom B
            'hasil yang diharapkan 2,12,22,32,42,52
            If (i - 2) Mod 10 = 0 Then
                ListBox2.Items.Add(i)
            End If
        Next

    End Sub
End Class

Jumat, 25 Maret 2011

ListView & Listbox

Buat Program yang outputnya seperti gambar

Listing programnya:

Public Class Form1
    Dim listtxt(3) As String ' pendeklarasian variable 
    Dim listitem As ListViewItem


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'memasukkan nilai ke variabel array
        listtxt(0) = Trim(TextBox1.Text)
        listtxt(1) = Trim(TextBox2.Text)
        listtxt(2) = Trim(TextBox3.Text)
        'utk memasukkan ke listview dari variable array
        listitem = New ListViewItem(listtxt)


        ListView1.Items.Add(listitem)






    End Sub
    'setting kolom listview
    Sub setlistview()
        ListView1.View = View.Details
        'utk menetukan kolom
        ListView1.Columns.Add("NPM", 100, HorizontalAlignment.Center)
        ListView1.Columns.Add("Nama", 200, HorizontalAlignment.Center)
        ListView1.Columns.Add("Alamat", 250, HorizontalAlignment.Center)




    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call setlistview()'memanggil procedure 


    End Sub


    Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        TextBox4.Text = e.Item.Text
        'untuk memasukkan nilai kolom pertama ke dalam texbox4
    End Sub


   
End Class

Listbox
Buat tampilan seperti ini:


souce code:


Public Class Form2
  
    Sub pros3()
        ListBox1.Items.Add(TextBox3.Text + " " + TextBox4.Text + " " + TextBox5.Text + " " + TextBox6.Text + " " + TextBox7.Text)
    End Sub


    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox4.Focus()
        End If
    End Sub


    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox5.Focus()
        End If
    End Sub


    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox6.Focus()
        End If
    End Sub


    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox7.Focus()
        End If
    End Sub


    Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        If e.KeyChar = Chr(13) Then
            Call pros3()
            Call bersihpros()
            TextBox3.Focus()
        End If
    End Sub


    Sub bersihpros()
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
    End Sub


End Class

Minggu, 13 Maret 2011

Membuat Tombol yang berpindah pindah

Kita akan berlatih membuat tombol yang berpindah-pindah..
Misal ada 6 kotakan... kotak 1 sampai lima ada tombolnya yang telah diwarnai... sedang kotak yang ke enam kosong... ketika salah satu dari tombol itu diklik maka tombol akan menempati posisi yang kosong...
misal... tombol satu diklik... maka tombol satu itu akan menempati posisi di kotak no enam, lalu klo tombol kedua diklik maka tombol 2 akan menempati posisi kotak no satu yang kosong.. begitu seterusnya..
Lihat gambar....

Listing programnya:
Public Class Form1
    Dim vButton As System.Windows.Forms.Button
    'buat variabel general utk menyimpan tombol yang invisible, misal vButton
'buat procedure aktif untuk memindah tulisan dan warna tombol ke lokasi yang kosong(tombol yang invisible)

Sub aktif(ByVal b As System.Windows.Forms.Button)
        vButton.Visible = True
        vButton.Text = b.Text
        vButton.BackColor = b.BackColor
        b.Visible = False
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click
        Call aktif(sender)
        vButton = sender
'panggil procedure aktif
'ganti variable vButton
        
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        vButton = Button6
'setting awal tombol 6 di invisible
    End Sub
End Class

cara lain yang lebih panjang tapi secara logika lebih mudah dimengerti:

Public Class Form1
   
    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       'cara 1 masukkan kode ini di setiap tombol, dan ganti tombol yang visible dan invisible
'warna tolbol belum dimasukkan

        'If Button2.Visible = False Then
        '    Button2.Visible = True
        '    Button2.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button3.Visible = False Then
        '    Button3.Visible = True
        '    Button3.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button4.Visible = False Then
        '    Button4.Visible = True
        '    Button4.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button5.Visible = False Then
        '    Button5.Visible = True
        '    Button5.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button6.Visible = False Then
        '    Button6.Visible = True
        '    Button6.Text = Button1.Text
        '    Button1.Visible = False
        'End If
        '===========================sampai sini cara panjang 1;
        'cara 2
        '===========================
        Call aktif(Button1)
        'panggil prosedur aktif, buat disemua tombol seperti ini

    End Sub

'buat prosedure aktif
Sub aktif(ByVal b As System.Windows.Forms.Button)


        If Button1.Visible = False Then
            Button1.Visible = True
            Button1.Text = b.Text
            Button1.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button2.Visible = False Then
            Button2.Visible = True
            Button2.Text = b.Text
            Button2.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button3.Visible = False Then
            Button3.Visible = True
            Button3.Text = b.Text
            Button3.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button4.Visible = False Then
            Button4.Visible = True
            Button4.Text = b.Text
            Button4.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button5.Visible = False Then
            Button5.Visible = True
            Button5.Text = b.Text
            Button5.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button6.Visible = False Then
            Button6.Visible = True
            Button6.Text = b.Text
            Button6.BackColor = b.BackColor
            b.Visible = False
        End If

    End Sub

    '==================== sampai sini cara 2
End Class




Jumat, 04 Maret 2011

Tugas 3 Maret 2011

1. Buatkan program menampilkan bilangan,  yang habis dibagi 7 dan habis dibagi 4 mulai 1-100
2. Tampilkan bilangan yang sisa hasilnya 3 jika pembaginya 4, mulai 1-100
------
Listing programnya:

1.
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        ListBox1.Items.Clear()
        For i = 1 To Val(TextBox1.Text)
            If (i Mod 7 = 0) And (i Mod 4 = 0) Then
                ListBox1.Items.Add(i)
            End If
        Next
    End Sub
End Class

Keterangan
ListBox1.Items.Clear()
ini untuk membersihkan nilai yang ada di ListBox1
For i = 1 To Val(TextBox1.Text) 
ini untuk loopingdari 1 sampai nilai di textbox1

   If (i Mod 7 = 0) And (i Mod 4 = 0) Then
                ListBox1.Items.Add(i)
            End If

Jika i dibagi 7 sisanya nol dan i dibagi 4 sisanya nol maka tambahkan nilai i ke listbox1
hasil:


2.
Listing programnya


Public Class Form2


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        ListBox1.Items.Clear()
        For i = 1 To Val(TextBox1.Text)
            If (i Mod 4 = 3) Then
                ListBox1.Items.Add(i)
            End If
        Next
    End Sub
End Class

Keterangan:

ListBox1.Items.Clear()
ini untuk membersihkan nilai yang ada di ListBox1
For i = 1 To Val(TextBox1.Text) 
ini untuk loopingdari 1 sampai nilai di textbox1
   If (i Mod 4 = 3) Then
                ListBox1.Items.Add(i)
            End If

Jika i dibagi 4 sisanya tiga maka tambahkan nilai i kedalam listbox1

Hasil

Operasi Perulangan

Operasi Perulangan dengan for....
contoh 1 . menampilkan bilangan genap
cuplikan program

Public Class Form1

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer
        ListBox1.Items.Clear()
        For i = 1 To Val(TextBox1.Text)
            If i Mod 2 = 0 Then
                ListBox1.Items.Add(i)
            End If
        Next
    End Sub
End Class

Hasilnya


contoh 2 Menampilkan bilangan   4,7,8,14,16,28,32,56...

Listing programnya:

Public Class Form2

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i, u, y As Integer
        u = 4
        y = 7
        For i = 1 To Val(TextBox1.Text)
            If i Mod u = 0 Then
                ListBox1.Items.Add(i)
                u = i
            Else
                If i Mod y = 0 Then
                    ListBox1.Items.Add(i)
                    y = i


                End If
            End If
        Next
    End Sub
End Class


Hasil:


Jumat, 25 Februari 2011

Input Data

Input data sederhana
Langsung latihan saja... buat project baru...buat form sehingga hasilnya outputnya nanti seperti gambar



Listing programnya:


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String
        s = 6
        If TextBox1.Text = "TI" Then
            TextBox2.Text = "Tehnik Informatika" + Trim(s)
        Else
            If TextBox1.Text = "SI" Then
                TextBox2.Text = "Sistem Informatika"
            End If
        End If
    End Sub


    Sub perintah()
        Dim s As String
        s = 6
        If TextBox1.Text = "TI" Then
            TextBox2.Text = "Tehnik Informatika" + Trim(s)
        Else
            If TextBox1.Text = "SI" Then
                TextBox2.Text = "Sistem Informatika"
            End If
        End If
    End Sub




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call perintah()




    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click




        ListBox1.Items.Add(TextBox3.Text + " " + TextBox4.Text _
        + " " + TextBox5.Text + " " + TextBox6.Text _
        + " " + TextBox7.Text)


    End Sub
    Sub TAMBAHLIST()
        ListBox1.Items.Add(TextBox3.Text + " " + TextBox4.Text _
       + " " + TextBox5.Text + " " + TextBox6.Text _
       + " " + TextBox7.Text)
    End Sub
    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox4.Focus()
        End If
    End Sub


   
    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox5.Focus()
        End If
    End Sub


    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox6.Focus()
        End If
    End Sub


    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        If e.KeyChar = Chr(13) Then
            TextBox7.Focus()
        End If
    End Sub


    Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        If e.KeyChar = Chr(13) Then
            Call TAMBAHLIST()


            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""


            TextBox3.Focus()
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()


    End Sub
End Class