Tip : ป้องกันโปรแกรมสุดหวง
โจทย์
คุณ ๆ ที่เขียนโปรแกรมด้วย Access บางคนก็หวงโปรแกรม ไม่อยากจะให้ใคร ๆ ดู Object ต่าง ๆ ว่ามีอะไรบ้าง เป็นอย่างไร เพราะ Access สามารถหยุดการ Start Up หรือ หยุดแมโคร Autoexec ได้ด้วยการกดปุ่ม Shift ตอนเปิดโปรแกรม
ก็เลยเป็นที่มาของเรื่องที่จะคุยกันวันนี้ว่า เราจะ Disable ปุ่ม Shift ไม่ให้สามารถใช้งานได้ตอนเปิดโปรแกรม และผมก็ไม่ได้เขียนโค๊ตเองหรอกครับ เอามาจาก Help ของ Microsoft Visaul Basic Help ใน Microsoft Access2003 โดยใช้คำว่า AllowBypassKey เข้าไปค้นหา แล้วก็เอามาคุยให้ฟังครับ
แก้โจทย์
ผลการค้นหาด้วยคำ AllowBypassKey ได้คำอธิบายและโค๊ต ดังนี้ คุณไม่ต้องสนใจอ่านมันก็ได้ครับ
AllowBypassKey Property
You can use the AllowBypassKey property to specify whether or not the SHIFT key is enabled for bypassing the startup properties and the AutoExec macro. For example, you can set the AllowBypassKey property to False to prevent a user from bypassing the startup properties and the AutoExec macro.
Setting
The AllowBypassKey property uses the following settings.
Setting Description
True Enable the SHIFT key to allow the user to bypass the startup properties and the AutoExec macro.
False Disable the SHIFT key to prevent the user from bypassing the startup properties and the AutoExec macro.
You can set this property by using a macro or Visual Basic.
To set the AllowBypassKey property by using a macro or Visual Basic, you must create the property in the following ways:
In a Microsoft Access database (.mdb), you can add it by using the CreateProperty method and append it to the Properties collection of the Database object.
In a Microsoft Access project (.adp), you can add it to the AccessObjectProperties collection of the CurrentProject object by using the Add method.
Remarks
You should make sure the AllowBypassKey property is set to True when debugging an application.
This property's setting doesn't take effect until the next time the application database opens.
Example
The following example shows a procedure named SetBypassProperty that passes the name of the property to be set, its data type, and its desired setting. The general purpose procedure ChangeProperty attempts to set the AllowBypassKey property and, if the property isn't found, uses the CreateProperty method to append it to the Properties collection. This is necessary because this property doesn't appear in the Properties collection until its been added.
Sub SetBypassProperty()
Const DB_Boolean As Long = 1
ChangeProperty "AllowBypassKey", DB_Boolean, False
End Sub
Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant) As Integer
Dim dbs As Object, prp As Variant
Const conPropNotFoundError = 3270
Set dbs = CurrentDb
On Error GoTo Change_Err
dbs.Properties(strPropName) = varPropValue
ChangeProperty = True
Change_Bye:
Exit Function
Change_Err:
If Err = conPropNotFoundError Then ' Property not found.
Set prp = dbs.CreateProperty(strPropName, _
varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
ChangeProperty = False
Resume Change_Bye
End If
End Function
เริ่มทำกันเลย
1. สมมติว่า คุณสร้างโปรแกรมด้วย Access เสร็จเรียบร้อย พร้อมจะเผยแพร่ต่อไป ขอแนะนำว่า คุณควรจะสำเนาต้นฉบับเอาไว้ก่อน เอาไว้คนละแห่งก็ได้
2. จากนั้นเอาไฟล์สำเนาที่ต้องการทำการจัดการต่อไป (หมายความว่า ไฟล์นี้มีฟอร์มทำหน้าที่ฟอร์มแรก หรือ Startup Form แล้วนะครับ)
3. เปิดไฟล์ แล้วไปที่ Module สร้าง Module ขึ้นใหม่ 1 ตัว
4. Copy โค๊ตทั้งหมด ตั้งแต่
Sub SetBypassProperty()
...
End Function
ไปวางใน Module ตามข้อ 3
5. จากนั้นคลิกเอา Cursor ไว้ที่หน้าแถวแรก
6. คลิกแถบเมนู Run > Run Sub/Userform หรือ กด F5 ก็ได้ ข้อนี้เป็นการ Add the CreateProperty method เพิ่มเข้าไปใน the Properties collection
7. ทดลองเปิดใช้งานได้เลย จะปรากฎว่าคุณกด Shift ไว้ก็ไม่มีผลอะไร
ท้ายบทความนี้ มีไฟล์ตัวอย่างให้ดาวน์โหลดไปดูครับ ถ้านำวิธีการดังกล่าวไปใช้แล้ว กลับมาคุยให้ฟังบ้างนะครับ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น