Prompt to update table
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 348   1
URL.หัวข้อ / URL
Prompt to update table

หลังจากที่เมื่อวานผมได้แนะนำเว็บไซต์ถามตอบ Access แหล่งที่ผมค้นเจอใหม่ ผมก็ได้เข้าไปช่วยตอบหลายๆ คำถามประเดิมไว้แล้ว ไม่ได้ต้องการที่จะอวดว่าตัวเองเก่ง แต่ต้องการที่จะแบ่งปันความเข้าใจและแนวทางการแก้ปัญหาเกี่ยวกับ Access ให้คนอื่นๆ ที่เขาเจอปัญหา ซึ่งเขาเหล่านั้นต้องการคำแนะนำ หรือยิ่งมีโค้ดตัวอย่างให้ดูด้วยก็จะดีมาก และจะเป็นประโยชน์กับตัวผมเองในแง่ที่จะตรวจสอบความเข้าใจของเราว่า ถ้ามีปัญหาอย่งนี้ ควรจะแก้อย่างไร ทำตามแนวทางของเราจะช่วยแก้ปัญหาได้หรือไม่ ถ้าไม่ได้ แล้วคนอื่นเขาแนะนำว่าอย่างไร 
 
ในการแก้ปัญหา จะไม่จำเป็นว่าจะต้องแก้ทางเดียว แต่อาจมีวิธีจัดการได้หลายๆ แบบ แล้วแต่ว่าใครเคยประสบอะไรมาก่อน และถนัดอะไร การแก้ปัญหาบางอย่างทำทำได้ในบางเครื่องฯ หรือบางระบบปฏิบัติการเท่านั้น 
 
การเข้าไปอ่านตามเว็บบอร์ดต่างๆ ทำให้ผมได้อาหารสมอง มีความสุขกับการได้คิดหาวิธีแก้ 
 
เข้าเรื่องเสียทีนะครับ เขาถามไว้ที่ 
http://www.access-programmers.co.uk/forums/showthread.php?s=&postid=106448#post106448 
 
ดังนี้ 
 
I have tables called "Orders", "Inventory", "Materials" and the three are related 
with Inventory being the joing table of a many to many relationship.  
 
The database is used to keep track of orders and I have a material lookup so 
the user can easily select from what has been ordered. The problem is that when 
a user enters something that is not in the lookup table "Materials" as an entry, 
Access displays a message "a related record in table 'materials' is required."  
 
After having explained this what I would like to do is instead of that message by 
access, have my own message that prompts the user if they would like to add 
their entry to the material table.  

Then conflict would no longer exsist and the new material would be with all the 
others in the lookup table. Please let me know how I could go about attempting this. 
 
สรุปคือ เขาต้องการให้ เมื่อผู้ใช้คีย์รายการใน Combo Box ลง ซึ่งดึงมาจากตาราง แต่รายการนั้นยังไม่มี Access จะแสดงข้อความเตือน แต่เขาต้องการเปลี่ยนข้อความเตือนนั้น และให้รายการที่พิมพ์ลงไปใหม่นั้นไปเพิ่มไว้ในตารางเป้าหมายได้เอง และจะได้อยู่ใน Combox นั้นสำหรับการคีย์ครั้งต่อไป 
 
ตอนแรกด้วยความที่ผมอ่านคำถามไม่ถ่องแท้ ทำให้ตอบไปคนละทางเลย 
เมื่อมาดูที่เขาโพสต์ตอบ ก็เลยกลับมาอ่านคำถามใหม่อีกครั้ง จึงเข้าใจ และได้ให้โค้ดข้างล่างนี้ไปครับ 
 
Private Sub acPlaceType_NotInList(NewData As String, Response As Integer) 
Dim dbs As Database, rst As Recordset 
Set dbs = CurrentDb 
 
Response = acDataErrContinue 
If MsgBox("ไม่มีรายการนี้ ต้องการจะเพิ่มเข้าไปหรือไม่?", vbOKCancel) = vbOK Then 
        ' Set Response argument to indicate that data is being added. 
        Response = acDataErrAdded 
        ' Add string in NewData argument to a table tblDiscipline. 
               
        With dbs 
            Set rst = .OpenRecordset("tblPlace") 
                rst.AddNew 
                    rst!PlaceName = NewData 
                rst.Update 
            rst.Close 
            .Close 
        End With 
 
Else 
    ' If user chooses Cancel, suppress error message and undo changes. 
        Response = acDataErrContinue 
End If 
 
End Sub 
 
โคดข้างต้นผมจะใช้บ่อยมากในการเขียนโปรแกรมของผม รู้สึกว่าจะมีเกือบทุกโปรแกรมที่ผมเคยทำมา จึงคิดว่าเป็นโค้ดที่น่าสนใจครับ *** Edited by Supap Chaiya *** 5/28/2003 10:14:41 AM

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

1 @R03431
เยี่ยมจริงๆ ค่ะทั้งข้อคิดที่เกริ่นนำก่อนเข้าเรื่อง
และ code ที่ใช้ประโยชน์ได้คุ้มจริงๆ
แทบจะทุกโปรแกรมที่เกี่ยวกับการ input ข้อมูล
อย่างที่ท่านอาจารย์ว่า

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