Setfocus not working (1 Viewer)

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
Hi,

I have been fiddling with my database and I need some help.

It’s not the easiest thing to explain but here goes…
I have a main menu with a tree view on the left. When the user clicks on an item in here, I have different forms appearing. For example if the user clicks on the top level, they will get a different form than if they click on the second level.
I am detecting which level they are clicking on and showing the correct form on the right.

I have one form visible and another invisible. When I need to change the form on the right, I make it visible, set it to have the focus and then set the old form to invisible. When I do this, I get an error saying
“Run time error ‘2449’ ”
“There is an invalid method in an expression”
The line it crashes on is
Form_frmOne.SetFocus

I have tried setting the focus to other things like text boxes. When I do this, then try to make the form invisible, it comes up with;
“Run time error ‘2165 ”
“You cant hide a control that has the focus”
On the following line
Form_frmTwo.Visible = False

And I know the focus is on a textbox before I do this because I can look at the form and see where the cursor is blinking.

I have done this before on a simpler database but I think access is getting confused because I am using sub forms and tab controls. Should using these affect my code?

The code on the tree view click looks like the following.


If selected = "SectorData" Then
sValue = Me.SectorText.Value
sValue2 = Me.SectionText.Value

sCriteria = " WHERE [tblSector.SectionName] = "
sCriteria = sCriteria + "'" & sValue & "'"
sCriteria = sCriteria + " AND [tblSection.SectionName] = "
sCriteria = sCriteria + "'" & sValue2 & "'"

Me.Text55.SetFocus

Form_frmOne.Visible = True
Form_ frmOne.SetFocus
Form_ frmTwo.Visible = False '2

sSql = "SELECT * from qrySectionLevel " & sCriteria
Form_ frmOne.Form.RecordSource = sSql
Form_ frmOne.Form.Requery
End If

If anyone could help, I would be very grateful.
Thanks,
Joe.
 

Mile-O

Back once again...
Local time
Today, 03:39
Joined
Dec 10, 2002
Messages
11,316
irishjoe said:
When I need to change the form on the right, I make it visible, set it to have the focus and then set the old form to invisible. When I do this, I get an error saying
“Run time error ‘2449’ ”
“There is an invalid method in an expression”
The line it crashes on is
Form_frmOne.SetFocus

You can only set the Focus to a form when it has not control to which the cursor can be assigned.

I have tried setting the focus to other things like text boxes. When I do this, then try to make the form invisible, it comes up with;
“Run time error ‘2165 ”
“You cant hide a control that has the focus”
On the following line
Form_frmTwo.Visible = False

As it says, the focus needs to be on a visible object. Hiding a form and then making it invisible will not work as the focus needs to be passed elsewhere first. Conside making the hidden form visible, setting the focus to a control on that, and then hiding the other form.
 

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
For your first comment, frmOne is visible so why cant I set the focus to it?

For your second comment, I have set the focus to a text box previously, which is on the main menu. Why could I not hide the form after this?

Thanks for replying.
Joe.
 

Mile-O

Back once again...
Local time
Today, 03:39
Joined
Dec 10, 2002
Messages
11,316
irishjoe said:
For your first comment, frmOne is visible so why cant I set the focus to it?

Because you have Active controls which can receive the focus. You can only, as I said, set the focus to a form when there is no control on that form to which the focus can be assigned.

Code:
For your second comment, I have set the focus to a text box previously, which is on the main menu.  Why could I not hide the form after this?

You can't hide the forum until the control on it with the focus has lost the focus. When a control has the focus, you can't hide the control, disable it, lock it because you have not told Access where to move the focus to in order to complete making the form invisible. You obviously can't set the focus to another control on the same form, you can't set the focus to a hidden form (or one of its controls if they are enabled), and you can't set the focus to a form if it's controls are enabled.

Do as I suggested:

  • You currently have two forms one hidden (Form1) and one not (Form2); you wish to swap this condition around
  • Make the Form1 visible
  • Set the focus to a control on Form1
  • Make Form2 invisible

set the focus to a control on the
 

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
So, to do as you suggested, I should
1) Make form1 visible : Form_frmOne.Visible = True
2) Give a control focus on form1 : Form_frmOne.Text65.SetFocus
3) Make form2 invisible : Form_ frmTwo.Visible = False

Text65 is a text box that I added to test this.
It still says that I cant set form2 to invisible so it must still have the focus.
Could it be anything to do with sub forms and tab tables?

Thanks for your help,
Joe.
 

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
For anyone who is reading this, I fixed the problem.
I added 2 lines of code to the onload part of the form.
Form_Form1.Visible = False
Form_Form2.Visible = False

This enabled me to make them visible and invisible whenever I liked.


Thanks to Mile-O-Phile for pointing me in the right direction...
 

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
Hi,

Could someone have a look to see where I am going wrong with this.
You can see above what my problem is. I thought I had fixed the problem but I hadn’t. I used a tab menu to solve the problem. I set the focus to another tab, made the forms invisible then set the focus back to the original tab.

That worked well but I have re-designed my user interface to get rid of the built in tab controls with nice buttons that do the same thing.

I have set the focus to something on the main menu and I can see that it is getting the focus each time I press a button but when I click on one of the forms that I am making invisible, it dies a horrible death. I still don’t know why because I am setting the focus to the tree view that I have on the main menu which isn’t connected to the forms that I am making visible and invisible.

I have read the answers from Mile-o-Phile but I still don’t understand why it is dying.
I have a procedure for making all the forms invisible. Before I run this, I was setting the focus to the treeview but it still goes tits up.

On another matter, has anyone learnt vb.net from already knowing vba? I want to learn about it but I don’t know how much of a difference in the language there is. I mainly know java and I’ve been told they are quite similar. Is that right?

Thanks to Mile-o-Phile for helping.

Joe.
 

irishjoe

Registered User.
Local time
Today, 03:39
Joined
Apr 2, 2004
Messages
34
I have attached a screenshot so you can see what i'm on about.

If the interface doesnt look ok, please feel free to criticise...
 

Attachments

  • Screenshot.jpg
    Screenshot.jpg
    52.1 KB · Views: 342

Users who are viewing this thread

Top Bottom