โปรแกรมนี้เป็นโปรแกรมที่ผมใช้มานานมากแล้ว ถือว่าดีมากๆเลยทีเดียวแล้วก็ยังฟรีอีกด้วย วันนี้ผมอยากแนะนำเพื่อนคนนี้ให้รู้จักกัน ก่อนอื่นต้อง Download มาก่อนครับ แตกไฟล์แล้วก็จะได้
Showing posts with label Programing. Show all posts
Showing posts with label Programing. Show all posts
29 November 2011
วิธีการทำ Auto Update โปรแกรมที่พัฒนาจาก MS Access
วิธีการทำ Auto Update โปรแกรมที่พัฒนาจาก MS Access ทุกเวอร์ชั่น ด้วย Auto FE Update
โปรแกรมนี้เป็นโปรแกรมที่ผมใช้มานานมากแล้ว ถือว่าดีมากๆเลยทีเดียวแล้วก็ยังฟรีอีกด้วย วันนี้ผมอยากแนะนำเพื่อนคนนี้ให้รู้จักกัน ก่อนอื่นต้อง Download มาก่อนครับ แตกไฟล์แล้วก็จะได้
โปรแกรมนี้เป็นโปรแกรมที่ผมใช้มานานมากแล้ว ถือว่าดีมากๆเลยทีเดียวแล้วก็ยังฟรีอีกด้วย วันนี้ผมอยากแนะนำเพื่อนคนนี้ให้รู้จักกัน ก่อนอื่นต้อง Download มาก่อนครับ แตกไฟล์แล้วก็จะได้
21 November 2011
การใข้งาน Progress bar ด้วย Background Worker ใน Vb.Net
การใข้งาน Progress bar ด้วย Background Worker ใน Vb.Net
โปรแกรมเมอร์หลายๆคนอาจเคยพบปัญหา เวลาที่เราดึงข้อมูลจำนวนมากมาแสดง หรือ ประมวลผลหน้าจอโปรแกรมจะ ค้าง หรือ Not Respond ไประยะหนึ่งจนมันประมวลผลเรียบร้อย ทำให้ User ไม่เข้าใจว่าโปรแกรมเรา Hang หรือทำงานอยู่และไม่จบด้วยการไป End Task มัน
17 October 2011
Unable to convert MySQL date/time value to System.DateTime
ในกรณีนี้ผมใช้ VB.Net คับ เมื่อ Query ข้อมูลออกมาแสดงผล จะขึ้น Error ว่า Unable to convert MySQL date/time value to System.DateTime ดังนั้นวิธีแก้ที่ได้ผลก็คือให้เราเพิ่ม Option
allow zero datetime=true; ใน Connection String ของเรา
เป็น
MySQLConnectionStr = "server=" & MySQLServer & ";user=" & MySQLUser & ";database=" & MySQLDatabase & ";port=" & MySQLPort & ";password=" & Crypto.Decrypt(MySQLPassword, "sys11266") & ";allow zero datetime=true;"
จากนั้นโปรแกรมที่เราเขียนก็จะแสดงผลได้ถูกต้องครับ
ในกรณีนี้ผมใช้ VB.Net คับ เมื่อ Query ข้อมูลออกมาแสดงผล จะขึ้น Error ว่า Unable to convert MySQL date/time value to System.DateTime ดังนั้นวิธีแก้ที่ได้ผลก็คือให้เราเพิ่ม Option
allow zero datetime=true; ใน Connection String ของเรา
เป็น
MySQLConnectionStr = "server=" & MySQLServer & ";user=" & MySQLUser & ";database=" & MySQLDatabase & ";port=" & MySQLPort & ";password=" & Crypto.Decrypt(MySQLPassword, "sys11266") & ";allow zero datetime=true;"
จากนั้นโปรแกรมที่เราเขียนก็จะแสดงผลได้ถูกต้องครับ
17 February 2011
การใส่ Row number ใน Devexpress Xtragrid สำรหับ VB.NET
สำหรับคนที่หันมาใช้ Third Party Component แล้ว Devexpress เป็นตัวเลือกหนึ่งที่น่าใช้ วันนี้ผมขอแนะนำเทคนิคเล็กๆน้อยที่จะทำให้เราสามารถ ลำดับที่ให้กับ XtraGrid ของเรา เริ่มกันเลยนะครับ
Labels:
Devexpress,
Number,
Programing,
Row,
Row number,
TIPS,
VB.NET,
Xtragrid
03 February 2011
การสร้าง Messagebox ยืนยันการออกจากโปรแกรม เมื่อกดปิดฟอร์ม
โปรแกรมเมอร์มือใหม่คงมีปัญหาว่า หาก user ไม่กดปุ่มหรือเมนูเพื่อออกจากระบบ แต่กดปุ่มสีแดงรูปตัว X เพื่อปิดฟอร์มเลยจะไม่มี message แจ้งเตือนเพื่อยืนยันการปิดฟอร์ม วันนี้ผมเลยนำเอาเทคนิคนี้มาฝากันครับ โดยให้เลือกที่ event FormClosing ครับ
Labels:
Confirm Close,
Formclosing,
Programing,
TIPS,
VB.NET
21 July 2010
.NET Componant Gradient Panel
PN_GradientPanel V.1.0
เนื่องจากผมเห็นว่า Panel มีการแสดงผลของสีที่พื้นๆเกินไป ทำให้ผมอยากจะเพิ่มความสามารถของมัน ให้แสดงผลแบบ Grdient ได้ผมจึงลอง พัฒนาตัวนี้ขึ้นมาใช้งาน แต่ก็อยากแบ่งปันคนอื่นๆด้วย นะครับ ผมแนบ Demo มาให้ดูด้วยเพื่อใครสนใจจะทดลองนำไปใช้ดู ตัวนี้ผมพัฒนาบน VS2008 นะครับ แล้วก็เอามาแจกให้ Download ไปใช้โดยไม่มีค่าใช้จ่ายใดๆ ครับ
Property ประกอบไปด้วย
GradientColor1 : ใช้สำหรับกำหนดสี ที่ 1
GradientColor2 : ใช้สำหรับกำหนดสี ที่ 2
GradientFont : ใช้สำหรับกำหนด ชนิด และขนาด ของ ตัวหนังสือของข้อความใน Title (Label)
GradientForeColor : ใช้สำหรับกำหนดสีตัวหนังสือ ของข้อความใน Title (Label)
GradientHeaderText : ใช้สำหรับกำหนด ข้อความใน Title (Label)
GradientLeftToRight : ใช้สำหรับกำหนดรูปแบบของ Gradient ถ้า เป็น Yes ก็จะไล่สีจากซ้ายไปขวา ถ้าเป็น No ก็จะไล่สีจากบนลงล่าง


Download Demo
Download DLL
เนื่องจากผมเห็นว่า Panel มีการแสดงผลของสีที่พื้นๆเกินไป ทำให้ผมอยากจะเพิ่มความสามารถของมัน ให้แสดงผลแบบ Grdient ได้ผมจึงลอง พัฒนาตัวนี้ขึ้นมาใช้งาน แต่ก็อยากแบ่งปันคนอื่นๆด้วย นะครับ ผมแนบ Demo มาให้ดูด้วยเพื่อใครสนใจจะทดลองนำไปใช้ดู ตัวนี้ผมพัฒนาบน VS2008 นะครับ แล้วก็เอามาแจกให้ Download ไปใช้โดยไม่มีค่าใช้จ่ายใดๆ ครับ
Property ประกอบไปด้วย
GradientColor1 : ใช้สำหรับกำหนดสี ที่ 1
GradientColor2 : ใช้สำหรับกำหนดสี ที่ 2
GradientFont : ใช้สำหรับกำหนด ชนิด และขนาด ของ ตัวหนังสือของข้อความใน Title (Label)
GradientForeColor : ใช้สำหรับกำหนดสีตัวหนังสือ ของข้อความใน Title (Label)
GradientHeaderText : ใช้สำหรับกำหนด ข้อความใน Title (Label)
GradientLeftToRight : ใช้สำหรับกำหนดรูปแบบของ Gradient ถ้า เป็น Yes ก็จะไล่สีจากซ้ายไปขวา ถ้าเป็น No ก็จะไล่สีจากบนลงล่าง


Download Demo
Download DLL
05 September 2009
No value given for one or more required paramer
วันนี้ผมเขียนโปรแกรม สำหรับการทำให้รายการชื่ออำเภอใน Combobox ของ อำเภอ เปลี่ยนตาม combobox ของจังหวัด โดยเขียนให้
Form load แสดงรายการ ตำบล อำเภอ จังหวัด ลงใน combobox และ ใน SelectedIndexChanged ของแต่ละอันก็ให้ Refresh ข้อมูลให้เปลี่ยนแปลงตาม จังหวัด
ปัญหาอยู่ที่ Event SelectedIndexChanged โปรแกรมแสดง Error "No value given for one or more required paramer"
เพราะ Event SelectedIndexChanged จะทำงานเมื่อ Combobox เปลี่ยนค่าที่เลือกไว้
ผมหาวิธีแก้อยู่และนึกเท่าไหร่ก็นึกไม่ออก จะ Search ก็ไม่รู้ว่าจะใช้คำจำกัดความว่าอย่างไร ก็เลยลองใส่ไปตรงๆ ว่า "Form_Load combobox SelectedIndexChanged" และแล้วผมก็เจอในเว็บไซต์ของ Microsoft วิธีแก้นั้นง่ายจนคิดไม่ถึง นั่นคือ ใส่ Flag ลงไปแค่นั้นเอง
ใน Project ของผมผมใส่
Dim Cboonload As Boolean = True
เมื่อ Event Form_Load เสร็จสิ้น ผมก็ Flag คืน ไปว่า Cboonload As Boolean = False
และใน Event SelectedIndexChanged ก็ให้เช็คว่า If Cboonload = True Then Exit Sub เท่่านั้นเอง
Ref : http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/cbe96c98-beef-4abc-a268-3956064ece1c/
Form load แสดงรายการ ตำบล อำเภอ จังหวัด ลงใน combobox และ ใน SelectedIndexChanged ของแต่ละอันก็ให้ Refresh ข้อมูลให้เปลี่ยนแปลงตาม จังหวัด
ปัญหาอยู่ที่ Event SelectedIndexChanged โปรแกรมแสดง Error "No value given for one or more required paramer"
เพราะ Event SelectedIndexChanged จะทำงานเมื่อ Combobox เปลี่ยนค่าที่เลือกไว้
ผมหาวิธีแก้อยู่และนึกเท่าไหร่ก็นึกไม่ออก จะ Search ก็ไม่รู้ว่าจะใช้คำจำกัดความว่าอย่างไร ก็เลยลองใส่ไปตรงๆ ว่า "Form_Load combobox SelectedIndexChanged" และแล้วผมก็เจอในเว็บไซต์ของ Microsoft วิธีแก้นั้นง่ายจนคิดไม่ถึง นั่นคือ ใส่ Flag ลงไปแค่นั้นเอง
ใน Project ของผมผมใส่
Dim Cboonload As Boolean = True
เมื่อ Event Form_Load เสร็จสิ้น ผมก็ Flag คืน ไปว่า Cboonload As Boolean = False
และใน Event SelectedIndexChanged ก็ให้เช็คว่า If Cboonload = True Then Exit Sub เท่่านั้นเอง
Ref : http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/cbe96c98-beef-4abc-a268-3956064ece1c/
24 August 2009
MySQL Error Illegal mix of collations

ปัญหาเกิดขึ้นตอนที่ใส่เงื่อนไขที่เป็นภาษาไทย เช่น = '"ข้าราชการ"' ครับ
อาจจะยังไม่ใช่วิธีแก้ปัญหาที่ถูกต้องนักแต่ที่ผมหาได้คือ ต้องเปลี่ยน Character set: เป็น utf8 และ collation ของ Field ดังกล่าวให้เป็น
utf8_unicode_ci

อ่านบทความอื่นๆเพิ่มเติม
ปัญหาการใช้เงื่อนไขวันที่ใน MS Access
| ||
| อ่านบทความอื่นๆ | | |
เมื่อ DTPicker ใน Access ขึ้น Runtime error '2763'
มี Error message Runtime error '2763' dtpicker returned the error : Property is read-only ขึ้นตอนพยายามจะทำให้ตัว Date Picker
ใน MS Access ตั้งค่า Default เป็น วันที่ปัจจุบัน


วิธีแก้นะครับ คือเราต้องตั้งค่า Checkbox ของ DTPicker ให้เป็น YES ครับ (อาจจะดูไม่มืออาชีพแต่ก็แก้ขัดได้) แล้วเวลาเราเปิดฟอร์มามันจะเป็น วันที่ปัจจุบันอัตโนมัติครับ ส่วนตัว checkbox แค่เลือกวันที่มันก็จะ check ให้อัตโนมัติครับ



อ่านบทความอื่นๆ
ใน MS Access ตั้งค่า Default เป็น วันที่ปัจจุบัน
วิธีแก้นะครับ คือเราต้องตั้งค่า Checkbox ของ DTPicker ให้เป็น YES ครับ (อาจจะดูไม่มืออาชีพแต่ก็แก้ขัดได้) แล้วเวลาเราเปิดฟอร์มามันจะเป็น วันที่ปัจจุบันอัตโนมัติครับ ส่วนตัว checkbox แค่เลือกวันที่มันก็จะ check ให้อัตโนมัติครับ
อ่านบทความอื่นๆ
23 August 2009
การใช้งาน Common Dialog ใน VBA
ผมพยายามที่จะใช้ Save Dialog ใน VBA ของ Access แต่ไม่ได้ผล ผมจึงลอง Search ใน Google ดู ปรากฏว่าได้ผลดีเยี่ยมครับ ใคร ที่เจอปัญหาเดียวกัน copy ไปใช้ได้เลยนะครับ
ได้มาจากที่นี่ครับ ->เรื่อง How do select a file using the a dialog box http://www.tek-tips.com/faqs.cfm?fid=2484
เนื้อหานะครับ คือ ว่า เราจะต้องสร้าง Class Module ชื่อ clsCommonDialog ครับ จากนั้น copy ข้อความดังนี้ใส่ลงไปครับ
อ่านเพิ่มเรื่องอื่นๆได้ที่ www.piyanat.net
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CommonDialog class '
' '
' This module contains an interface to the Common '
' Dialog File Open/Save functions. It may be enhanced '
' for other Common Dialog functions at a future date. '
' '
' This object presents exactly the same interface as '
' the Microsoft Common Dialog 6.0 library from Visual '
' Basic 6.0 (comdlg32.dll). '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Values for the Flags property; multiple values can be ORed together.
' In versions of Access prior to Access 2000, comment or delete these
' and use the CommonDialogConstants module (Enum keyword is not
' valid in these versions).
Public Enum CmdlgOpenFlags
cdlOFNAllowMultiselect = &H200
cdlOFNCreatePrompt = &H2000
cdlOFNExplorer = &H80000
cdlOFNFileMustExist = &H1000
cdlOFNHideReadOnly = &H4
cdlOFNNoChangeDir = &H8
cdlOFNNoDereferenceLinks = &H100000
cdlOFNNoNetworkButton = &H20000 ' not documented for common dlg
cdlOFNNoReadOnlyReturn = &H8000
cdlOFNNoValidate = &H100
cdlOFNOverwritePrompt = &H2
cdlOFNPathMustExist = &H800
cdlOFNReadOnly = &H1
cdlOFNShowHelp = &H10
cdlOFNShareAware = &H4000
cdlOFNExtensionDifferent = &H400
End Enum
' Errors raised
Public Enum CmdlgErrors
cdlCancel = 32755 ' user pressed Cancel in dialog
End Enum
' Filter string used for the Open/Save dialog filters (the
' "Files of type" combo box). The string consists of a list of
' filter specs, each of which consists of a pair of elements.
' The first element of each spec is the description to appear
' in the combo box, and the second is a filter pattern. When
' the user selects the description, the corresponding pattern
' is used to filter the list of files in the file list box.
' A pipe character ("|") separates each element of the string.
' Example: "Database Files|*.mdb|All Files|*.*"
Public Filter As String
' Initial Filter to display. This sets/returns the index of the
' currently selected item in the filter combo box.
Public FilterIndex As Long
' Initial directory for the dialog to open in.
' Default = Current working directory.
Public InitDir As String
' Initial file name to populate the dialog with. Default = "".
' Returns the full path name of the selected file.
Public FileName As String
' Returns file name (without path) of file picked
Public FileTitle As String
' Title to appear on the dialog box.
Public DialogTitle As String
' Default extension to append to file if user didn't specify one.
Public DefaultExt As String
' Flags (see constant list) to be used.
' Returns cdlOFNDifferentExtension if extension present and not = DefaultExt
Public Flags As Long
' Maximum length of the file name to be returned
Public MaxFileSize As Integer
' Set to True to raise error 32755 if user cancels dialog box
Public CancelError As Boolean
' Constants used when raising errors
Private Const ErrSource = "MyComDlg.CommonDialog"
' Interface to Win32
Private Type W32_OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
lngFlags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As Long 'String
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As W32_OPENFILENAME) As Boolean
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (pOpenfilename As W32_OPENFILENAME) As Boolean
Private Sub Class_Initialize()
' Initialize the MaxFileSize to minimum, in case the user doesn't set it
MaxFileSize = 256
End Sub
Public Sub ShowOpen()
' Shows the Open dialog
Dim wofn As W32_OPENFILENAME
Dim intRet As Integer
OFN_to_WOFN wofn
On Error GoTo ShowOpen_Error
intRet = GetOpenFileName(wofn)
On Error GoTo 0
WOFN_to_OFN wofn
If (intRet = 0) And CancelError Then _
Err.Raise cdlCancel, ErrSource, "File open canceled by user"
Exit Sub
ShowOpen_Error:
WOFN_to_OFN wofn
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, _
Err.HelpContext
End Sub
Public Sub ShowSave()
' Shows the Save dialog
Dim wofn As W32_OPENFILENAME
Dim intRet As Integer
OFN_to_WOFN wofn
On Error GoTo ShowSave_Error
intRet = GetSaveFileName(wofn)
On Error GoTo 0
WOFN_to_OFN wofn
If (intRet = 0) And CancelError Then _
Err.Raise cdlCancel, ErrSource, "File save canceled by user"
Exit Sub
ShowSave_Error:
WOFN_to_OFN wofn
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, _
Err.HelpContext
End Sub
Private Sub OFN_to_WOFN(wofn As W32_OPENFILENAME)
' This sub converts from the Microsoft Access structure to the Win32 structure.
' Initialize some parts of the structure.
With wofn
.hwndOwner = Application.hWndAccessApp
.hInstance = 0
.lpstrCustomFilter = vbNullString
.nMaxCustrFilter = 0
.lpfnHook = 0
.lpTemplateName = 0
.lCustrData = 0
.lpstrFilter = ConvertFilterString(Filter)
.nFilterIndex = FilterIndex
If MaxFileSize < maxfilesize =" 256" maxfilesize =" Len(FileName)" nmaxfile =" MaxFileSize" lpstrfile =" FileName" nmaxfiletitle =" 260" lpstrfiletitle =" String(260," lpstrtitle =" DialogTitle" lpstrinitialdir =" InitDir" lpstrdefext =" DefaultExt" lngflags =" Flags" lstructsize =" Len(wofn)" filename =" Left$(.lpstrFile," filetitle =" Left$(.lpstrFileTitle," filterindex =" .nFilterIndex" flags =" .lngFlags" strfilter = "" intnum =" 0" intpos =" 1" intlastpos =" 1" intpos =" InStr(intLastPos,"> intLastPos) Then
strFilter = strFilter & Mid(strFilterIn, intLastPos, intPos - intLastPos) & vbNullChar
intNum = intNum + 1
intLastPos = intPos + 1
ElseIf (intPos = intLastPos) Then
intLastPos = intPos + 1
End If
Loop Until (intPos = 0)
' Get last string if it exists (assuming strFilterIn was not bar terminated).
intPos = Len(strFilterIn)
If (intPos >= intLastPos) Then
strFilter = strFilter & Mid(strFilterIn, intLastPos, intPos - intLastPos + 1) & vbNullChar
intNum = intNum + 1
End If
' Add *.* if there's no extension for the last string.
If intNum Mod 2 = 1 Then
strFilter = strFilter & "*.*" & vbNullChar
End If
' Add terminating NULL
ConvertFilterString = strFilter & vbNullChar
End Function
ส่วน code ตัวอย่างนะครับ
สำหรับ Open Dialog
Sample code:
Dim cmdlgOpenFile As New clsCommonDialog
Dim FileName As String 'full file name
Const clngFilterIndexAll = 5
cmdlgOpenFile.Filter = "Text Files (*.txt)|*.txt|DBF Files (DBF)|*.dbf|All Files (*.*)|*.*"
cmdlgOpenFile.FilterIndex = clngFilterIndexAll
'this is where the dialog opens
cmdlgOpenFile.ShowOpen
'returns your full file name.
FileName = cmdlgOpenFile.FileName
'hence no len, no name...
If Len(FileName) = 0 Then Exit Sub
สำหรับ Save Dialog
Dim cmdlgSaveFile As New clsCommonDialog
Dim FileName As String 'full file name
Const clngFilterIndexAll = 5
With cmdlgSaveFile
.FileName = "Claim.xls"
.Filter = "Excel Files (*.xls)|*.xls"
.FilterIndex = clngFilterIndexAll
.ShowSave
'returns your full file name.
FileName = .FileName
If Len(FileName) = 0 Then Exit Sub
End With
ได้มาจากที่นี่ครับ ->เรื่อง How do select a file using the a dialog box http://www.tek-tips.com/faqs.cfm?fid=2484
เนื้อหานะครับ คือ ว่า เราจะต้องสร้าง Class Module ชื่อ clsCommonDialog ครับ จากนั้น copy ข้อความดังนี้ใส่ลงไปครับ
อ่านเพิ่มเรื่องอื่นๆได้ที่ www.piyanat.net
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CommonDialog class '
' '
' This module contains an interface to the Common '
' Dialog File Open/Save functions. It may be enhanced '
' for other Common Dialog functions at a future date. '
' '
' This object presents exactly the same interface as '
' the Microsoft Common Dialog 6.0 library from Visual '
' Basic 6.0 (comdlg32.dll). '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Values for the Flags property; multiple values can be ORed together.
' In versions of Access prior to Access 2000, comment or delete these
' and use the CommonDialogConstants module (Enum keyword is not
' valid in these versions).
Public Enum CmdlgOpenFlags
cdlOFNAllowMultiselect = &H200
cdlOFNCreatePrompt = &H2000
cdlOFNExplorer = &H80000
cdlOFNFileMustExist = &H1000
cdlOFNHideReadOnly = &H4
cdlOFNNoChangeDir = &H8
cdlOFNNoDereferenceLinks = &H100000
cdlOFNNoNetworkButton = &H20000 ' not documented for common dlg
cdlOFNNoReadOnlyReturn = &H8000
cdlOFNNoValidate = &H100
cdlOFNOverwritePrompt = &H2
cdlOFNPathMustExist = &H800
cdlOFNReadOnly = &H1
cdlOFNShowHelp = &H10
cdlOFNShareAware = &H4000
cdlOFNExtensionDifferent = &H400
End Enum
' Errors raised
Public Enum CmdlgErrors
cdlCancel = 32755 ' user pressed Cancel in dialog
End Enum
' Filter string used for the Open/Save dialog filters (the
' "Files of type" combo box). The string consists of a list of
' filter specs, each of which consists of a pair of elements.
' The first element of each spec is the description to appear
' in the combo box, and the second is a filter pattern. When
' the user selects the description, the corresponding pattern
' is used to filter the list of files in the file list box.
' A pipe character ("|") separates each element of the string.
' Example: "Database Files|*.mdb|All Files|*.*"
Public Filter As String
' Initial Filter to display. This sets/returns the index of the
' currently selected item in the filter combo box.
Public FilterIndex As Long
' Initial directory for the dialog to open in.
' Default = Current working directory.
Public InitDir As String
' Initial file name to populate the dialog with. Default = "".
' Returns the full path name of the selected file.
Public FileName As String
' Returns file name (without path) of file picked
Public FileTitle As String
' Title to appear on the dialog box.
Public DialogTitle As String
' Default extension to append to file if user didn't specify one.
Public DefaultExt As String
' Flags (see constant list) to be used.
' Returns cdlOFNDifferentExtension if extension present and not = DefaultExt
Public Flags As Long
' Maximum length of the file name to be returned
Public MaxFileSize As Integer
' Set to True to raise error 32755 if user cancels dialog box
Public CancelError As Boolean
' Constants used when raising errors
Private Const ErrSource = "MyComDlg.CommonDialog"
' Interface to Win32
Private Type W32_OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
lngFlags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As Long 'String
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As W32_OPENFILENAME) As Boolean
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (pOpenfilename As W32_OPENFILENAME) As Boolean
Private Sub Class_Initialize()
' Initialize the MaxFileSize to minimum, in case the user doesn't set it
MaxFileSize = 256
End Sub
Public Sub ShowOpen()
' Shows the Open dialog
Dim wofn As W32_OPENFILENAME
Dim intRet As Integer
OFN_to_WOFN wofn
On Error GoTo ShowOpen_Error
intRet = GetOpenFileName(wofn)
On Error GoTo 0
WOFN_to_OFN wofn
If (intRet = 0) And CancelError Then _
Err.Raise cdlCancel, ErrSource, "File open canceled by user"
Exit Sub
ShowOpen_Error:
WOFN_to_OFN wofn
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, _
Err.HelpContext
End Sub
Public Sub ShowSave()
' Shows the Save dialog
Dim wofn As W32_OPENFILENAME
Dim intRet As Integer
OFN_to_WOFN wofn
On Error GoTo ShowSave_Error
intRet = GetSaveFileName(wofn)
On Error GoTo 0
WOFN_to_OFN wofn
If (intRet = 0) And CancelError Then _
Err.Raise cdlCancel, ErrSource, "File save canceled by user"
Exit Sub
ShowSave_Error:
WOFN_to_OFN wofn
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, _
Err.HelpContext
End Sub
Private Sub OFN_to_WOFN(wofn As W32_OPENFILENAME)
' This sub converts from the Microsoft Access structure to the Win32 structure.
' Initialize some parts of the structure.
With wofn
.hwndOwner = Application.hWndAccessApp
.hInstance = 0
.lpstrCustomFilter = vbNullString
.nMaxCustrFilter = 0
.lpfnHook = 0
.lpTemplateName = 0
.lCustrData = 0
.lpstrFilter = ConvertFilterString(Filter)
.nFilterIndex = FilterIndex
If MaxFileSize < maxfilesize =" 256" maxfilesize =" Len(FileName)" nmaxfile =" MaxFileSize" lpstrfile =" FileName" nmaxfiletitle =" 260" lpstrfiletitle =" String(260," lpstrtitle =" DialogTitle" lpstrinitialdir =" InitDir" lpstrdefext =" DefaultExt" lngflags =" Flags" lstructsize =" Len(wofn)" filename =" Left$(.lpstrFile," filetitle =" Left$(.lpstrFileTitle," filterindex =" .nFilterIndex" flags =" .lngFlags" strfilter = "" intnum =" 0" intpos =" 1" intlastpos =" 1" intpos =" InStr(intLastPos,"> intLastPos) Then
strFilter = strFilter & Mid(strFilterIn, intLastPos, intPos - intLastPos) & vbNullChar
intNum = intNum + 1
intLastPos = intPos + 1
ElseIf (intPos = intLastPos) Then
intLastPos = intPos + 1
End If
Loop Until (intPos = 0)
' Get last string if it exists (assuming strFilterIn was not bar terminated).
intPos = Len(strFilterIn)
If (intPos >= intLastPos) Then
strFilter = strFilter & Mid(strFilterIn, intLastPos, intPos - intLastPos + 1) & vbNullChar
intNum = intNum + 1
End If
' Add *.* if there's no extension for the last string.
If intNum Mod 2 = 1 Then
strFilter = strFilter & "*.*" & vbNullChar
End If
' Add terminating NULL
ConvertFilterString = strFilter & vbNullChar
End Function
ส่วน code ตัวอย่างนะครับ
สำหรับ Open Dialog
Sample code:
Dim cmdlgOpenFile As New clsCommonDialog
Dim FileName As String 'full file name
Const clngFilterIndexAll = 5
cmdlgOpenFile.Filter = "Text Files (*.txt)|*.txt|DBF Files (DBF)|*.dbf|All Files (*.*)|*.*"
cmdlgOpenFile.FilterIndex = clngFilterIndexAll
'this is where the dialog opens
cmdlgOpenFile.ShowOpen
'returns your full file name.
FileName = cmdlgOpenFile.FileName
'hence no len, no name...
If Len(FileName) = 0 Then Exit Sub
สำหรับ Save Dialog
Dim cmdlgSaveFile As New clsCommonDialog
Dim FileName As String 'full file name
Const clngFilterIndexAll = 5
With cmdlgSaveFile
.FileName = "Claim.xls"
.Filter = "Excel Files (*.xls)|*.xls"
.FilterIndex = clngFilterIndexAll
.ShowSave
'returns your full file name.
FileName = .FileName
If Len(FileName) = 0 Then Exit Sub
End With
Subscribe to:
Posts (Atom)




