Pada artikel sebelumnya kita sudah mengetahui bagaimana menampilkan data dari database access ke datagrid, sekarang kita akan mencoba membuat fitur filter dengan keyword.
UI dan data masih lanjut dari artikel sebelumnya.
Code pun masih menggunakan code sebelumnya dengan menambahkan beberapa baris code yang ditandai dengan highlight kuning.
Option Explicit
Dim oConn As New ADODB.Connection
Dim rsData As New ADODB.Recordset
Dim strConn As String
Dim SQL As String
Sub Open_Connection()
Set oConn = New ADODB.Connection
oConn.ConnectionString = strConn
oConn.Open
End Sub
Sub Load_Data(Optional strFilter As String)
On Error GoTo errHandler
Open_Connection
Set rsData = New ADODB.Recordset
SQL = "SELECT * FROM Category " & strFilter
With rsData
.CursorLocation = adUseClient
.Open SQL, oConn, adOpenDynamic, adLockOptimistic
.ActiveConnection = Nothing
End With
Set grdData.DataSource = rsData
oConn.Close
Exit Sub
errHandler:
MsgBox Err.Number & ":" & Err.Description
End Sub
Private Sub Form_Load()
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\latihan.mdb;" & _
"Persist Security Info=False"
Load_Data
End Sub
Private Sub cmdFilter_Click()
Load_Data "WHERE CategoryCode LIKE '%" & txtFilter.Text & "%' " & _
"OR CategoryName LIKE '%" & txtFilter.Text & "%' "
End Sub
!!! Jangan lupa untuk menambahkan spasi pada SQL setelah Category.
Runtime:
Kemudian bagaimana dengan proses input, edit dan delete?
Mari kita mulai pembahasan dengan proses inpu data baru.
Click here if you like this article.
8 Comments
error 3704 biasanya karena recordset digunakan tapi blom open.
Option Explicit
Dim oConn As New ADODB.Connection
Dim rsData As New ADODB.Recordset
Dim strConn As String
Dim SQL As String
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdPrint_Click()
DataReport1.Show
End Sub
Private Sub Form_Load()
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\zsofttadb.mdb;" & _
"Persist Security Info=False"
Load_Data
End Sub
Sub Open_Connection()
Set oConn = New ADODB.Connection
oConn.ConnectionString = strConn
oConn.Open
End Sub
Private Sub cmdFilter_Click()
Load_Data "WHERE Tanggal_Log LIKE '%" & TxtFilter.Text & "%' " & _
"OR Nama_Staff LIKE '%" & TxtFilter.Text & "%' "
End Sub
Sub Load_Data(Optional strFilter As String)
On Error GoTo errHandler
Open_Connection
Set rsData = New ADODB.Recordset
SQL = "SELECT * FROM TA_Log " & strFilter
With rsData
.CursorLocation = adUseClient
.Open SQL, oConn, adOpenDynamic, adLockOptimistic
.ActiveConnection = Nothing
End With
Set grdData.DataSource = rsData
With grdData
.Columns(0).Visible = False
.Columns(1).Visible = False
.Columns(2).Visible = False
.Columns(3).Width = 2300
.Columns(3).Caption = "Nama"
.Columns(4).Visible = False
.Columns(5).Visible = False
.Columns(6).Visible = False
.Columns(7).Width = 1500
.Columns(8).Width = 1500
.Columns(9).Visible = False
.Columns(10).Visible = False
.Columns(11).Visible = False
.Columns(12).Visible = False
.Columns(13).Caption = "Tanggal & Jam"
End With
oConn.Close
Exit Sub
errHandler:
MsgBox Err.Number & ":" & Err.Description
End Sub
kecuali di format ke string
Makasih sebelumnya..
Load_Data "where CategoryName Like '%" & TxtFilter.Text & "%'"
http://rani-irsan.blogspot.com/2016/01/vb6-ms-access-pengolahan-data_28.html