Loading DAO at run time....
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 270   1
URL.หัวข้อ / URL
Loading DAO at run time....

มีคนถามไว้ที่ 
http://www.access-programmers.co.uk/forums/showthread.php?s=&postid=107400#post107400 
 
Hi experts,  
 
I am using a bit of VB code in my form and that needs to have DAO references loaded first.  
I needed to run the same code in another database, and it generated error, coz DAO was not loaded there.  
 
What I need is to have a VB code to load DAO at run time if i need to run this form in any database.  
 
Thanx 
 
 
__________________ 
EverBlue 
 
ถ้าใครเป็นผู้พัฒนาโปรแกรม Access 2K หรือ 2002 มักจะมีปัญหากับโค้ดของตัวเองที่เรียกใช้ Microsoft DAO Object Library 3.X หรือเรียกสั้นๆ ว่า DAO ซึ่ง DAO ตัวนี้จะดูเหมือนกลายเป็นลูกที่ไม่มีค่าสำหรับ Microsoft อีกแล้ว จึงไม่ได้ใส่เข้าไปใน Access ให้เราโดยอัตโนมัติตั้งแต่ Access 2000 เป็นต้นมา แต่จะให้ ADO มาแทน ถ้าใครต้องการใช้ต้องใส่เข้าไปเอง จะด้วยการไปที่ VB Editor>Tools>References>มองหา DAO แล้วคลิกเลือก 
 
หรือถ้าเป็นนักพัฒนาก็จะใช้วิธีการตรวจหาก่อนว่าได้มีการใส่ Reference ให้ DAO 3.6 หรือยัง ถ้ายังก็ให้ใส่ให้โดยการเขียนโค้ด เหมือนคนที่โพสต์ถามแหล่ะครับ  
 
นี่คือโค้ดสำหรับใส่ DAO ใน References ครับ 
 
Function ReferenceFromFile(strFilename As String) As Boolean 
    Dim Ref As Reference 
 
    On Error GoTo Error_ReferenceFromFile 
    Set Ref = References.AddFromFile(strFilename) 
    ReferenceFromFile = True 
 
Exit_ReferenceFromFile: 
    Exit Function 
 
Error_ReferenceFromFile: 
    MsgBox Err & ": " & Err.Description 
    ReferenceFromFile = False 
    Resume Exit_ReferenceFromFile 
End Function 
 
Sub CreateCalendarReference() 
    If ReferenceFromFile("C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll") = True Then 
        MsgBox "Reference set successfully." 
    Else 
        MsgBox "Reference not set successfully." 
    End If 
End Sub 
 
ซึ่งเราจะต้องรู้จักห้องที่เก็บไฟล์ dao360.dll ก่อนล่วงหน้าด้วย หรือที่ผมทำคือ ใส่ dll นี้ไว้ในห้องเดียวกับโปรแกรมของเราเลย จะได้อ้างอิงโดยเอาจากห้องที่เก็บไฟล์ db นั่นเอง 
 
คำถามเพิ่มเติม 
 
1. ถ้าต้องการจะตรวจสอบดูก่อนว่ามีการตั้ง dao ไว้หรือยัง จะทำอย่างไรดี 
2. ถ้าจะโหลด Object Library ตัวอื่นๆ จะใช้หลักการนี้ได้หรือไม่ เช่นต้องการจะใช้ฟังก์ของ Excel ใน Access ก็จะต้องเรียกใช้ Excel9.olb 
3. ถ้าจะเอา dao ออกจาก References โดยใช้โค้ดจะทำอย่างไรดี 
 
 

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

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