snasui wrote:
แต่หากจะให้แสดงเฉพาะค่าที่คล้ายกับค่าที่ต้องการค้นหา แนวทางนี้มาผิดทางครับ การทำเช่นนั้นควรจะ Loop จาก Database มาวางใน ListBox ไม่ใช่ให้เลือกรายการใน ListBox ที่ตรงกันกับค่าที่ต้องการค้นหาเช่น Code ด้านบน
การแสดงค่าใน ListBox ควรจะนำค่าจากต้นทางมาแสดง ไม่ใช่นำค่าใน ListBox เองมาแสดง เพราะหากทำเช่นนั้น เมื่อมีการค้นหาไปรอบนึงแล้ว ListBoxt จะเหลือเพียงบางจำนวนที่ตรงกับเงื่อนไขในครั้งนั้น หากคีย์ค้นหาครั้งต่อไปก็จะไม่เหลือรายการที่ตรงกับค่าที่ค้นหาอีก เช่นนี้เป็นต้นครับ
ผมได้ทำการปรับ Code เป็นตัวด้านล่างนี้
Code: Select all
Private Sub TextBox6_Change()
Dim x, i As Long, ii As Long, iii As Integer
x = [_Data]
With ListBox1
If TextBox6 = "" Then
.RowSource = "_Data"
Else
.RowSource = ""
For i = 1 To UBound(x, 1)
If LCase(x(i, 1)) Like LCase(TextBox6) & "*" Then
For ii = 1 To 9
.AddItem
.List(iii, ii - 1) = x(i, ii)
Next
iii = iii + 1
End If
Next
End If
End With
End Sub
สามารถค้นหาได้ตามต้องการแต่ ปัญหาที่ตามมาคือ จากที่สามารถแก้ไขรายการจากการเลือกรายการในListbox ได้ หลังจากใส่ Code แล้วเกิด Error 1004 ตามรูปนี้ครับ รบกวนช่วยดูให้หน่อยนะครับ
Code: Select all
Private Sub CommandButton4_Click() ' edit
Dim sonsat As Long
If ListBox1.ListIndex = -1 Then
MsgBox "คุณยังไม่ได้ใส่ข้อมูล", vbInformation, "ระบบค้นหาสำนวน"
Me.TextBox6.SetFocus
Exit Sub
End If
lastrow = Sheets("Database").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Database").Range("A2:I" & lastrow).Find(What:=ListBox1.Text, LookIn:=xlValues, LookAt:=xlWhole).Activate
sonsat = ActiveCell.Row
Cells(sonsat, 1) = Me.TextBox1.Value
Cells(sonsat, 2) = Me.TextBox2.Value
Cells(sonsat, 3) = Me.TextBox3.Value
Cells(sonsat, 4) = Me.TextBox4.Value
Cells(sonsat, 5) = Me.ComboBox3.Value
Cells(sonsat, 6) = Me.ComboBox4.Value
Cells(sonsat, 7) = Me.ComboBox1.Value
Cells(sonsat, 8) = Me.ComboBox2.Value
Cells(sonsat, 9) = Me.TextBox5.Value
Me.TextBox6.SetFocus
MsgBox "แก้ไขข้อมูลเรียบร้อย", vbInformation, "ระบบค้นหาสำนวน"
ListBox1.List = Sheets("Database").Range("A2:I" & Sheets("Database").Cells(Rows.Count, 1).End(xlUp).Row).Value
Me.TextBox7.Value = ListBox1.ListCount
ActiveWorkbook.Save
End Sub
You do not have the required permissions to view the files attached to this post.