By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,212 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,212 IT Pros & Developers. It's quick & easy.

Automatic Data Entry into Form Fields - How?

P: 1
I'm using Access 2003, on Windows XP Pro.

I have a subform which is launched from a command button on my main form. When this form opens, I have 2 fields which I have inserted VBA Code into to capture the information in two of the fields on the main form, so I can sync and track the data entered into the subform to the record on the mainform.

In the "Got Focus" event of each of the two fields I've inserted the following code, to capture the data from the main form:

Expand|Select|Wrap|Line Numbers
  1.  Me.ActiveControl = Forms!F_KSM_Contacts_Sc!SM_Contacts_ID 

Expand|Select|Wrap|Line Numbers
  1.  Me.ActiveControl = Forms!F_KSM_Contacts_Sc![SM_S-PersonRef] 
This works great to capture the data, however it requires me to have the user tab through these two fields in order to have the data be pulled into the fields in question. I want to automate this so that these fields capture that data automatically upon the form opening, but I don't know how to write the syntax in such a way as to make this happen.

I have tried using SendKeys to try and send a "Tab" command using:

Expand|Select|Wrap|Line Numbers
  1.  SendKeys vbKeyTab, 0  
in several properties, including the "On Open" property of the form (thinking that on open it would send two tab keys, which, if the two fields were the first two in the tab order it would tab through them, and thus the data would be pulled in. However this does not work.

Any suggestions for the easiest way to do this? I'm a beginner at VBA and I have a bit of trouble translating the syntax definition into a working syntax statement, so please give me some comments in any suggested code, as a guide. Thanks
Sep 28 '06 #1
Share this Question
Share on Google+
1 Reply

P: 143
Avoid SendKeys at all costs. I think ther may be an easier way.Why not refer to the fields implicitily. Something like this behind your command button. Not too sure about the syntax for subforms though.
Expand|Select|Wrap|Line Numbers
  1.  DoCmd.OpenForm "frmSubFormName", acNormal, "", "[PrimaryKeyField]=[Forms]![frmMainForm]![PrimaryKeyField]", acAdd, acNormal
  3. Forms![frmSubForm]![PrimaryKeyField] = Forms!frmMainForm![PrimaryKeyField]
  5. Forms![frmSubForm]![YourControlName1] = Forms!frmMainForm![YourControlName1]
  7. Forms![frmSubForm]![YourControlName2] = Forms!frmMainForm![YourControlName2] 
Sep 29 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.