Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

设置系统时间格式的代码

时 间:2016-09-13 15:24:21
作 者:爱吉瑞   ID:15524  城市:连云港
摘 要:在一些ACCESS程序中,我们需用记录当前操作的时间。但当系统设置的时间带上午、下午的格式时,我们应用DOCMD.RUNSQL 语句添加记录时(包含日期字段),系统往往会出错。于是,我们得手工修改系统自带的时间格式,而显得我们的程序不够专业。下面的代码提供了自动更改时间格式的一种方式。
正 文:

Option Compare Database
Public Const LOCALE_STIMEFORMAT = &H1003
Public Const LOCALE_SSHORTDATE = &H1F
Public Const WM_SETTINGCHANGE = &H1A
  Public Const HWND_BROADCAST = &HFFFF&
  Const SPI_SETDESKWALLPAPER = 20
  Const SPIF_SENDCHANGE = &H2
Public Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Public Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long

  Public Sub setdateformat()
    Dim dwLCID     As Long, i       As Long
    Dim lpLCData     As String * 255, cchData
    cchData = Len(lpLCData)
        dwLCID = GetSystemDefaultLCID()
    i = SetLocaleInfo(dwLCID, LOCALE_STIMEFORMAT, "h:mm:ss") '设置时间格式
    PostMessage HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0
    SystemParametersInfo SPI_SETDESKWALLPAPER, 0, "", SPIF_SENDCHANGE
    Debug.Print Time
  End Sub

然后,在相应的窗口代码中调用setdateformat即可。

缺点:改好相应的格式(如"h:mm:ss”,需重新启动或注销系统再次打开程序方生效)



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助