Import ข้อมูลใน List ให้มาอยู่ใน subform
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 301   9
URL.หัวข้อ / URL
Import ข้อมูลใน List ให้มาอยู่ใน subform

จากข้อมูลที่ส่งตัวอย่างมาให้อาจารย์ดู คือว่าถ้าเรากด command Product แล้วเราเลือกสินค้าจาก listbox ที่เราทำแล้วเมือเรากด command Add Product ให้สินค้าที่เราเลือกไปอยู่ใน Subform จะได้หรือเปล่าค่ะ

รบกวนอาจารย์ด้วยค่ะ ขอบคุณมากค่ะ

9 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R03435
ผมทดลองทำดูมันได้แค่ไปแทนที่ครับ ถ้าเรค์ครอดใหม่ไม่ได้ครับ
DoCmd.RunSQL "UPDATE tblReceiptDetail SET tblReceiptDetail.ProductID = " & [Forms]![frmProduct]![List0] & ",tblReceiptDetail.ID = " & [Forms]![frmReceiptMain]![ID] & _
" WHERE tblReceiptDetail.[AutoID] = " & [Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID]

[Forms]![frmReceiptMain]![frmSubReceipt].Requery
DoCmd.Close acForm, "frmProduct"
2 @R03436
ลืมบอกไป ผมใช้ Event DblClick ของ List0 ของฟอร์ม frmProduct นะครับ
3 @R03437
เอาโค๊ตนี้ไปใช้ใน Event DblClick ของ List0 ใน frmProduct

Dim db As DAO.database
Dim rst As DAO.Recordset
Dim strup As String

If IsNull([Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID]) = True Or [Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID] = 0 Then

Set db = CurrentDb
Set rst = db.OpenRecordset("tblReceiptDetail")

rst.MoveLast
rst.AddNew

rst("ProductID") = [Forms]![frmProduct]![List0]
rst("ID") = [Forms]![frmReceiptMain]![ID]

rst.Update
rst.Close
db.Close

Set rst = Nothing
Set db = Nothing
Else

strup = "UPDATE tblReceiptDetail SET tblReceiptDetail.ProductID = " & [Forms]![frmProduct]![List0] & ",tblReceiptDetail.ID = " & [Forms]![frmReceiptMain]![ID] & _
" WHERE tblReceiptDetail.[AutoID] = " & [Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID]

MsgBox strup
DoCmd.RunSQL strup
End If

[Forms]![frmReceiptMain]![frmSubReceipt].Requery
DoCmd.Close acForm, "frmProduct"
4 @R03440
ขอบพระคุณมากๆ เลยค่ะ เดี๋ยวขอลองทำดูก่อนนะค่ะ เดี๋ยวมาแจ้งผลให้ฟังค่ะ คุณ moo
5 @R03441
ฟ้องกับบันทัดนี้ค่ะ ถามหา AutoID ค่ะ แต่พอเปลี่ยน เป็น ID ไม่ error ค่ะ แต่ว่า ไม่สามารถ เติมข้อมูลได้ค่ะ

If IsNull([Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID]) = True Or [Forms]![frmReceiptMain]![frmSubReceipt].[Form]![AutoID] = 0 Then
6 @R03445
ขอเพิ่มเติมนะค่ะ ถ้าเปลี่ยนจาก AutoID เป็น ID ข้อมุลจะเข้าเฉพาะ ID เท่านั้น แต่ product ไม่ยอมเข้าไปด้วย
7 @R03446
พอดีเจอปัญหาคล้าย ๆกับของผม ปัญหาก็คือเวลาเราสั่ง อัพเดทข้อมูลจากฟอร์มอื่น มันจะไม่ยอมรีเฟรทให้ จึงทำให้อัพเดทได้ครั้งละ 1 เรคคอร์ดผมเลยทำการรวมเป็นSubform

ผมทำมาให้เป็นใหม่โดยการดึง Frmproduct เข้ามาใน Subform
วิธีใช้
1. คลิกที่ Addproduct จะโชว์ Subform ของ Frmproduct
2. คลิกที่รายการ เพื่อเพิ่มข้อมูลอัตโนมัติ
ลองดูครับ
8 @R03451
ลองใช้โค้ดนี้กับปุ่ม ADD Product ดูครับ

Private Sub Command14_Click()
Dim intCurrentRow As Integer, ctl As Control
Dim dbs As Object, rst As Object
If IsLoaded("frmProduct") Then
Set ctl = Forms!frmProduct!List0
Set dbs = CurrentDb
Set rst = dbs.openrecordset("tblReceiptDetail")
If ctl.ItemsSelected.Count > 0 Then
For intCurrentRow = 0 To ctl.ListCount - 1
If ctl.Selected(intCurrentRow) Then
MsgBox ctl.ItemData(intCurrentRow) & " " _
& ctl.Column(1, intCurrentRow) & " " _
& ctl.Column(2, intCurrentRow) & " " _
& ctl.Column(3, intCurrentRow) & " " _
& ctl.Column(4, intCurrentRow)
rst.AddNew
rst("ID") = Me.ID
rst("ProductID") = ctl.ItemData(intCurrentRow)
rst("ProductName") = ctl.Column(1, intCurrentRow)
rst("UnitPrice") = ctl.Column(4, intCurrentRow)
rst.Update
End If
Next intCurrentRow
Me.frmSubReceipt.Requery
End If
rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing
End If
End Sub

และต้องมีฟังก์ชันนี้ด้วย

Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function

9 @R03452
ขอบคุณมากค่ะอาจารย์สุภาพ และอาจารย์ Access2000 ใช้ได้ทั้ง 2 เลยค่ะ จะเก็บไว้เป็นตัวอย่างเพื่อประยุกต์งานต่อไปค่ะ

ขอบคุณอีกครั้ง
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0496s