Menyimpan Foto dalam Folder dengan VB

Source Code-nya:
Option Explicit
Private koneksi As ADODB.Connection
Dim rsImage As New ADODB.Recordset
Private Function konek() As Boolean
    On Error GoTo out
        Set koneksi = New ADODB.Connection
        koneksi.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DB1.mdb;Persist Security Info=False"
        koneksi.CursorLocation = adUseClient
        konek = True
out:
End Function

Private Sub CMDBROWSE_Click()
With CommonDialog1
    .FileName = ""
    .Filter = "Image (*.jpg)|*.jpg"
    .ShowOpen
        Image1.Picture = LoadPicture(.FileName)
End With
TXTNIP.Text = ""
TXTNAMA.Text = ""
End Sub


Private Sub CMDFIRST_Click()
If Adodc1.Recordset.BOF Then
    MsgBox "Ini adalah Foto paling Awal", vbInformation + vbOKOnly = vbIgnore
Else
    Adodc1.Recordset.MovePrevious
Call DataGrid1_Click
End If
End Sub

Private Sub CMDHAPUS_Click()
Dim x As String
'Buat pernyatanya sebelum dihapus
x = MsgBox(("Anda yakin data ini mau dihapus?"), vbYesNo + vbCritical)
If x = vbYes Then
'Hapus Record
    Adodc1.Recordset.Delete
    Adodc1.Recordset.MoveFirst
    DataGrid1.ReBind
    DataGrid1.Refresh
    'menghaous foto pada filder foto\ NIP0000.jpg
    Kill (App.Path & "\foto\NIP_" & TXTNIP.Text & ".jpg")
    ' buat laporanya
    MsgBox "foto telah dihapus!", vbInformation + vbOKOnly = vbIgnore
    Call DataGrid1_Click
End If
End Sub

Private Sub CMDNEXT_Click()
If Adodc1.Recordset.EOF Then
    MsgBox "Ini Adalah Foto Paling Akhir", vbInformation + vbOKOnly = vbIgnore
Else
    Adodc1.Recordset.MoveNext
    Call DataGrid1_Click
End If
End Sub

Private Sub CMDSIMPAN_Click()
If TXTNIP.Text = "" Then
    MsgBox "Nama foto harus di isi"
Else
    Adodc1.Recordset.Find "NIP='" + TXTNIP.Text + "'", , adSearchForward, 1
    If Not Adodc1.Recordset.EOF Then
        MsgBox "Maaf, NIP sudah ada!"
        TXTNIP.Text = ""
        TXTNIP.SetFocus
    Else
        Adodc1.Recordset.AddNew
        Adodc1.Recordset!NIP = TXTNIP.Text
        Adodc1.Recordset!Nama = TXTNAMA.Text
        Adodc1.Recordset.Update
        ' code berikut berfungsi untuk menyimpan gambar ke dalam folder foto
        ' nama file gambar didepannya ada kata NIP, contoh: nama foto = NIP_12
        SavePicture Image1.Picture, App.Path & "\foto\NIP_" & TXTNIP.Text & ".jpg"
        'membuat laporan
        MsgBox "Foto telah di Simpan!", vbInformation + vbOKOnly = vbIgnore
    End If
End If
End Sub

Private Sub DataGrid1_Click()

If Adodc1.Recordset.RecordCount <= 0 Then Exit Sub
    If Not Adodc1.Recordset.BOF And Not Adodc1.Recordset.EOF Then
        TXTNIP.Text = Adodc1.Recordset.Fields("NIP")
        TXTNAMA.Text = Adodc1.Recordset.Fields("Nama")
        Image1.Picture = LoadPicture(App.Path & "\foto\NIP_" & TXTNIP.Text & ".jpg")
    End If
End Sub

Private Sub Form_Load()
Adodc1.Visible = False
If Not konek() Then
    MsgBox "Gak bisa terhubung ke database!", vbCritical
End
End If
    Adodc1.ConnectionString = koneksi.ConnectionString
    Adodc1.RecordSource = "Gambar"
    Set DataGrid1.DataSource = Adodc1
End Sub

Interfacing:
 





Komentar

  1. diperjelas lagi sob...
    untuk pembuatan Databasenya
    nama tabelnya juga diperjelas

    BalasHapus

Posting Komentar

Postingan Populer