Is there a better way?

R

Rob

I am trying to create a new record with an ID field prefilled in the new
record based on another form.
This code is sloppy in my opinion, but is the only way I know how to do it
because I can't set focus on invisible, non-enabled or locked fields.

DoCmd.GoToRecord , , acNewRec
txtSubPhaseID.Visible = True
Forms!frmProducts!txtSubPhaseID.SetFocus
txtSubPhaseID.Text = Forms!frmPhaseSearch.CboSubphase
Forms!frmProducts!cboVendorID.SetFocus
txtSubPhaseID.Visible = False


Is there a better way?

Rob
 
S

Steve Conway

Hi Rob,

You should be able to just use:

DoCmd.GoToRecord , , acNewRec
txtSubPhaseID = Forms!frmPhaseSearch.CboSubphase

if you want to update the record right away you can include a Dirty = False

DoCmd.GoToRecord , , acNewRec
txtSubPhaseID = Forms!frmPhaseSearch.CboSubphase
Me.Dirty = False

HTH
Steve C
 
P

Pavel Romashkin

Have you tried NOT using the Text property? Like, leaving it out
completely (or replacing .Text with .Value)? This will default to using
Value instead, which, as far as I know, does not require the control to
have focus, and, therefore, be visible.

Pavel
 
G

Graham Mandeno

Hi Rob

A textbox has both a Text property and a Value property. The Text property
is only valid when the control has the focus, and gives the key-by-key text
of what is currently in the control as you type, delete, etc. The Value
property (which is the default property for all editable controls) is what
is finally left in the textbox after its update is complete.

You can set the Value property from code any time, no matter whether the
control has focus, or is visible or enabled. So, all you need is:
txtSubPhaseID.Value = Forms!frmPhaseSearch.CboSubphase

Or, because Value is the default property, you can omit it entirely:
txtSubPhaseID = Forms!frmPhaseSearch.CboSubphase
 
R

Rob

Thanks to all,

These suggestions were all right on target and I have it working very well.

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top