Thony Blair wrote:
Dear Blair,Dear All pakar VBer mohon pencerahan... bagaimana caranya mengupdate kolom dengan nilai yang diambil dari kolom di tabel lainMisal Saya punya 2 tabel Tabel SATU Kode_brg Nama_Barang Qty1 qty_Trans 0001 Susu Kaleng 1kg 10 - 0002 Indomie rasa Ayam 15 - 0003 Gula Pasir 20 - 0004 Penyedap Rasa 100 - Tabel DUA Kode_brg Nama_Barang Qty2 0001 Susu Kaleng 1kg 55 0002 Indomie rasa Ayam 29 0003 Gula Pasir 46 Bagaimana cara memasukkan nilai Qty2 pada tabel DUA kedalam qty_trans pada Tabel SATU? dengan kondisi kode_brg nya sama. Jadi hasilnya kira-kira begini : Kode_brg Nama_Barang Qty1 qty_Trans 0001 Susu Kaleng 1kg 10 55 0002 Indomie rasa Ayam 15 29 0003 Gula Pasir 20 46 0004 Penyedap Rasa 100 0 NOTE : Saya pakai Database Access Terimakasih sebelumnya salam, mc ![]() Mumpung weekend nih :-) Saya berusaha mensimulasikan persoalan anda kedalam program komputer saya.oke langsung aja yah : Buat Project baru dan buat Form seperti diatas. 1 datagrid, 1 TextBox, 1 Label ,atur supaya bentuk label sesuai dengan textbox (seperti gambar diatas), 3 command button. Beri caption dari masing-masing command button seperti caption command button pada gambar diatas. Saya menggunakan koneksi dengan referensi adodb.jadi tambahkan adodb reference ke project.Saya buat database dengan nama : Latihan01.mdb dan saya letakkan project ini di folder Latihan1,untuk Tabel 1 pada database access saya berinama Satu,dan tabel 2 saya berinama Dua, sesuaikan coding untuk koneksi dibawah ini dengan kebutuhan kamu : Public Sub koneksiDb() koneksi.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Latihan1\Latihan01.mdb;Persist Security Info=False" koneksi.Open End Sub **Oke copy dan paste coding saya dibawah ini : * Option Explicit Dim koneksi As New ADODB.Connection Dim fillrs As New ADODB.Recordset Dim Saturs As New ADODB.Recordset Dim Duars As New ADODB.Recordset Public Sub koneksiDb() koneksi.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Latihan1\Latihan01.mdb;Persist Security Info=False" koneksi.Open End Sub Private Sub Command1_Click() Label1.Visible = False Text1.Visible = True If Command1.Caption = "Table 1" Then If Not (Saturs.State = adStateOpen) Then With Saturs .ActiveConnection = koneksi .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient .Open "Select * from Satu" End With Set DataGrid1.DataSource = Saturs Command1.Caption = "Table 2" Text1.Text = "isi dari Tabel Satu" Else Saturs.Close Set Saturs = Nothing End If Else If Command1.Caption = "Table 2" Then If Not (Duars.State = adStateOpen) Then With Duars .ActiveConnection = koneksi .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient .Open "Select * from Dua" End With Set DataGrid1.DataSource = Duars Command1.Caption = "Table 1" Text1.Text = "Isi dari Tabel Dua" Else Duars.Close Set Duars = Nothing End If End If End If End Sub Private Sub Command2_Click() Text1.Visible = False Label1.Visible = True If fillrs.State = adStateOpen Then fillrs.Close Set fillrs = Nothing Else With fillrs .ActiveConnection = koneksi .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient .Open "Select Satu.Kode_Brg as ID, Satu.Nama_Barang as Barang,Satu.Qty1, Dua.Qty2 as qty_trans from satu left join dua on satu.Kode_Brg = Dua.Kode_Brg" End With Set DataGrid1.DataSource = fillrs Label1.Caption = "Gabungan Data dari Tabel satu dan Tabel Dua (Hanya di DataGrid) dengan Query : " & Chr(13) & _ "Select Satu.Kode_Brg as ID, Satu.Nama_Barang as Barang,Satu.Qty1, Dua.Qty2 as qty_trans from satu left join dua on satu.Kode_Brg = Dua.Kode_Brg;" End If End Sub Private Sub Command3_Click() updateSatu End Sub Private Sub Form_Load() koneksiDb End Sub Private Sub updateSatu() Dim jml, i As Integer If Not (Saturs.State = adStateOpen) And Not (fillrs.State = adStateOpen) Then With fillrs .ActiveConnection = koneksi .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient .Open "Select Satu.Kode_Brg as ID, Satu.Nama_Barang as Barang,Satu.Qty1, Dua.Qty2 as qty_trans from satu left join dua on satu.Kode_Brg = Dua.Kode_Brg" End With fillrs.MoveLast jml = fillrs.RecordCount fillrs.MoveFirst With Saturs .ActiveConnection = koneksi .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient For i = 0 To jml On Error Resume Next .Open "Update Satu set qty_trans = " & fillrs.Fields("qty_trans") & " where Kode_Brg = '" & fillrs.Fields("ID") & "'" fillrs.MoveNext Next i End With Saturs.MoveFirst Saturs.Requery Set DataGrid1.DataSource = Saturs Else If Saturs.State = adStateOpen And fillrs.State = adStateOpen Then Saturs.Close Set Saturs = Nothing fillrs.Close Set fillrs = Nothing End If End If End Sub Kemudian Tekan F5, command1 (Tabel 1) ===> view tabel 1(satu) dan tabel 2(dua) command2 (Fill Qty2 to qty_Trans) ==> sebenarnya adalah view gabungan dari tabel 1 dan tabel 2 hanya di datagrid dari recordset dalam hal ini fillrs. command3 (Insert) atau lebih pasnya update, akan mengupdate tabel 1 pada database atau sesuai dengan kriteria pada persoalan anda. sebaiknya pada waktu program dirun anda tekan dulu command1, command2 dan command 3 secara berurutan. (karena saya belum menempatkan coding error handling pada program ini) saya tidak bisa menjelaskan satu persatu coding diatas,,mohon anda pelajari sendiri. Oke sekian dulu jawaban dari saya, semoga membantu Best regadrs, suwanto |

