Access 自增函数及相关技巧

  1. 'Access 自增函数及相关技巧
  2. '检查指定文件是否存在
  3. ***************** Code Start *******************
  4. Function fIsFileDIR(stPath As String, _
  5.                     Optional lngType As Long) _
  6.                     As Integer
  7. 'Fully qualify stPath
  8. 'To check for a file
  9. '   ?fIsFileDIR("c:/winnt/win.ini")
  10. 'To check for a Dir
  11. '   ?fIsFileDir("c:/msoffice",vbdirectory)
  12. '
  13.     On Error Resume Next
  14.     fIsFileDIR = Len(Dir(stPath, lngType)) > 0
  15. End Function
  16. '***************** Code End *********************
  17. '列表框中多选查询
  18. '******************** Code Start ************************
  19.     Dim frm As Form, ctl As Control
  20.     Dim varItem As Variant
  21.     Dim strSQL As String
  22.     Set frm = Form!frmMyForm
  23.     Set ctl = frm!lbMultiSelectListbox
  24.     strSQL = "Select * from Employees where EmpID="
  25.     'Assuming long EmpID is the bound field in lb
  26.     'enumerate selected items and
  27.     'concatenate to strSQL
  28.     For Each varItem In ctl.ItemsSelected
  29.         strSQL = strSQL & ctl.ItemData(varItem) & " OR EmpID="
  30.     Next varItem
  31.     'Trim the end of strSQL
  32.     strSQL=left$(strSQL,len(strSQL)-12))
  33. '******************** Code end ************************
  34. 屏蔽PageUP , PageDown
  35. '************ Code Start **********
  36. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  37. '33 - PgUp; 34 - PgDown; 9 - Tab; 18=Alt
  38.     Select Case KeyCode
  39.         Case 33, 34, 9, 18
  40.             KeyCode = 0
  41.         Case Else
  42.             'Debug.Print KeyCode, Shift
  43.     End Select
  44. End Sub
  45. '************ Code End   **********
  46. ''窗体参数
  47.   DoCmd.OpenForm "SomeFormB", , , , , , Me.Name
  48.   DoCmd.Close acForm, Me.OpenArgs
  49. '更新保存提示.
  50. ****************** Code Start ******************
  51. Private Sub Form_BeforeUpdate(Cancel As Integer)
  52.     Dim strMsg As String
  53.     strMsg = "Data has changed."
  54.     strMsg = strMsg & "@Do you wish to save the changes?"
  55.     strMsg = strMsg & "@Click Yes to Save or No to Discard changes."
  56.     If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
  57.         'do nothing
  58.     Else
  59.         DoCmd.RunCommand acCmdUndo
  60.         
  61.         'For Access 95, use DoMenuItem instead
  62.         'DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
  63.     End If
  64. End Sub
  65. '子窗口无数据时,隐藏
  66. '*********** Code Start **********
  67. Private Sub Form_Current()
  68.     With Me!SubformName.Form
  69.         .Visible = (.RecordsetClone.RecordCount > 0)
  70.     End With
  71. End Sub
  72. '*********** Code End **********
  73. '窗口增加时钟
  74. ***************** Code Start ***************
  75. Private Sub Form_Timer()
  76.     Me!lblClock.Caption = Format(Now, "dddd, mmm d yyyy, hh:mm:ss AMPM")
  77. End Sub
  78. Private Sub cmdClockStart_Click()
  79.     Me.TimerInterval = 1000
  80. End Sub
  81. Private Sub cmdClockEnd_Click()
  82.     Me.TimerInterval = 0
  83. End Sub
  84. '***************** Code End ***************
  85. '引用外部数据库的窗体
  86. '************ Code Start *************
  87. 'Private Declare Function apiSetForegroundWindow Lib "user32" _
  88.             Alias "SetForegroundWindow" _
  89.             (ByVal hwnd As Long) _
  90.             As Long
  91. Private Declare Function apiShowWindow Lib "user32" _
  92.             Alias "ShowWindow" _
  93.             (ByVal hwnd As Long, _
  94.             ByVal nCmdShow As Long) _
  95.             As Long
  96. Private Const SW_MAXIMIZE = 3
  97. Private Const SW_NORMAL = 1
  98. Function fOpenRemoteForm(strMDB As String, _
  99.                                         strForm As String, _
  100.                                         Optional intView As Variant) _
  101.                                         As Boolean
  102. Dim objAccess As Access.Application
  103. Dim lngRet As Long
  104.     On Error GoTo fOpenRemoteForm_Err
  105.     If IsMissing(intView) Then intView = acViewNormal
  106.     If Len(Dir(strMDB)) > 0 Then
  107.         Set objAccess = New Access.Application
  108.         With objAccess
  109.             lngRet = apiSetForegroundWindow(.hWndAccessApp)
  110.             lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
  111.             'the first call to ShowWindow doesn't seem to do anything
  112.             lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
  113.             .OpenCurrentDatabase strMDB
  114.             .DoCmd.OpenForm strForm, intView
  115.             Do While Len(.CurrentDb.Name) > 0
  116.                 DoEvents
  117.             Loop
  118.         End With
  119.     End If
  120. fOpenRemoteForm_Exit:
  121.     On Error Resume Next
  122.     objAccess.Quit
  123.     Set objAccess = Nothing
  124.     Exit Function
  125. fOpenRemoteForm_Err:
  126.     fOpenRemoteForm = False
  127.     Select Case Err.Number
  128.         Case 7866:
  129.             'mdb is already exclusively opened
  130.             MsgBox "The database you specified " & vbCrLf & strMDB & _
  131.                 vbCrLf & "is currently open in exclusive mode.  " & vbCrLf _
  132.                 & vbCrLf & "Please reopen in shared mode and try again", _
  133.                 vbExclamation + vbOKOnly, "Could not open database."
  134.         Case 2102:
  135.             'form doesn't exist
  136.             MsgBox "The Form '" & strForm & _
  137.                         "' doesn't exist in the Database " _
  138.                         & vbCrLf & strMDB, _
  139.                         vbExclamation + vbOKOnly, "Form not found"
  140.         Case 7952:
  141.             'user closed mdb
  142.             fOpenRemoteForm = True
  143.         Case Else:
  144.             MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description, _
  145.                     vbCritical + vbOKOnly, "Runtime error"
  146.     End Select
  147.     Resume fOpenRemoteForm_Exit
  148. End Function
  149. '************ Code End *************
  150. '关闭所有窗体
  151. Dim intx As Integer
  152.    Dim intCount As Integer
  153.    intCount = Forms.Count - 1
  154.    For intx = intCount To 0 Step -1
  155.     DoCmd.Close acForm, Forms(intx).Name
  156.    Next
  157. '*************OR**************
  158.    For intx = intCount To 0 Step -1
  159.         If Forms(intx).Name <> "MyFormToKeepOpen" Then
  160.             DoCmd.Close acForm, Forms(intx).Name
  161.         End If
  162.    Next
  163. '复制当前打开的数据库
  164. '********** Code Start *************
  165. Private Type SHFILEOPSTRUCT
  166.     hwnd As Long
  167.     wFunc As Long
  168.     pFrom As String
  169.     pTo As String
  170.     fFlags As Integer
  171.     fAnyOperationsAborted As Boolean
  172.     hNameMappings As Long
  173.     lpszProgressTitle As String
  174. End Type
  175. Private Const FO_MOVE As Long = 
  176. Private Const FO_COPY As Long = 
  177. Private Const FO_DELETE As Long = 
  178. Private Const FO_RENAME As Long = 
  179. Private Const FOF_MULTIDESTFILES As Long = 
  180. Private Const FOF_CONFIRMMOUSE As Long = 
  181. Private Const FOF_SILENT As Long = 
  182. Private Const FOF_RENAMEONCOLLISION As Long = 
  183. Private Const FOF_NOCONFIRMATION As Long = 
  184. Private Const FOF_WANTMAPPINGHANDLE As Long = 
  185. Private Const FOF_CREATEPROGRESSDLG As Long = 
  186. Private Const FOF_ALLOWUNDO As Long = 
  187. Private Const FOF_FILESONLY As Long = 
  188. Private Const FOF_SIMPLEPROGRESS As Long = 
  189. Private Const FOF_NOCONFIRMMKDIR As Long = 
  190. Private Declare Function apiSHFileOperation Lib "Shell32.dll" _
  191.             Alias "SHFileOperationA" _
  192.             (lpFileOp As SHFILEOPSTRUCT) _
  193.             As Long
  194. Function fMakeBackup() As Boolean
  195. Dim strMsg As String
  196. Dim tshFileOp As SHFILEOPSTRUCT
  197. Dim lngRet As Long
  198. Dim strSaveFile As String
  199. Dim lngFlags As Long
  200. Const cERR_USER_CANCEL = vbObjectError + 1
  201. Const cERR_DB_EXCLUSIVE = vbObjectError + 2
  202.     On Local Error GoTo fMakeBackup_Err
  203.     If fDBExclusive = True Then Err.Raise cERR_DB_EXCLUSIVE
  204.     
  205.     strMsg = "Are you sure that you want to make a copy of the database?"
  206.     If MsgBox(strMsg, vbQuestion + vbYesNo, "Please confirm") = vbNo Then _
  207.             Err.Raise cERR_USER_CANCEL
  208.             
  209.     lngFlags = FOF_SIMPLEPROGRESS Or _
  210.                             FOF_FILESONLY Or _
  211.                             FOF_RENAMEONCOLLISION
  212.     strSaveFile = CurrentDb.Name
  213.     With tshFileOp
  214.         .wFunc = FO_COPY
  215.         .hwnd = hWndAccessApp
  216.         .pFrom = CurrentDb.Name & vbNullChar
  217.         .pTo = strSaveFile & vbNullChar
  218.         .fFlags = lngFlags
  219.     End With
  220.     lngRet = apiSHFileOperation(tshFileOp)
  221.     fMakeBackup = (lngRet = 0)
  222.     
  223. fMakeBackup_End:
  224.     Exit Function
  225. fMakeBackup_Err:
  226.     fMakeBackup = False
  227.     Select Case Err.Number
  228.         Case cERR_USER_CANCEL:
  229.             'do nothing
  230.         Case cERR_DB_EXCLUSIVE:
  231.             MsgBox "The current database " & vbCrLf & CurrentDb.Name & vbCrLf & _
  232.                     vbCrLf & "is opened exclusively.  Please reopen in shared mode" & _
  233.                     " and try again.", vbCritical + vbOKOnly, "Database copy failed"
  234.         Case Else:
  235.             strMsg = "Error Information…" & vbCrLf & vbCrLf
  236.             strMsg = strMsg & "Function: fMakeBackup" & vbCrLf
  237.             strMsg = strMsg & "Description: " & Err.Description & vbCrLf
  238.             strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
  239.             MsgBox strMsg, vbInformation, "fMakeBackup"
  240.     End Select
  241.     Resume fMakeBackup_End
  242. End Function
  243. Private Function fCurrentDBDir() As String
  244. 'code courtesy of
  245. 'Terry Kreft
  246.     Dim strDBPath As String
  247.     Dim strDBFile As String
  248.     strDBPath = CurrentDb.Name
  249.     strDBFile = Dir(strDBPath)
  250.     fCurrentDBDir = Left(strDBPath, InStr(strDBPath, strDBFile) - 1)
  251. End Function
  252. Function fDBExclusive() As Integer
  253.     Dim db As Database
  254.     Dim hFile As Integer
  255.     hFile = FreeFile
  256.     Set db = CurrentDb
  257.     On Error Resume Next
  258.     Open db.Name For Binary Access Read Write Shared As hFile
  259.     Select Case Err
  260.         Case 0
  261.             fDBExclusive = False
  262.         Case 70
  263.             fDBExclusive = True
  264.         Case Else
  265.             fDBExclusive = Err
  266.     End Select
  267.     Close hFile
  268.     On Error GoTo 0
  269. End Function
  270. '************* Code End ***************
  271. '代替replace函数
  272. '************ Code Start **********
  273. Function fstrTran(ByVal sInString As String, _
  274.                            sFindString As String, _
  275.                            sReplaceString As String) As String
  276.     Dim iSpot As Integer, iCtr As Integer
  277.     Dim iCount As Integer
  278.   
  279.     iCount = Len(sInString)
  280.     For iCtr = 1 To iCount
  281.         iSpot = InStr(1, sInString, sFindString)
  282.         If iSpot > 0 Then
  283.             sInString = Left(sInString, iSpot - 1) & _
  284.                         sReplaceString & _
  285.                         Mid(sInString, iSpot + Len(sFindString))
  286.         Else
  287.             Exit For
  288.         End If
  289.     Next
  290.     fstrTran = sInString
  291.   
  292. End Function
  293. '************* Code End ***************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值