Simulate changing the field value of the access subform in VBA

Source: Internet
Author: User

Copyright Notice: Can be reproduced at will.Hyperlink formMark as followsArticleOriginal Source and author information andThis statement

Author:Xixi

Source: http://blog.csdn.net/slowgrace/archive/2009/04/21/4098649.aspx

1. sendkeys

Alas, after several hours, I finally made a try. I was so happy when I came out. Now it seems that I don't feel any more. If you have less time to talk about it, remember it.

In other words, I want to test a series of changes to the field value, so I wantLetProgramSimulate automatically and randomly changing the field value in a subformTo stimulate the dirty and beforeupdate events of the control corresponding to this field.CodeAs follows:

'Change its value by sending a Keyboard Message <br/> 'to give the child form control focus, you must first give the child form focus <br/> me. SFr. setfocus <br/> me. SFr. form. controls ("sngvalue "). setfocus <br/> I = rndx. getrandomnum (1,100) 'returns a new value randomly. <br/>' to use sendkeys, you must use the statement that sets the focus, make the form that receives keyboard input get focus <br/> call sendkeys ("{Delete 2}") 'First Delete two numbers <br/> call sendkeys (I) 'enter a new value, and the sngvalue_dirty event <br/> call sendkeys ("+ {tab}")' is triggered. Press shift and tab at the same time and move the cursor to the previous control, the sngvalue_beforeupdate event is triggered. <br/>

Description:

(1) the key is to use the sendkeys function to simulate keyboard messages.

(2) The sendkeys function can only send keyboard messages to the current window with focus, so you must set the control of the subform to focus first, in this way, only letters can be entered to the appropriate position in the current activity window after obtaining the keyboard input.

(3) Inputting a new value will trigger the dirty event, while the beforeupdate event will trigger the dirty event with Shift + TAB keys to move the focus and then trigger the event.

(4) I directly wrote another control. setfocus is used to move the focus, but the message seems to be faster than the previous sendkeys, resulting in the input of the new string into another control. It is highly suspected that the mouse and keyboard are not in the same queue.

(5) I also tried to use the API function sendmessage to send the wm_settext message. Failed. Because the text box control of the access form does not seem to be a standard control with hwnd, no handle can be found with spy ++. When I tried to find the control handle of a similar Delphi Form using enumchildwindow, I gave up.

2. doevents

In addition, because the keyboard message is a delivery method, if you want to get the correct result in the subsequent test sequence, you must give the system enough time to obtain the message and run the corresponding event process; otherwise, maybe your program runs the test statement, but the tested module has not. This can be implemented using doevents:

Dim starttime as single </P> <p> several sendkeys statements </P> <p> starttime = timer 'set the time when the pause starts. <Br/> do while timer <starttime + 2' and so on for 2 seconds <br/> doevents 'give control to other programs <br/> loop </P> <p> 'Test statement ...... <Br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.