Setting the cursor in a field on a form on load

hunterfan48

Registered User.
Local time
Today, 10:18
Joined
Aug 17, 2008
Messages
436
Is there a command to set the cursor to a certain field upon load? I'm assuming there must be something that would go under the 'on load' event of the form.

I could be wrong, but that's just a guess from a rookie.

Please advise,
Thanks
Brady
 
1) Every control has a SetFocus method which does what you'd expect. Setfocus does not enable a control, so an error occurs if you call SetFocus on a disabled control.
2) Every control has a TabIndex property and a TabStop property. If for ControlA TabStop is True and TabIndex is zero, then ControlA receives the focus by default when the form opens. Probably you can set this programmatically during the Load or Open event handlers of the form, but more commonly you'd set this at design time.
3) Cheers,
 
1) Every control has a SetFocus method which does what you'd expect. Setfocus does not enable a control, so an error occurs if you call SetFocus on a disabled control.

This is exactly what I tried to do and got an error as you had mentioned. What is the difference between an enable and disabled control? How would I know? And last, is there a way I can enable a disabled control?
 
Thanks for the tips...I used your 2nd tip. That one works...not sure if it's the best way, but it will work for now!
 
Every control has an Enabled property which you can get or set in VBA. It is common practice to establish this setting in the Current event of the form, which fires every time a form loads a new record. For instance, you might disable a delete button if a certain record is from the last calendar year, so
Code:
Sub Form_Current()
[COLOR="Green"]  'sets the enabled status of the delete button, disabled if the order is from prior year
[/COLOR]  Me.cmdDelete.Enabled = Not Year(Me.OrderDate) < Year(Date())
End Sub
So before enabling a control and setting focus to it, I'd wonder why is it disabled? Is there an organized scheme or set of business rules as to when and why certain controls are made available and others not?
Cheers,
 
The form is a pop-up form (Data entry). This form is based off my customers table. Any information I enter in here is stored as a new record in my customers table with the corresponding info.

One of the fields is Address (the 4th one down when looking at the form)

As I mentioned, the ID has a default value formula that returns the number I'm looking for, and the 2nd and 3rd fields are prefilled in from the Split function.

So...that means every time this form opens up, I will be starting on the 4th field...that being the Address field.

The address field is field from my Customers table. See any problems with this so far as to why it would be disabled?

Btw...what's an easy way to see if a control is enabled or disabled? Can you find it in the property settings?

Thanks!
 
Visually, a disabled control is greyed out and is not available for interaction with the user. Also, the Enabled property will be equal to True.

If fields are 'pre-filled' presumably that data originates from another table? If so, should those field values be repeated in a new record on a different form? If not, shouldn't those fields be enabled for editing? Also, if the address is the fourth field in the TabIndex and is enabled, why does SetFocus fail?

Consider that in Form_Current you can check the value of certain fields, set their enabled status, and then set the focus before handing the form over to the user for interaction. This sample code checks the values of certain controls and disables them if data is present. If data is not present it sets focus...
Code:
private sub form_current()
[COLOR="Green"]  'a field is enabled if it is null, otherwise disabled[/COLOR]
  me.field1.enabled = not isnull(me.field1)
  me.field2.enabled = not isnull(me.field2)
  me.field3.enabled = not isnull(me.field3)

[COLOR="Green"]  'traverse the controls, and set focus the first one that is enabled[/COLOR]
  dim i as integer
  for i = 1 to 3
    with me.controls("Field" & i)
      if .enabled then 
        .setfocus
        exit for
      end if
    end with
  next
end sub
See how that sort of composes the form prior to interaction with the user? You can do tons of stuff with this approach, and customizations to how the form functions based on the data it contains...
Cheers,
 
3) Cheers,
lagbolt, how does one implement option 3 please? :D

Just a note, since you're referring to the index, you could just use the index counting from 1 to 3 when you're traversing the Controls collection. That is:
Code:
with me.controls(i)
 
If the controls in question happen to be the first three items in the controls collection that would work. What I commonly do if I want to work with a subset of controls is make an array like...
Code:
private m_controls

property Get MyControls as Variant
[COLOR="Green"]  'built when first referenced[/COLOR]
  if isempty(m_controls) then m_controls = Array(me.field1, me.field2, me.field3)
  MyControls = m_controls
end property

private sub form_current()
[COLOR="Green"]  'a field is enabled if it is null, otherwise disabled
[/COLOR]  dim ctrl as control
  for each control in me.MyControls
    ctrl.enabled = not isnull(ctrl)
  next

[COLOR="Green"]  'traverse the controls, and set focus the first one that is enabled[/COLOR]
  for each ctrl in me.MyControls
    if ctrl.enabled then 
      ctrl.setfocus
      exit for
    end if
  next

end sub

And "Cheers" is not to be implemented, it's simply to be received and enjoyed. :) Although if you have a nice old scotch just lying around, then, well, Cheers,
 

Users who are viewing this thread

Back
Top Bottom