Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _ As Long Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _ As Long Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _ ByVal dwBytes As Long) As Long Declare Function CloseClipboard Lib "User32" () As Long Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _ As Long Declare Function EmptyClipboard Lib "User32" () As Long Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _ ByVal lpString2 As Any) As Long Declare Function SetClipboardData Lib "User32" (ByVal wFormat _ As Long, ByVal hMem As Long) As Long Public Const GHND = &H42 Public Const CF_TEXT = 1 Public Const MAXSIZE = 4096 Function ClipBoard_SetData(MyString As String) Dim hGlobalMemory As Long, lpGlobalMemory As Long Dim hClipMemory As Long, X As Long ' Allocate moveable global memory. '------------------------------------------- hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1) ' Lock the block to get a far pointer ' to this memory. lpGlobalMemory = GlobalLock(hGlobalMemory) ' Copy the string to this global memory. lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString) ' Unlock the memory. If GlobalUnlock(hGlobalMemory) <> 0 Then MsgBox "Could not unlock memory location. Copy aborted." GoTo OutOfHere2 End If ' Open the Clipboard to copy data to. If OpenClipboard(0&) = 0 Then MsgBox "Could not open the Clipboard. Copy aborted." Exit Function End If ' Clear the Clipboard. X = EmptyClipboard() ' Copy the data to the Clipboard. hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory) OutOfHere2: If CloseClipboard() = 0 Then MsgBox "Could not close Clipboard." End If End Function
This function illustrates how to add custom properties to the Properties collection associated with an Access Object.
Function AddCustomFormProperty(strFormName As String, _ strPropName As String, _ varPropValue As Variant) ' This procedure illustrates how to add custom ' properties to the Properties collection that ' is associated with an AccessObject object. With CurrentProject.AllForms(strFormName).Properties .Add strPropName, varPropValue End With End Function
The following generic object
variables are required when there is no reference to the DAO 3.6 object
library.
Depenent Libraries:
DAO 3.6 object library or greater
Function AddCustomProperty(strName As String, _ varType As Variant, _ varValue As Variant) As Boolean ' The following generic object variables are required ' when there is no reference to the DAO 3.6 object library. Dim objDatabase As Object Dim objProperty As Object Const PROP_NOT_FOUND_ERROR = 3270 Set objDatabase = CurrentDb On Error GoTo AddProp_Err objDatabase.Properties(strName) = varValue AddCustomProperty = True AddProp_End: Exit Function AddProp_Err: If Err = PROP_NOT_FOUND_ERROR Then Set objProperty = objDatabase.CreateProperty(strName, varType, varValue) objDatabase.Properties.Append objProperty Resume Else AddCustomProperty = False Resume AddProp_End End If End Function
This procedure illustrates how
to add custom properties to the Properties collection that is associated
with an AccessObject object.
Dependencies:
Access 2000 or Greater
Function AddCustomPropertyToObject(intType As AcObjectType, _ strObjectName As String, _ strPropName As String, _ strPropValue As String) As Boolean Dim acpProperty As AccessObjectProperty Dim acoCategory As Access.AllObjects Dim acobjObject As AccessObject On Error GoTo AddProp_Err Select Case intType Case acForm Set acoCategory = CurrentProject.AllForms Case acReport Set acoCategory = CurrentProject.AllReports Case acDataAccessPage Set acoCategory = CurrentProject.AllDataAccessPages Case Else AddCustomPropertyToObject = False Exit Function End Select Set acobjObject = acoCategory(strObjectName) With acobjObject.Properties .Add strPropName, strPropValue End With AddCustomPropertyToObject = True AddProp_End: Exit Function AddProp_Err: AddCustomPropertyToObject = False Resume AddProp_End End Function
These examples display various
ways to open the Opens File Open dialog box in the Comdlg32.dll.
Code Requirements:
Access2000 or greater
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _ "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Boolean Type MSA_OPENFILENAME ' Filter string used for the Open dialog filters. ' Use MSA_CreateFilterString() to create this. ' Default = All Files, *.* strFilter As String ' Initial Filter to display. ' Default = 1. lngFilterIndex As Long ' Initial directory for the dialog to open in. ' Default = Current working directory. strInitialDir As String ' Initial file name to populate the dialog with. ' Default = "". strInitialFile As String strDialogTitle As String ' Default extension to append to file if user didn't specify one. ' Default = System Values (Open File, Save File). strDefaultExtension As String ' Flags (see constant list) to be used. ' Default = no flags. lngFlags As Long ' Full path of file picked. When the File Open dialog box is ' presented, if the user picks a nonexistent file, ' only the text in the "File Name" box is returned. strFullPathReturned As String ' File name of file picked. strFileNameReturned As String ' Offset in full path (strFullPathReturned) where the file name ' (strFileNameReturned) begins. intFileOffset As Integer ' Offset in full path (strFullPathReturned) where the file extension begins. intFileExtension As Integer End Type Const ALLFILES = "All Files" Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As Long nMaxCustrFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String Flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustrData As Long lpfnHook As Long lpTemplateName As Long End Type Const OFN_ALLOWMULTISELECT = &H200 Const OFN_CREATEPROMPT = &H2000 Const OFN_EXPLORER = &H80000 Const OFN_FILEMUSTEXIST = &H1000 Const OFN_HIDEREADONLY = &H4 Const OFN_NOCHANGEDIR = &H8 Const OFN_NODEREFERENCELINKS = &H100000 Const OFN_NONETWORKBUTTON = &H20000 Const OFN_NOREADONLYRETURN = &H8000 Const OFN_NOVALIDATE = &H100 Const OFN_OVERWRITEPROMPT = &H2 Const OFN_PATHMUSTEXIST = &H800 Const OFN_READONLY = &H1 Const OFN_SHOWHELP = &H10 Private Function MSA_GetSaveFileName(msaof As MSA_OPENFILENAME) As Integer ' Opens the file save dialog. Dim of As OPENFILENAME Dim intRet As Integer MSAOF_to_OF msaof, of of.Flags = of.Flags Or OFN_HIDEREADONLY intRet = GetSaveFileName(of) If intRet Then OF_to_MSAOF of, msaof End If MSA_GetSaveFileName = intRet End Function Function MSA_SimpleGetSaveFileName() As String ' Opens the file save dialog with default values. Dim msaof As MSA_OPENFILENAME Dim intRet As Integer Dim strRet As String intRet = MSA_GetSaveFileName(msaof) If intRet Then strRet = msaof.strFullPathReturned End If MSA_SimpleGetSaveFileName = strRet End Function Function MSA_ConvertFilterString(strFilterIn As String) As String ' Creates a filter string from a bar ("|") separated string. ' The string should be pairs of filter|extension strings, i.e. "Access ' Databases|*.mdb|All Files|*.*" ' If no extensions exists for the last filter pair, *.* is added. ' This code will ignore any empty strings, i.e. "||" pairs. ' Returns "" if an empty string is passed in. Dim strFilter As String Dim intNum As Integer, intPos As Integer, intLastPos As Integer strFilter = "" intNum = 0 intPos = 1 intLastPos = 1 ' Add strings as long as we find bars. ' Ignore any empty strings (not allowed). Do intPos = InStr(intLastPos, strFilterIn, "|") If (intPos > 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 if we have any filter. If strFilter <> "" Then strFilter = strFilter & vbNullChar End If MSA_ConvertFilterString = strFilter End Function Function MSA_CreateFilterString(ParamArray varFilt() As Variant) As String ' Creates a filter string from the passed in arguments. ' Returns "" if no argumentss are passed in. ' Expects an even number of argumentss (filter name, extension), but ' if an odd number is passed in, it appends "*.*". Dim strFilter As String Dim intRet As Integer Dim intNum As Integer intNum = UBound(varFilt) If (intNum <> -1) Then For intRet = 0 To intNum strFilter = strFilter & varFilt(intRet) & vbNullChar Next If intNum Mod 2 = 0 Then strFilter = strFilter & "*.*" & vbNullChar End If strFilter = strFilter & vbNullChar Else strFilter = "" End If MSA_CreateFilterString = strFilter End Function Private Sub OF_to_MSAOF(of As OPENFILENAME, msaof As MSA_OPENFILENAME) ' This sub converts from the Win32 structure to the Microsoft Access structure. msaof.strFullPathReturned = Left(of.lpstrFile, InStr(of.lpstrFile, vbNullChar) - 1) msaof.strFileNameReturned = of.lpstrFileTitle msaof.intFileOffset = of.nFileOffset msaof.intFileExtension = of.nFileExtension End Sub Private Sub MSAOF_to_OF(msaof As MSA_OPENFILENAME, of As OPENFILENAME) ' This sub converts from the Microsoft Access structure to the Win32 structure. Dim strFile As String * 512 ' Initialize some parts of the structure. of.hwndOwner = Application.hWndAccessApp of.hInstance = 0 of.lpstrCustomFilter = 0 of.nMaxCustrFilter = 0 of.lpfnHook = 0 of.lpTemplateName = 0 of.lCustrData = 0 If msaof.strFilter = "" Then of.lpstrFilter = MSA_CreateFilterString(ALLFILES) Else of.lpstrFilter = msaof.strFilter End If of.nFilterIndex = msaof.lngFilterIndex of.lpstrFile = msaof.strInitialFile _ & String(512 - Len(msaof.strInitialFile), 0) of.nMaxFile = 511 of.lpstrFileTitle = String(512, 0) of.nMaxFileTitle = 511 of.lpstrTitle = msaof.strDialogTitle of.lpstrInitialDir = msaof.strInitialDir of.lpstrDefExt = msaof.strDefaultExtension of.Flags = msaof.lngFlags of.lStructSize = Len(of) End Sub
This example shows you how to create a procedure that will run if your Microsoft Access application does not detect any user input for a specified period of time.
'This example uses a form with the TimerInterval set to 1000 Sub Form_Timer() ' IDLEMINUTES determines how much idle time to wait for before ' running the IdleTimeDetected subroutine. Const IDLEMINUTES = 5 Static PrevControlName As String Static PrevFormName As String Static ExpiredTime Dim ActiveFormName As String Dim ActiveControlName As String Dim ExpiredMinutes On Error Resume Next ' Get the active form and control name. ActiveFormName = Screen.ActiveForm.Name If Err Then ActiveFormName = "No Active Form" Err = 0 End If ActiveControlName = Screen.ActiveControl.Name If Err Then ActiveControlName = "No Active Control" Err = 0 End If ' Record the current active names and reset ExpiredTime if: ' 1. They have not been recorded yet (code is running ' for the first time). ' 2. The previous names are different than the current ones ' (the user has done something different during the timer ' interval). If (PrevControlName = "") Or (PrevFormName = "") _ Or (ActiveFormName <> PrevFormName) _ Or (ActiveControlName <> PrevControlName) Then PrevControlName = ActiveControlName PrevFormName = ActiveFormName ExpiredTime = 0 Else ' ...otherwise the user was idle during the time interval, so ' increment the total expired time. ExpiredTime = ExpiredTime + Me.TimerInterval End If ' Does the total expired time exceed the IDLEMINUTES? ExpiredMinutes = (ExpiredTime / 1000) / 60 If ExpiredMinutes >= IDLEMINUTES Then ' ...if so, then reset the expired time to zero... ExpiredTime = 0 ' ...and call the IdleTimeDetected subroutine. IdleTimeDetected ExpiredMinutes End If End Sub ' Create the following procedure in the form module: Sub IdleTimeDetected (ExpiredMinutes) Dim Msg As String Msg = "No user activity detected in the last " Msg = Msg & ExpiredMinutes & " minute(s)!" MsgBox Msg, 48 End Sub
When you run the Shell() function in a Visual Basic for Applications procedure, it starts an executable program asynchronously and returns control to the procedure. This shelled program continues to run independently of your procedure until you close it
Option Explicit Private Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End Type Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _ hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal _ hObject As Long) As Long Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const INFINITE = -1& Public Sub ExecCmd(cmdline$) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim ReturnValue As Integer ' Initialize the STARTUPINFO structure: start.cb = Len(start) ' Start the shelled application: ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc) ' Wait for the shelled application to finish: Do ReturnValue = WaitForSingleObject(proc.hProcess, 0) DoEvents Loop Until ReturnValue <> 258 ReturnValue = CloseHandle(proc.hProcess) End Sub 'To show the Sub Sub Testing() ExecCmd "NOTEPAD.EXE" MsgBox "Process Finished" End Sub
