ปฏิทินสำหรับ ms access

กันยายน 23, 2010

ms access มี ปฏิทิน ซึ่งเป็น ActiveX ให้เลือกใช้ตามสะดวก แต่ปัญหาก็คือ เมื่อมีการเปลี่ยนรุ่น เช่น จาก ms access 2003 หรือ 2007 เป็น access 2010 เจ้า activeX จะก่อปัญหา มี error ขึ้นว่า No object in this control. ดังนั้น จึงควรใช้ ปฏิทิน ที่เป็นฟอร์มธรรมดา ดีกว่า

Allen Browne เขียนปฏิทินไว้ให้แล้วที่ http://allenbrowne.com/ser-51.html สามารถนำมาใช้ได้เลย สามารถปรับฟอร์ม frmCalendar เปลี่ยนภาษาอังกฤษให้เป็นไทยได้ เช่น ชื่อย่อวันในรอบสัปดาห์ เป็นต้น ข้อดีอีกอย่างคือ วัน เดือน ปี ใช้จากในเครื่อง จึงเห็นเป็นภาษาไทย และปี เป็นปี พุทธศักราช


การยกเลิก keypress event ใน ms access

กันยายน 23, 2010

ในกรณีที่มีกล่องข้อความ (text box) ให้เติมข้อความ แต่ไม่ต้องการให้คนพิมพ์เข้าโดยตรง ให้คลิกปุ่มเพื่อเลือกวันที่ จากปฏิทิน เท่านั้น เมือผู้ใช้กดแป้นที่กล่องข้อความเพื่อจะพิมพ์ข้อความ ก็ให้แสดง Message และยกเลิกตัวอักษรที่เพิ่งพิมพ์ไปในกล่องข้อความ

ให้เลือกเขียน event procedure ที่ on keypress ของกล่องข้อความนั้น (ไม่ใช้ on keyDown เพราะ on keypress จะเกิดเมื่อกดแป้นและปล่อย ภายในเวลาประมาณ 500 ms แต่ถ้ากดค้างไว้จะเกิด on keyDown event)

การยกเลิก keyPress event ไม่สามารถทำได้ใน event procedure ต้องสร้าง macro และเลือก cancelEvent ใน Macro ที่สร้างขึ้น จากนั้น จึงมาเรียกใช้ใน Event procedure อีกครั้งหนึ่ง

ตัวอย่าง

สมมตว่า text box ชื่อ txtCalendar บนฟอร์ม frmMyForm และไม่ต้องการให้พิมพ์ ถ้ากดแป้นให้มี Message ขึ้น และให้ยกเลิกตัวอักษรใด ๆ ที่พิมพ์ลงไป ทำดังนี้

1. สร้าง Macro โดยไปที่ Objects Macro > New
2. ที่ Action เลือก CancelEvent และปิด Macro โดยตั้งชื่อ เช่น myCancelEvent
3. กลับมาที่ฟอร์ม frmMyForm และคลิกที่ txtCalendar และเปิด property
4. เลือก Event และเลือก on keypress และสร้าง Code ดังนี้

Private Sub txtCalendar_KeyPress(KeyAscii As Integer)
MsgBox "กรุณาใช้ปุ่มเลือกวันที่", vbInformation, "โปรแกรมธนาคารโรงเรียน"
DoCmd.RunMacro "myCancelEvent"
End Sub


การใช้ combobox กับ ฟอร์มย่อย (subform)

กรกฎาคม 22, 2010

เมื่อต้องการสร้าง combobox บนฟอร์มหลัก และให้เรียกข้อมูลที่ต้องการมาปรากฏบนฟอร์มย่อย (subform) ให้ทำดังนี้

  1. สร้างฟอร์มหลัก และสร้าง combobox บนฟอร์มหลัก
  2. สร้างแบบสอบถาม Query นำข้อมูลที่จะแสดงบนฟอร์มย่อย และที่ฟิลด์ที่เป็น primary Key ให้เขียนโค้ดเงื่อนไขจาก combobox บนฟอร์มหลัก เช่น
    forms!ชื่อฟอร์มหลัก!ชื่อcombobox

  3. สร้างฟอร์มย่อย subform ให้ใช้ข้อมูลจากแบบสอบถามที่สร้าง
  4. ที่ Event After Update ของ commbobox ให้เขียนโค้ดเพื่อให้ปรับข้อมุลบนฟอร์มย่อย เช่น
    Me!ชื่อฟอร์มย่อย.Form.Requery


GroupBy Month ใน Query

กรกฎาคม 21, 2010

ในกรณีที่มีการรับจ่ายเงินกันทุกวัน และจำเป็นต้องสรุปยอดเป็นรายเดือน นอกจากจะสรุปในรายงานของ MS Acess แล้ว เรายังสามารถสรุปได้จาก Query โดยใช้ GroupBy ได้ โดยใช้ฟังกชั่น dateSerial

สมมุติว่า เรามีข้อมูลดังนี้

เราใช้ DateSerial เพื่อจัดกลุ่มวันที่ในฟิลด์ visitDate เสียใหม่ และสั่ง GroupBy ในฟิลด์จำนวนเงิน ดังภาพ

ฟังก์ชั่น DateSerial() มีรูปแบบการใช้คือ
DateSerial ( year, month, day )

จากตัวอย่างข้างต้น เรากำหนดให้ทุกวัน เป็นวันที่ 1 เพื่อประโยชน์ในการจัดกลุ่ม

จะได้ยอดรวมอย่างรวดเร็ว

แต่ถ้าต้องการรายละเอียด ให้ไปใช้ Report และจัดกลุ่มรายเดือน จะดีกว่า เพราะสามารถมีรายละเอียดอื่น ๆ เช่น ชื่อคน เป็นต้น รวมอยู่ในรายงานได้


การอ้างอิงไฟล์ภาพในตาราง MS Access

กรกฎาคม 20, 2010

การนำภาพมาใช้ในโปรแกรม MS Access มักจะใช้การเก็บที่อยู่ หรือ path ของภาพนั้น ๆ ในตาราง แล้วนำมาอ้างถึงในฟอร์ม หรือ รายงาน ที่ต้องการให้แสดงภาพ

ปัญหาคือ เราต้องระบุ path ของไฟล์ภาพ โดยระบุชื่อไดรฟ์ และห้องที่เก็บ เช่น c:\mypic\tjk.jpg ซึ่งทำให้เมื่อมีการย้ายไฟล์ mdb จะทำให้ลิงค์ต่าง ๆ เสียไปหมด ครั้นจะอ้างอิงแบบ relational คือเอาห้อง mypic ไว้ในห้องที่เก็บฐานข้อมูลแล้วอ้างชื่อห้อง เช่น mypic\tjk.jpg ก็ไม่สามารถทำได้

การแก้ปัญหา คือการใช้ตัวแปรระบบของ MS Access ให้หาที่อยู่ของไฟล์ฐานข้อมูล แล้วนำมาต่อด้วยห้องและชื่อไฟล์ แต่ทั้งนี้ห้องที่เก็บไฟล์ต้องอยู่ในห้องเดียวกัน หรือ ห้องย่อยภายใต้ห้องที่เก็บฐานข้อมูล

ตัวแปรที่ว่าคือ currentproject.path

ถ้ากด Ctrl+G และพิมพ์
? currnetproject.path แล้วกด enter จะเห็นว่าโปรแกรมระบุชื่อ path ที่ฐานข้อมูลอยู่ เช่น แต่จะไม่มีเครื่องหมาย \ ต่อท้าย เช่น
c:\myDatabase (myDatabase คือชื่อห้อง หรือ folder ของไฟล์ฐานข้อมูลที่กำลังเปิดใช้งานอยู่)

ดังนั้น ในตารางที่เก็บที่อยู่ของไฟล์ควรขึ้นต้นด้วย เครื่องหมาย \ เช่น
\mypic\tjk

วิธีนำภาพมาแสดงมีดังนี้
1. ให้สร้างตารางและสร้างฟิลด์เก็บข้อมูลของภาพ
2. กรอก path ของภาพ และชื่อภาพ เช่น \mypic\tjk.jpg
3. สร้างฟอร์ม และนำฟิลด์ของตารางนั้นทั้งหมดมาวางบนฟอร์ม
4.ลาก image control มาวาง เมื่อถูกถามหาไฟล์ภาพ ให้ระบุภาพใดก็ได้เอาไว้ก่อน

4. ที่ Event On Current ของฟอร์ม ให้ระบุว่า
Private Sub Form_Current()
On Error Resume Next
Me.imageFrame.Picture = CurrentProject.Path & Me.imgPath
End Sub
- ImageFrame เป็นชื่อ image control
- imgPath เป็นชื่อฟิลด์ในตารางที่เก็บข้อมูล path และชื่อไฟล์ภาพ

เท่านี้ก็จะได้ภาพ และสามารถย้ายห้องฐานข้อมูลไปที่อื่น ๆ ได้ โดยที่ลิงค์ของภาพยังอยู่ครบถ้วนสมบูรณ์


Crosstab Query ใน MS Access คืออะไร

กรกฎาคม 18, 2010

Crosstab Query เป็นการสรุปข้อมูลใหม่ โดยใช้ข้อมูลจากในตารางหรือแบบสอบถาม (Query) และนำมาจัดเรียงเสียใหม่ เพื่อให้การสรุปข้อมูลดูได้ง่ายขึ้นตามความต้องการ และมักจะมีการคำนวณ เช่น การรวม การนับจำนวนด้วย ตัวอย่าง เช่น ถ้าเรามีข้อมูลในลักษณะข้างล่างนี้

จะเห็นว่ามีคนมาใช้บริการในแต่ละวันหลายคน และมีการชำระเงินกันทุกคน บางคนมาหลายวัน เช่น พรศักดิ์ มาใช้บริการทั้งสามวัน คือ วันที่ 1/10/2550 2/10/2550 และวันที่ 3/10/2550 แต่มีบางคนที่มาใช้บริการเพียงวันเดียว เช่น สมปอง มาใช้บริการเฉพาะวันที่ 2/10/2550 เท่านั้น

ถ้าเราต้องการดูข้อมูลเสียใหม่ โดยต้องการเปรียบเทียบว่าแต่ละคนจ่ายเงินมาก-น้อยเพียงใด และในแต่ละวันจ่ายคนละเท่าไร ดังภาพข้างล่างนี้

จากภาพข้างบน จะเห็นว่า มีการวิเคราะห์ข้อมูลเดิมใหม่ โดยเป็นการดูข้อมูลของคนแต่ละคนว่า จ่ายเงินรวมทั้งหมดเท่าไร และในแต่ละวันจ่ายเท่าไร การสรุปข้อมูลในลักษณะนี้ เราควรใช้ Crosstab Query การใช้ Crosstab Query จะช่วยให้การวิเคราะห์ข้อมูลดูง่ายขึ้น ซึ่งสามารถทำได้รวดเร็ว โดยใช้ ตัวช่วย หรือ Wizard


คำสั่ง sql เลือกข้อมูลไม่ซ้ำกับข้อมูลในตารางอื่น

กุมภาพันธ์ 14, 2010

สมมุติว่า ต้องการเขียนคำสั่ง sql ใน VBA ให้เลือกข้อมูลจากตารางหนึ่ง โดยมีเงื่อนไขว่า ต้องไม่ซ้ำกับข้อมูลที่มีอยู่แล้วในอีกตารางหนึ่ง จะทำอย่างไร

เช่นต้องการสร้าง combo box ให้ผู้ใช้สามารถเลือกข้อมูลจากตารางสมาชิกทั้งหมด เพื่อสมัครเป็นสมาชิกอีกประเภทหนึ่ง แต่ถ้าสมาชิกที่สมัครแล้ว ก็จะไม่เอาชื่อมาแสดงใน combox ให้เลือก

ถ้าเป็นอย่างนี้ การเลือกข้อมูลจากตารางสมาชิกทั้งหมด จะต้องไม่ซ้ำกับข้อมูลในตารางสมาชิกที่จะสมัคร

สมมุติว่า สมาชิกกองทุนหมู่บ้าน มีสิทธิสมัครสมาชิกสวัสดิการชุมชน ต้องการสร้าง cbobox ให้เลือกข้อมูลจากตารางสมาชิกกองทุนหมู่บ้าน เพื่อสมัครเป็นสมาชิกสวัสดิการชุมชน แต่สมาชิกกองทุนหมู่บ้านที่สมัครเป็นสมาชิกสวัสดิการชุมชนแล้ว จะต้องไม่มีชื่อใน combobox

นั่นคือ การเลือกข้อมูลจากตารางสมาชิกกองทุนหมู่บ้าน ต้องไม่ซ้ำกับข้อมูลที่มีอยู่แล้วในตารางสมาชิกสวัสดิการชุมชน

ทีนี้เมื่อพิจารณาลักษณะเงื่อนไขที่ต้องการจะเห็นว่า ข้อมูลที่จะเลือกจากตารางกองทุนหมู่บ้าน คือ ต้องเป็นข้อมูลที่ไม่มี ในตารางสมาชิกสวัสดิการชุมชน

สมมุติว่า ตารางสมาชิกกองทุนหมู่บ้าน ชื่อ tblCustomers และมีข้อมูลดังนี้

cusID | custName
111 | สมหมาย
112 | สมศักดิ์
113 | สมศรี
114 | สมทรง
115 | สมบัติ
116 | สมถวิล
117 | สมนึก

ตารางสมาชิกสวัสดิการชุมชน ชื่อ tblWFMembers และมีข้อมูล ดังนี้

memID | custName
112 | สมศักดิ์
113 | สมศรี
116 | สมถวิล
117 | สมนึก

ข้อมูลที่จะนำมาให้เลือกใน combo box จะมีแค่

111 | สมหมาย
114 | สมทรง
115 | สมบัติ

เท่านั้น

วิธีการ

ให้เรากำหนดเงื่อนไขคือ กำหนด field ที่ ไม่มี (NOT IN) ในตาราง tblWFMembers โดยไปกำหนดใน WHERE clause เช่น

SELECT * FROM tblCustomers WHERE tblCustomer.custID not in (field memID ในตาราง tblWFMembers)

เขียนเป็นคำสั่ง เต็ม ๆ ได้ ดังนี้

SELECT * FROM tblCustomers WHERE tblCustomer.custID not in (SELECT memID FROM tblWFMembers)

ให้นำประโยค SQL นี้ ไปใส่ใน Row Source ของ combobox ที่สร้างขึ้น (อย่าลืมกำหนด Row Source Type เป็น Table/Query ด้วยนะ) ก็จะได้ข้อมูลให้เลือกตามต้องการ


การตรวจว่า report กำลังเปิดอยู่หรือไม่

กุมภาพันธ์ 13, 2010

เมื่อสั่งเปิด Report จาก Form ถ้าสั่งเปิด Report ไปครั้งหนึ่งแล้ว และจะสั่งใหม่อีก Report ที่ถูกเปิดไว้จะไม่ปิด และ เมื่อสั่งเปิดใหม่ ข้อมูลก็จะเป็นข้อมูลเก่า จึงดูเหมือนว่าไม่มีอะไรเปลี่ยน ทั้ง ๆ ที่ข้อมูลอาจจะเปลี่ยน

ดังนั้น การสั่งจากฟอร์ม ให้เปิดรายงาน จึงควรตรวจสอบก่อนว่า ฟอร์ม กำลังเปิดอยู่หรือไม่ ถ้ากำลังเปิดอยู่ก็สั่งให้ปิดเสีย

การตรวจว่า Report กำลังเปิดอยุ่หรือไม่ ให้ใช้คำสั่ง SysCmd เช่น

ถ้าต้องการตรวจสอบว่า รายงานชื่อ rptWF_viewPayment กำลังเปิดอยุ่หรือไม่ ถ้าเปิด ก็ให้ปิดเสีย ใช้คำสั่ง ดังนี้

If SysCmd(acSysCmdGetObjectState, acReport, “rptWF_viewPayment”) <> 0 Then
DoCmd.Close acReport, “rptWF_viewPayment”, acSaveNo
End If

สำหรับ object อาจจะใช้คำหรือตัวเลข ต่อไปนี้
Table acTable 0
Query acQuery 1
Form acForm 2
Report acReport 3
Macro acMacro 4
Module acModule 5
Data Access Page acDataAccessPage 6

ค่าที่ส่งออกมา เมื่อใช้ cmdSys() ตรวจสอบสถานะของ Object มีดังนี้
0 – object is closed or doesn’t exist
1 – Open acObjStateOpen
2 – Changed but not saved
acObjStateDirty
4 – New acObjStateNew

อ่านเพิ่มเติมที่

http://blogs.techrepublic.com.com/msoffice/?p=387

และที่

http://msdn.microsoft.com/en-us/library/aa221609(office.11).aspx

Access
SysCmd Method [Access 2003 VBA Language Reference]
You can use the SysCmd method to, display a progress meter or optional specified text in the status bar, return information about Microsoft Access and its associated files, or return the state of a specified database object (to indicate whether the object is open, is a new object, or has been changed but not saved). Variant.

expression.SysCmd(Action, Argument2, Argument3)

expression Required. An expression that returns one of the objects in the Applies To list.

AcSysCmdAction
AcSysCmdAction can be one of these AcSysCmdAction constants.
acSysCmdAccessDir. Returns the name of the directory where Msaccess.exe is located.
acSysCmdAccessVer. Returns the version number of Microsoft Access.
acSysCmdClearHelpTopic
acSysCmdClearStatus. The following constant provides information on the state of a database object.
acSysCmdGetObjectState. Returns the state of the specified database object. You must specify argument1 and argument2 when you use this action value.
acSysCmdGetWorkgroupFile. Returns the path to the workgroup file (System.mdw).
acSysCmdIniFile. Returns the name of the .ini file associated with Microsoft Access.
acSysCmdInitMeter. Initializes the progress meter. You must specify the argument1 and argument2 arguments when you use this action.
acSysCmdProfile. Returns the /profile setting specified by the user when starting Microsoft Access from the command line.
acSysCmdRemoveMeter. Removes the progress meter.
acSysCmdRuntime. Returns True (–1) if a run-time version of Microsoft Access is running.
acSysCmdSetStatus. Sets the status bar text to the text argument.
acSysCmdUpdateMeter. Updates the progress meter with the specified value. You must specify the text argument when you use this action.

Argument2 Optional Variant. A string expression identifying the text to be displayed left-aligned in the status bar. This argument is required when the action argument is acSysCmdInitMeter, acSysCmdUpdateMeter, or acSysCmdSetStatus; this argument isn’t valid for other action argument values.

acObjectType
acTable
acQuery
acForm
acReport

acMacro

acModule

acDataAccessPage

acDefault

acDiagram

acServerView

acStoreProcedure

This argument isn’t valid for other action argument values.

Argument3 Optional Variant. A numeric expression that controls the display of the progress meter. This argument is required when the action argument is acSysCmdInitMeter; this argument isn’t valid for other action argument values.

Note When you specify the acSysCmdGetObjectState value for the Action parameter, then you must specify the name of the database object.

Remarks
For example, if you are building a custom wizard that creates a new form, you can use the SysCmd method to display a progress meter indicating the progress of your wizard as it constructs the form.

By calling the SysCmd method with the various progress meter actions, you can display a progress meter in the status bar for an operation that has a known duration or number of steps, and update it to indicate the progress of the operation.

To display a progress meter in the status bar, you must first call the SysCmd method with the acSysCmdInitMeter action argument, and the text and value arguments. When the action argument is acSysCmdInitMeter, the value argument is the maximum value of the meter, or 100 percent.

To update the meter to show the progress of the operation, call the SysCmd method with the acSysCmdUpdateMeter action argument and the value argument. When the action argument is acSysCmdUpdateMeter, the SysCmd method uses the value argument to calculate the percentage displayed by the meter. For example, if you set the maximum value to 200 and then update the meter with a value of 100, the progress meter will be half-filled.

You can also change the text that’s displayed in the status bar by calling the SysCmd method with the acSysCmdSetStatus action argument and the text argument. For example, during a sort you might change the text to “Sorting…”. When the sort is complete, you would reset the status bar by removing the text. The text argument can contain approximately 80 characters. Because the status bar text is displayed by using a proportional font, the actual number of characters you can display is determined by the total width of all the characters specified by the text argument.

As you increase the width of the status bar text, you decrease the length of the meter. If the text is longer than the status bar and the action argument is acSysCmdInitMeter, the SysCmd method ignores the text and doesn’t display anything in the status bar. If the text is longer than the status bar and the action argument is acSysCmdSetStatus, the SysCmd method truncates the text to fit the status bar.

You can’t set the status bar text to a zero-length string (” “). If you want to remove the existing text from the status bar, set the text argument to a single space. The following examples illustrate ways to remove the text from the status bar:

varReturn = SysCmd(acSysCmdInitMeter, ” “, 100)
varReturn = SysCmd(acSysCmdSetStatus, ” “)
If the progress meter is already displayed when you set the text by calling the SysCmd method with the acSysCmdSetStatus action argument, the SysCmd method automatically removes the meter.

Call the SysCmd method with other actions to determine system information about Microsoft Access, including which version number of Microsoft Access is running, whether it is a run-time version, the location of the Microsoft Access executable file, the setting for the /profile argument specified in the command line, and the name of an .ini file associated with Microsoft Access.

Note Both general and customized settings for Microsoft Access are now stored in the Windows Registry, so you probably won’t need an .ini file with your Microsoft Access application. The acSysCmdIniFile action argument exists for compatibility with earlier versions of Microsoft Access.

Call the SysCmd method with the acSysCmdGetObjectState action argument and the objecttype and objectname arguments to return the state of a specified database object. An object can be in one of four possible states: not open or nonexistent, open, new, or changed but not saved.

For example, if you are designing a wizard that inserts a new field in a table, you need to determine whether the structure of the table has been changed but not yet saved, so that you can save it before modifying its structure. You can check the value returned by the SysCmd method to determine the state of the table.

The SysCmd method with the acSysCmdGetObjectState action argument can return any combination of the following constants.

Constant State of database object Value
acObjStateOpen Open 1
acObjStateDirty Changed but not saved 2
acObjStateNew New 4

Note If the object referred to by the objectname argument is either not open or doesn’t exist, the SysCmd method returns a value of zero.

The following code can be used to enable the use of your ActiveX control in expressions when the ActiveX control has been added to a form:

SysCmd 14, “”
Note Replace with the globally unique identifier (GUID) that identifies the ActiveX control that you want to enable in expressions.

Note You cannot remove an ActiveX control once it has been added to the list of allowed controls.


การสั่งปุ่มเพิ่ม record บน record navigation ของฟอร์มของ MS Access ไม่ให้ทำงาน

กุมภาพันธ์ 6, 2010

วันนี้นั่งงงอยู่ตั้งนานว่า เราจะสั่งไม่ให้ปุ่ม เพิ่มข้อมูลใหม่ หรือ append new record (>*) ไม่ให้ทำงานได้อย่างไร เพราะจุดประสงค์คือ ต้องการให้ดู แก้ไขข้อมูลในตารางได้ แต่ไม่ต้องการให้เพิ่มข้อมูล จึงต้องสั่งให้ปุ่มนี้ไม่ทำงาน
ค้นหาในเว็บอยู่นาน ก็เจอ ง่ายนิดเดียวเอง

เพียงไปที่คุณสมบัติของฟอร์ม และกำหนดให้ Allow additions มีค่าเป็น No ก็เสร็จแค่นี้เอง

เว็บที่ว่าคือ

http://www.bigresource.com/Tracker/Track-ms_access-1533jKNc/


การปรับริบบิ้น ใน MS Access 2007

กันยายน 12, 2009

ริบบิ้นใน Access 2007 ต่างจากเมนูใน Access 2003 ของใหม่ใช้ XML ควบคุม
ถ้าจะเปลี่ยนแปลง มีวิธี 2-3 วิธี แต่ที่รู้สึกว่าง่ายกว่าพวก คือ สร้างตารางใหม่ เก็บข้อมูล XML แล้วเรียกใช้งาน

วิธีการ ดูที่

http://office.microsoft.com/th-th/access/HA102114151054.aspx


Follow

Get every new post delivered to your Inbox.