ต้องการทำการสุ่มค่าใน Table หนึ่ง แล้วไปเก็บไว้อีก Table หนึ่ง ได้อย่างไร
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 222   2
URL.หัวข้อ / URL
ต้องการทำการสุ่มค่าใน Table หนึ่ง แล้วไปเก็บไว้อีก Table หนึ่ง ได้อย่างไร

ใน MS Access ผมต้องการทำการสุ่มค่าใน Table หนึ่ง แล้วไปเก็บไว้อีก Table หนึ่ง เช่น 
TableBeforeCheck                              TableAfterCheck 
ID            Desc                                              ID                       Desc 
1              น้ำแข็ง 
2              ยาคูลท์ 
3              น้ำเฉาก๊วย 
4             ไฟแช็ค 
5             กะทิงแดง 
6            ลิโพ 
ทำการสุ่ม ครั้งที่ 1 จำนวน 2 รายการ 
TableBeforeCheck                                            TableAfterCheck 
ID            Desc                                       ID               Desc 
2              ยาคูลท์                                          1              น้ำแข็ง 
4             ไฟแช็ค                                          3              น้ำเฉาก๊วย   
5              กะทิงแดง 
6             ลิโพ 
 
ทำการสุ่มครั้งที่ 2 จำนวน 2 รายการ จะต้องสุ่ม แค่เลข 2,4,5,6 เพราะว่าเลข 1 กับ 2 ได้ใช้ไปแล้วและย้ายไปที่ตาราง 
TableAfterCheck แล้ว 
TableBeforeCheck                                       TableAfterCheck 
ID            Desc                                       ID               Desc 
4             ไฟแช็ค                                         1              น้ำแข็ง 
5              กะทิงแดง                                    3              น้ำเฉาก๊วย   
                                                                    6              ลิโพ 
                                                                    2              ยาคูลท์   
ทำการสุ่มครั้งที่ 3 จำนวน 2 รายการ จะต้องสุ่ม แค่เลข 4,5 เพราะว่าเลข 1,2,3,6  ได้ใช้ไปแล้วและย้ายไปที่ตาราง 
TableAfterCheck แล้ว 
TableBeforeCheck                                       TableAfterCheck 
ID            Desc                                        ID               Desc 
                                                                    1              น้ำแข็ง 
                                                                    3              น้ำเฉาก๊วย   
                                                                    6              ลิโพ 
                                                                    2              ยาคูลท์   
                                                                    4             ไฟแช็ค  
                                                                    5              กะทิงแดง 
เมื่อครบหมดแล้วก็ให้มี Message บอก แล้วทำการเริ่มต้นทำการเรียกค่าที่ย้ายไปกับมายังตาราง TableBeforeCheck ใหม่ 
TableBeforeCheck                              TableAfterCheck 
ID            Desc                                              ID                       Desc 
1              น้ำแข็ง 
2              ยาคูลท์ 
3              น้ำเฉาก๊วย 
4             ไฟแช็ค 
5             กะทิงแดง 
6            ลิโพ 
 
ไม่ทราบว่าต้องทำยังไงครับ

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

1 @R00393
ลองใช้โค้ดข้างล่างดูครับ Private Sub cmdRandomize_Click() Dim intRec As Long, J As Integer Dim dbs As Database, rst As Recordset, rst2 As Recordset Set dbs = CurrentDb Set rst2 = dbs.OpenRecordset("TableAfterCheck") Set rst = dbs.OpenRecordset("TableBeforeCheck") If Not rst.EOF Then rst.MoveLast rst.MoveFirst If rst.RecordCount <= 2 Then ' MsgBox "No record to be randomized", vbOKOnly dbs.Execute "INSERT INTO TableAfterCheck " _ & "SELECT TableBeforeCheck.* " _ & "FROM TableBeforeCheck;" dbs.Execute "Delete * From TableBeforeCheck;" Else For J = 1 To 2 rst.MoveLast rst.MoveFirst intRec = rst.RecordCount - 1 rst.MoveFirst intRec = CInt((intRec - 0 + 1) * Rnd + 0) If intRec = rst.RecordCount Then intRec = intRec - 1 End If ' โอนข้อมูลไปตาราง TableAfterCheck rst.Move intRec rst2.AddNew rst2(0) = rst(0) rst2(1) = rst(1) rst2.Update ' ลบข้อมูลออกจากตาราง TableBeforeCheck rst.Delete Next J End If Else MsgBox "No record to be randomized", vbOKOnly dbs.Execute "INSERT INTO TableBeforeCheck " _ & "SELECT TableAfterCheck.* " _ & "FROM TableAfterCheck;" dbs.Execute "Delete * From TableAfterCheck;" End If rst.Close rst2.Close dbs.Close End Sub ตัวอย่างจริงอยู่ที่ http://agserver.kku.ac.th/basiceng/RandomizedAppend.zip ครับ
2 @R00398
ได้แล้วครับ ขอบพระคุณอาจารย์อย่างสูงครับ กุ๊กไก่
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0528s