Handling NotInList
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 255   1
URL.หัวข้อ / URL
Handling NotInList

ผมค้นเจอคำแนะที่เขาให้ไว้ที่ http://users.bigpond.net.au/abrowne1/ser-08.html เขาเขียนไว้อย่างนี้ครับ
........
Tip 1.3 - Handling NotInList
A final thing that you want your combo to do is to have provision for entering new list items if a user types in a new list item. I shall switch examples here: let's say you have a table of customers - name, address, phone no etc. You have a combo based on 'select name from custlist' and want the user to be able to enter a new customer name and have a dialog pop up.

The first go at programming this will look something like this:

Sub CustName_NotInList (NewData As String, Response As Integer)
If MsgBox("""" & NewData & """ is not in the customer list. Add it?", 33) <> 1 Then
Response = DATA_ERRCONTINUE
Exit Sub
End If
DoCmd OpenForm "AddNewCust", , , , 1 'Data Entry Mode
Forms!AddNewCust!Name = NewData
Response = DATA_ERRADDED
Exit Sub
This will not work as expected. The new form will pop up, then control will return to the combo. The combo, having been told that the data has been added, will look for it and not find it, and will pop up an error message over the top of the form.

1.3.1 - Solution 1

Sub CustName_NotInList (Newdata As String, Response As Integer)
If Not IsNull(DLookup("Name", "CustList", "Name=""" & Newdata & """")) Then
Response = DATA_ERRADDED
Exit Sub
End If
If MsgBox("""" & Newdata & """ is not in the customer list. Add it?", 33) <> 1 Then
Response = DATA_ERRCONTINUE
Exit Sub
End If
DoCmd OpenForm "AddNewCust", A_NORMAL, , A_ADD ' Data Entry Mode.
Forms!AddNewCust!Name = Newdata
Response = DATA_ERRCONTINUE
End Sub
The flow of events is this:

The user types in an unknown customer name, and so NotInList gets triggered.
NotInList looks for the customer name via DLookup, doesn't find it, asks if the user wants to continue, and pops up the dialog form.
It then exits, telling the combo that the error has been handled.
The user then enters the customer details on the dialog and then closes it.
When the user moves to the next field, the combo does not yet know that a new customer has been added, and so NotInList is triggered again.
This time, the DLookup finds the customer name in the table and so tells the combo to requery itself by returning DATA_ERRADDED.
The combo requeries, finds the newly added customer, and closes happy.
Disadvantages: DLookup is slow

1.3.2 - Solution 2

Sub CustName_NotInList (Newdata As String, Response As Integer)
If MsgBox("""" & Newdata & """ is not in the customer list. Add it?", 33) <> 1 Then
Response = DATA_ERRCONTINUE
Exit Sub
End If
DoCmd OpenForm "AddNewCust", , , , 1 'Data Entry Mode
Forms!AddNewCust!Name = Newdata
Response = DATA_ERRCONTINUE
End Sub
The key here is to stick a Forms![MainForm]!CustName.Requery inside the AfterUpdate event on the AddNewCustName form. This will update the combo box list after you add the new customer in the dialog.

Disadvantages:

Ties the AddNewCustName form to the MainForm - you can't use it from another form.


อ่านแล้วก็งงว่า sub CustName_NotInList จะใส่ไว้ตรงไหน อยากขอคำแนะนำจาก อ.สุภาพ หรือผู้รู้ ด้วยครับ และถ้าอยากให้อาจารย์ช่วยชี้แนะเพิ่มเติมกระทู้ที่ 01624 ด้วยครับ

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

1 @R03723
NotInList เป็นเหตุการณ์ที่มีอยู่ใน Combo Box ครับ ถ้าเราเลือกให้เป็น Yes แล้วเราจะไม่สามารถพิมพ์ข้อความหรือตัวเลขลงในช่องนี้ได้ ต้องเลือกจากรายการที่มีให้เท่านั้น หรือ จะใช้โค้ดช่วยนำข้อมูลที่มีให้ไปเปิดให้ในรายการด้วย จึงมีการใช้เหตุการณ์นี้เพื่อถามผู้ใช้ว่า ต้องการจะเพิ่มรายการใหม่ที่พิมพ์ลงไปหรือไม่

จากตัวอย่างที่ให้มา จะมี Combo Box ชื่อ CustName แล้วใช้ NotInList event ช่วยเพิ่มรายการใหม่ลงไป เขายกตัวอย่างให้เห็นว่าถ้าใช้โค้ดแบบแรกจะเป็นมีปัญหา และให้แนวทางแก้ไขมาให้ด้วย

ลองอ่านกระทู้ http://www.thai-access.com/suphap.php?topic_id=257 ประกอบด้วยนะครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0527s