469,913 Members | 2,347 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

Hiding a form: If conditions

I'm a beginner programmer so bear with me if this seems very simple...

I have a subform which I don't want to have displayed until the user has entered first name, last name, extension, and chose a dept. from a combobox.

The If condition I tried for the first name was:

If Me.Employee_First_Name.Value = Null Then
Me.Report_Sub.Visible = False
Else
Me.Report_Sub.Visible = True
End If

But this does not hide the subform, it seems to do nothing. I also tried "" instead of Null but to no avail. If I try "Me.Employee_First_Name.Text" instead of .Value I get the error:

"You can't reference a property or method for a control unless the control has the focus."

I plan on adding in the conditions for last name, extension, and dept. eventually but can't get this first condition to work. The 'extension' is obviously set as a number so is default 0, so I have gotten this condition to work because I can get it to equal the default value of a new record:

If Me.Extension.Value = 0 Then

But the other 3 are killing me. I just want the subform hidden if all fields are null or empty. I also am not sure if the combobox for dept. should be set as .value as well or something different because it's a combobox. Please help me out! Thanks in advance.


- Adam
Jan 16 '08 #1
6 1587
MindBender77
234 100+
I'm a beginner programmer so bear with me if this seems very simple...

I have a subform which I don't want to have displayed until the user has entered first name, last name, extension, and chose a dept. from a combobox.

The If condition I tried for the first name was:

If Me.Employee_First_Name.Value = Null Then
Me.Report_Sub.Visible = False
Else
Me.Report_Sub.Visible = True
End If

But this does not hide the subform, it seems to do nothing. I also tried "" instead of Null but to no avail. If I try "Me.Employee_First_Name.Text" instead of .Value I get the error:
- Adam
Assuming your using a textbox for firstname, lastname and extension.
You could try something like this:

Expand|Select|Wrap|Line Numbers
  1. If textbox1.text = "" then
  2. Report_sub.visible = false
  3. else
  4. Report_sub.visible = true
  5. end if
  6.  
In comparison, a combobox would look something like this:
Expand|Select|Wrap|Line Numbers
  1. if combobox1.SelectedValue = "" then
  2. Report_sub.visible = false
  3. else
  4. Report_sub.visible = true
  5. end if
  6.  
Tip: I would put all of the criteria into the same if statement to make the code easier for you to read.

Hope this helps,
JS
Jan 16 '08 #2
I tried this code, and still got the same error as before:

"You can't reference a property or method for a control unless the control has the focus."

I have no idea what this means. Also, I am using Access 2000 and I assume an older version of VB as well because there is no 'SelectedValue' for my combobox, only 'SelLength' 'SelStart' and 'SelText'. I obviously used 'SelText' and get the same error as I did with the textboxes.

Side note: I am placing all of this code on the form itself under the Event "On Current." Is this correct?

Thanks for the reply, any other ideas?




Assuming your using a textbox for firstname, lastname and extension.
You could try something like this:

Expand|Select|Wrap|Line Numbers
  1. If textbox1.text = "" then
  2. Report_sub.visible = false
  3. else
  4. Report_sub.visible = true
  5. end if
  6.  
In comparison, a combobox would look something like this:
Expand|Select|Wrap|Line Numbers
  1. if combobox1.SelectedValue = "" then
  2. Report_sub.visible = false
  3. else
  4. Report_sub.visible = true
  5. end if
  6.  
Tip: I would put all of the criteria into the same if statement to make the code easier for you to read.

Hope this helps,
JS
Jan 16 '08 #3
kadghar
1,295 Expert 1GB
I tried this code, and still got the same error as before:

"You can't reference a property or method for a control unless the control has the focus."


Thanks for the reply, any other ideas?
you have to focus it before you can change its properties.

use SHOW or SETFOCUS depending if it's already shown and of the version of VB and after that, you can change its properties, such as visible.

HTH
Jan 16 '08 #4
Ok now it's getting more interesting...

I added the setfocus control, and have the following code:

Employee_Last_Name.SetFocus
If Employee_Last_Name.Text = "" Then
Me.Report_Sub.Visible = False
Else: Employee_Last_Name.Text = ""
Me.Report_Sub.Visible = True
End If


For some reason, when i scroll through my records, anywhere that there once was a last name, it gets erased. When i scroll to the next record, i can see the last name there for a split second then it disappears and is erased. I don't know why this code would have caused that. Any ideas??

The last name is set as a required field in its table settings if that has any effect on this.
Jan 16 '08 #5
MindBender77
234 100+
I tried this code, and still got the same error as before:

"You can't reference a property or method for a control unless the control has the focus."

I have no idea what this means. Also, I am using Access 2000
I was unaware you were using Access VBA. It's slightly different than regular VB and I agree that focus has to be set to modify.

Try something like this in your OnCurrent Event:
Expand|Select|Wrap|Line Numbers
  1. if textbox1 = "" then
  2. Report_sub.setfocus
  3. Report_sub.visible = false
  4. end if
  5.  
It's getting erased because of the OnCurrent Event. When you scroll, it's activating the Event.
Jan 16 '08 #6
Thanks for all of the help guys, input from both of you helped me solve the problem. To get it all to work, and in the same condition it ended up looking like this, in case somebody in the future has the same problem:

Expand|Select|Wrap|Line Numbers
  1. Employee_First_Name.SetFocus
  2.     If Employee_First_Name.Text = "" Then
  3.         Me.Report_Sub.Visible = False
  4.     ElseIf Employee_First_Name.Text <> "" Then
  5.         Employee_Last_Name.SetFocus
  6.         If Employee_Last_Name.Text = "" Then
  7.             Me.Report_Sub.Visible = False
  8.         ElseIf Employee_Last_Name.Text <> "" Then
  9.             Extension.SetFocus
  10.             If Extension.Text = "" Then
  11.                 Me.Report_Sub.Visible = False
  12.             ElseIf Extension.Text <> "" Then
  13.                 Group_Box.SetFocus
  14.                 If Group_Box.SelText = "" Then
  15.                     Me.Report_Sub.Visible = False
  16.                 ElseIf Group_Box.SelText <> "" Then
  17.                     Me.Report_Sub.Visible = True
  18.                 End If
  19.             End If
  20.         End If
  21.     End If
  22.  
Thanks again for the advice, you guys are awesome.
Jan 16 '08 #7

Post your reply

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

Similar topics

4 posts views Thread by James Greig | last post: by
10 posts views Thread by Gregory A Greenman | last post: by
2 posts views Thread by George Steward | last post: by
1 post views Thread by Bogdan Zamfir | last post: by
reply views Thread by Michelle Stone | last post: by
1 post views Thread by Doug | last post: by
11 posts views Thread by Alex | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.