ACC: How to Simulate Drag-And-Drop Capabilities (95/97)Last reviewed: November 12, 1997Article ID: Q137650 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. Microsoft Access does not have a built-in DragDrop event for controls as does Visual Basic. This article describes how to create a DragDrop procedure that will run in response to a Microsoft Access control being dragged to another control. This article provides two examples that demonstrate the use of the DragDrop procedure. The first example demonstrates how to drag the value in a control on one form to a control on a second form. The second example demonstrates how to move items from one list box to a second list box using a drag-and-drop procedure. If you perform a drag-and-drop procedure with the CTRL key held down, all items from one list will be moved to the second list. NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:
ARTICLE-ID: Q150895 TITLE : ACC95: Microsoft Access Sample Forms Available on MSL ARTICLE-ID: Q175066 TITLE : ACC97: Microsoft Access 97 Sample Forms Available on MSL MORE INFORMATIONThe four procedures below, DragStart, DragStop, DropDetect, and DragDrop, are required to implement drag-and-drop capabilities between controls in Microsoft Access.
DragStart, DragStopEnables a control to be dragged. DragStart and DragStop are called from the MouseDown and MouseUp events respectively for each control that you want to be capable of being dragged.
Syntax: Sub DragStart (DragFrm As Form) Sub DragStop () DragFrm: The form containing the control being dragged. Example: Private Sub MyControl_MouseDown (Button As Integer, Shift As ... DragStart Me End Sub Private Sub MyControl_MouseDown (Button As Integer, Shift As ... DragStop End Sub DropDetectEnables a control to be a drop target. DropDetect should be called from the MouseMove event for each control that you want to be a drop target for a control being dragged.
Syntax: Sub DropDetect (DropFrm As Form, DropCtrl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) DropFrm: The form containing the control being dropped on. DropCtrl: The control being dropped on. Button, Shift, X, Y: The parameters from the MouseMove event. Example: Private Sub MyControl_MouseMove (Button As Integer, Shift As ... DropDetect Me, Me![Employee ID], Button, Shift, X, Y End SubNOTE: A control can be dragged and be a drop target by calling DragStart, DragStop, and DropDetect from the MouseDown, MouseUp, and MouseMove events respectively.
DragDropThis procedure will be called in response to a drag-enabled control being dragged to a drop-enabled control. You will add your own code to this procedure to control what happens in response to a drop operation. For example you could copy the contents of the dragged control to the dropped control.
Syntax: Sub DragDrop (DragFrm As Form, DragCtrl As Control, DropFrm As Form, DropCtrl As Control, Button As Integer, Shift As _ Integer, X As Single, Y As Single) DragFrm: The form containing the control being dragged. DragCtrl: The control being dragged. DropFrm: The form containing the control being dropped on. DropCtrl: The control being dropped on. Button: The state of the mouse buttons when the drop occurred. Shift: The state of the SHIFT, CTRL, ALT keys when the drop occurred. X, Y: The x and y coordinates of the mouse where the drop occurred. NOTE: For more information on Button, Shift, X, Y arguments, search Help for the MouseMove event.Example: The following example will copy the contents of the drag control to the drop control, trapping and displaying any error that occurs:
Sub DragDrop (DragFrm As Form, DragCtrl As Control, ... On Error Resume Next DropCtrl = DragCtrl If Err Then MsgBox Error$ End Sub EXAMPLE 1: Drag and Drop Between Controls on Two FormsThe following example demonstrates how to set the Salesperson combo box on the Northwind Orders form by dragging the EmployeeID field from the Employees form and dropping it into the Salesperson combo box.
1. Open the sample database Northwind.mdb. 2. Create a new module and type the following lines in the Declarations section: Option Explicit Dim DragFrm As Form Dim DragCtrl As Control Dim DropTime Const MAX_DROP_TIME = .1 Dim CurrentMode As Integer Const NO_MODE = 0 Const DROP_MODE = 1 Const DRAG_MODE = 2 3. Type the following three procedures: Sub DragStart (SourceFrm As Form) ' NOTE: You should not use Screen.ActiveForm in place of ' SourceFrm because you may be dragging from a subform. Set DragFrm = SourceFrm Set DragCtrl = Screen.ActiveControl CurrentMode = DRAG_MODE End Sub Sub DragStop () CurrentMode = DROP_MODE DropTime = Timer End Sub Sub DropDetect (DropFrm As Form, DropCtrl As Control, _ Button As Integer, Shift As Integer, _ X As Single, Y As Single) ' If a drop hasn't happened, then exit. If CurrentMode <> DROP_MODE Then Exit Sub CurrentMode = NO_MODE ' The timer interval is permitted between the MouseUp event and ' the MouseMove event. This ensures that the MouseMove event does ' not invoke the Drop procedure unless it is the MouseMove event ' that Microsoft Access automatically fires for the Drop control ' following the MouseUp event of a drag control. Subsequent ' MouseMove events will fail the timer test and be ignored. If Timer - DropTime > MAX_DROP_TIME Then Exit Sub ' Did we drag/drop onto ourselves? If (DragCtrl.Name <> DropCtrl.Name) Or _ (DragFrm.hWnd <> DropFrm.hWnd) Then ' If not, then a successful drag/drop occurred. DragDrop DragFrm, DragCtrl, DropFrm, DropCtrl, Button, Shift, X, Y End If End Sub 4. Type the following DragDrop procedure. NOTE: This procedure does not have to appear in the same module as the procedures typed above. Because you will be customizing the contents of the DragDrop procedure to respond to a successful drag-and-drop operation, it may be better to place it in a separate module. Sub DragDrop (DragFrm As Form, DragCtrl As Control, _ DropFrm As Form, DropCtrl As Control, _ Button As Integer, Shift As Integer, _ X As Single, Y As Single) On Error Resume Next DropCtrl = DragCtrl If Err Then MsgBox Error$ End Sub 5. Open the Orders form in Design view and add the following OnMouseMove event procedure code for the Salesperson combo box: Sub EmployeeID_MouseMove (Button As Integer, Shift As Integer, _ X As Single, Y As Single) DropDetect Me, Me![EmployeeID], Button, Shift, X, Y End Sub NOTE: This control will only be a drop target. 6. Open the Employees form in Design view and set the Enabled property of the EmployeeID text box to Yes. 7. Add the following OnMouseDown event procedure code for the EmployeeID control: Sub EmployeeID_MouseDown (Button As Integer, Shift As Integer, _ X As Single, Y As Single) DragStart Me End Sub NOTE: This control will only be a drag target. 8. Add the following OnMouseUp event procedure code for the EmployeeID control: Sub EmployeeID_MouseUp (Button As Integer, Shift As Integer, _ X As Single, Y As Single) DragStop End Sub 9. View the Orders form in Form view and position it in the upper-left corner of the screen. 10. View the Employees form in Form view and position it on top of the Orders form, but to the right in such a way that the Salesperson combo box on the Orders form is still visible. 11. Hold down the mouse button in the EmployeeID text box in the Employees form, then move the mouse pointer (still holding the button down) over the Salesperson combo box on the Orders form, and then release the mouse button.RESULT: The Salesperson combo box will contain the salesperson that you dragged from the Employees form. Try navigating to different Employee records and dragging the Employee ID to the Salesperson combo box.
EXAMPLE 2: Drag and Move Items from One List Box to Another List BoxThe following example demonstrates how to present a list of items to a user in one list box and how to move these items back and forth to a second list box using a drag-and-drop operation.
|
Additional query words: 7.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |