Disini saya tidak akan menjelaskan bagaimana melakukan koneksi ke database access, intinya aplikasi Anda sudah siap menjalankan perintah SELECT, INSERT, UPDATE, dan DELETE.
Untuk kasus disini kita akan menyimpan data mahasiswa dengan tiga field saja, lihat gambar berikut :
Khusus untuk field foto tipe yang dipilih adalah OLE Object, kita langsung aja copy paste kode berikut di module :
003 | Public Const CHUNK_SIZE As Long = 16384 |
005 | Dim rsImage As ADODB.Recordset |
010 | Dim nFragmentOffset As Long |
013 | Dim nHandle As Integer |
014 | Dim varChunk() As Byte |
016 | Public Function fileExists(ByVal strNamaFile As String) As Boolean |
017 | If Not (Len(strNamaFile) > 0 ) Then fileExists = False: Exit Function |
019 | If Dir$(strNamaFile, vbNormal) = "" Then |
026 | Public Sub closeRecordset(ByVal vRs As ADODB.Recordset) |
029 | If Not (vRs Is Nothing) Then |
030 | If vRs.State = adStateOpen Then |
037 | Public Function addImageToDB(ByVal query As String, ByVal imageName As String, ByVal imageField As String) As Boolean |
038 | On Error GoTo errHandle |
040 | Set rsImage = New ADODB.Recordset |
041 | rsImage.Open query, conn, adOpenKeyset, adLockOptimistic |
042 | If Not rsImage.EOF Then |
044 | Open imageName For Binary Access Read As nHandle |
046 | If nHandle = 0 Then Close nHandle |
048 | lchunks = lsize / CHUNK_SIZE |
049 | nFragmentOffset = lsize Mod CHUNK_SIZE |
051 | ReDim varChunk(nFragmentOffset) |
052 | Get nHandle, , varChunk() |
053 | rsImage(imageField).AppendChunk varChunk() |
055 | ReDim varChunk(CHUNK_SIZE) |
057 | Get nHandle, , varChunk() |
058 | rsImage(imageField).AppendChunk varChunk() |
063 | Call closeRecordset(rsImage) |
072 | Public Function getImageFromDB(ByVal query As String) As IPictureDisp |
075 | On Error GoTo errHandle |
077 | Set rsImage = New ADODB.Recordset |
078 | rsImage.Open query, conn, adOpenForwardOnly, adLockReadOnly |
079 | If Not rsImage.EOF Then |
080 | If Not IsNull(rsImage( 0 ).Value) Then |
083 | sFile = App.Path & "\output.bin" |
084 | If fileExists(sFile) Then Kill sFile |
087 | Open sFile For Binary Access Write As nHandle |
089 | lsize = rsImage( 0 ).ActualSize |
090 | iChunks = lsize \ CHUNK_SIZE |
091 | nFragmentOffset = lsize Mod CHUNK_SIZE |
093 | varChunk() = rsImage( 0 ).GetChunk(nFragmentOffset) |
094 | Put nHandle, , varChunk() |
096 | ReDim varChunk(CHUNK_SIZE) As Byte |
098 | varChunk() = rsImage( 0 ).GetChunk(CHUNK_SIZE) |
099 | Put nHandle, , varChunk() |
104 | Set getImageFromDB = LoadPicture(sFile, , vbLPColor) |
107 | Set getImageFromDB = Nothing |
111 | Set getImageFromDB = Nothing |
113 | Call closeRecordset(rsImage) |
117 | Set getImageFromDB = Nothing |
Untuk di form cukup dengan kode sederhana berikut :
01 | Private Sub cmdSimpan_Click() |
02 | 'untuk format gambar bisa JPG atau BMP |
03 | strSql = "INSERT INTO mhs (nim, nama) VALUES ('" & txtNIM.Text & "', '" & txtNama.Text & "')" |
06 | If fileExists(txtLokasiFoto.Text) Then |
07 | strSql = "SELECT nim, foto FROM mhs WHERE nim = '" & txtNIM.Text & "'" |
08 | If Not addImageToDB(strSql, txtLokasiFoto.Text, "foto" ) Then MsgBox "Foto mahasiswa gagal disimpan !" |
12 | Private Sub cmdTampil_Click() |
13 | strSql = "SELECT foto FROM mhs WHERE nim = '" & txtNIM.Text & "'" |
14 | Set picMhs.Picture = getImageFromDB(strSql) |
Selamat mencoba
makasi ya gan!! izin sedot ya
BalasHapus