Assign focus to a field that isnt always there (1 Viewer)

omarrr128

Member
Local time
Today, 23:42
Joined
Feb 5, 2023
Messages
69
Currently I have a line of code that is:

Private Sub Form_Open(Cancel As Integer)
Me.Assign_Porter.SetFocus
End Sub

I have now changed the form design a bit so that the field "Assign.porter" is not always present (I turned allow additions off) whereas before it was. Now i get errors when the field is not there that the code cannot run.

How do i set the VBA now so that it sets focus to the field only when it is present in the form and if it is not present then it does nothing?

Thank you
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 15:42
Joined
Oct 29, 2018
Messages
21,474
Why do you need to use code to assign the focus? Is it on a subform? Just curious...
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:42
Joined
May 21, 2018
Messages
8,529
by present do you mean visible? If not, I do not get a form that may or may not have a control. Makes no sense. If you are dynamically adding and removing controls at runtime this is a bad idea, and do not waste your time doing this in Access.

Code:
if me.assign_Porter.visible then me.Assign_Porter.setfocus
 
Last edited:

omarrr128

Member
Local time
Today, 23:42
Joined
Feb 5, 2023
Messages
69
Why do you need to use code to assign the focus? Is it on a subform? Just curious...

The form refreshes every minute with the VBA. If someone is writing some notes then the refresh will push them to another field and they will continue writing in the wrong field and they may not realise.

The assign focus helps to block them from writing.

I know there's code to put them back on the same field they were on but I don't think it would be a good idea for my database
 

omarrr128

Member
Local time
Today, 23:42
Joined
Feb 5, 2023
Messages
69
by present do you mean visible? If not, I do not get a form that may or may not have a control. Makes no sense. If you are dynamically adding and removing controls at runtime this is a bad idea, and do not waste your time doing this in Access.

Code:
if me.assign_Porter.visible then me.Assign_Porter.setfocus
I turned allow additions off so when there are no records to display then the form is completely empty and so the assign.porter field is not at all there. Do you know if this Will this work for that?
 

Mike Krailo

Well-known member
Local time
Today, 18:42
Joined
Mar 28, 2020
Messages
1,044
Code:
If me.recordset.recordcount > 0 then
   Me.Assign_Porter.SetFocus
End If
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:42
Joined
Oct 29, 2018
Messages
21,474
The form refreshes every minute with the VBA. If someone is writing some notes then the refresh will push them to another field and they will continue writing in the wrong field and they may not realise.

The assign focus helps to block them from writing.

I know there's code to put them back on the same field they were on but I don't think it would be a good idea for my database
The code you posted was in the Open event. How exactly is the getting refreshed? If you're just requerying the form, the Open event shouldn't fire again.
 

omarrr128

Member
Local time
Today, 23:42
Joined
Feb 5, 2023
Messages
69
The code you posted was in the Open event. How exactly is the getting refreshed? If you're just requerying the form, the Open event shouldn't fire again.
That was just a copy and paste I used to show the code.

I have the same code in other places where the form does a requery
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:42
Joined
Feb 28, 2001
Messages
27,189
Now i get errors when the field is not there that the code cannot run.
so the assign.porter field is not at all there

IF you are actually adding and/or removing controls from a form then DON'T DO THIS. Stop it right now. Immediately. Each time you add a control to a form, even one that was there before but got removed, it counts against a lifetime limit of (I think) 768 controls that can be added to a given form over its entire service life. You will get an irrevocable and unrecoverable error when you reach the limit and will have to build the form over again from scratch.

If on the other hand you are saying that the control might be disabled and not visible but is really still there, you can test for those conditions with something resembling:

Code:
IF Me.[assign.porter].visible then
    Do something
ELSE
    Don't do something
END IF

You will absolutely need the brackets as noted in the reference to [assign.porter] because otherwise in the context following a Me. type of command, Access will be looking for a control named ASSIGN with a property named PORTER (which doesn't exist and would lead to an Object Not Found error.)

AND if you are saying that the control is there but might be empty, you can test for IF NZ( [ASSIGN.PORTER], "" ) <> "" to see if there is something in the control other than NULL or a zero-length-string. And in this context, you STILL need the brackets. It is a VERY bad idea in the long run to put special characters in a control or field name. Among other things, leading to a requirement for brackets all of the time.
 

omarrr128

Member
Local time
Today, 23:42
Joined
Feb 5, 2023
Messages
69
IF you are actually adding and/or removing controls from a form then DON'T DO THIS. Stop it right now. Immediately. Each time you add a control to a form, even one that was there before but got removed, it counts against a lifetime limit of (I think) 768 controls that can be added to a given form over its entire service life. You will get an irrevocable and unrecoverable error when you reach the limit and will have to build the form over again from scratch.

If on the other hand you are saying that the control might be disabled and not visible but is really still there, you can test for those conditions with something resembling:

Code:
IF Me.[assign.porter].visible then
    Do something
ELSE
    Don't do something
END IF

You will absolutely need the brackets as noted in the reference to [assign.porter] because otherwise in the context following a Me. type of command, Access will be looking for a control named ASSIGN with a property named PORTER (which doesn't exist and would lead to an Object Not Found error.)

AND if you are saying that the control is there but might be empty, you can test for IF NZ( [ASSIGN.PORTER], "" ) <> "" to see if there is something in the control other than NULL or a zero-length-string. And in this context, you STILL need the brackets. It is a VERY bad idea in the long run to put special characters in a control or field name. Among other things, leading to a requirement for brackets all of the time.
I don't really understand the first part about controls.

What I have right now is a field called "Assign Porter" and in the form it is a combo box where the porter clicks their name on the list.

I turned allow additions off so that when there are no records to display then the screen is completely empty and none of the fields are on the screen. Even invisibly.

I used the above persons code of using the recordset.recordcount to set focus on the assign.porter field when it is there.

Is this a bad idea?
 

Mike Krailo

Well-known member
Local time
Today, 18:42
Joined
Mar 28, 2020
Messages
1,044
The reason why he was getting the error was because when there is no records in the sub form and allowadditions is false, there are no visible controls in the sub form to do the me.assign_porter.setfocus. Hence, a simple test to check the number of records is required first.

So he is not directly manipulating any controls visibility or anything out of the ordinary.

Ommarrr128, glad you got it working.
 

moke123

AWF VIP
Local time
Today, 18:42
Joined
Jan 11, 2013
Messages
3,920
The form refreshes every minute with the VBA. If someone is writing some notes then the refresh will push them to another field and they will continue writing in the wrong field and they may not realise.

The assign focus helps to block them from writing.
Just a thought and I've never tried it but can't you alter the timer event to not fire if the form is dirty?

edit: A quick test seems to work

Code:
Private Sub Form_Timer()
    If Not Me.Dirty Then
       'your code here
    End If
End Sub
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:42
Joined
Feb 28, 2001
Messages
27,189
The reason why he was getting the error was because when there is no records in the sub form and allowadditions is false, there are no visible controls in the sub form to do the me.assign_porter.setfocus. Hence, a simple test to check the number of records is required first.

So he is not directly manipulating any controls visibility or anything out of the ordinary.

Ommarrr128, glad you got it working.

Ah, thanks Mike. I kept on reading that and taking the words at face value when saying "not there."
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:42
Joined
May 21, 2018
Messages
8,529
How do i set the VBA now so that it sets focus to the field only when it is present in the form and if it is not present then it does nothing?
Just to be semantically correct. When a form returns no records the controls "are present" and they are "visible", but there are no records so nothing is painted to the form. It is not that you cannot go to the control as much as you cannot go to the record as specified by the error.
Run time error 2105: you cannot go to the specified record.

You can verify this by code
Code:
Private Sub Command8_Click()
  Debug.Print Me.txtID.Name & ", Is visible = " & Me.txtID.Visible
  Me.txtID.SetFocus
End Sub
Prints
txtID, Is visible = True
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:42
Joined
Feb 19, 2002
Messages
43,293
I would not run a timer event on a form that allows updates. I would make the list form not updateable and use the double-click event to open an edit form.

The point of the timer is to show the availability of the porters. Let it do just that and let the timer refresh it to reflect updates by other users.
 

Users who are viewing this thread

Top Bottom