By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,383 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Hiding a form: If conditions

P: 6
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
Share this Question
Share on Google+
6 Replies


MindBender77
100+
P: 234
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

P: 6
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
Expert 100+
P: 1,295
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

P: 6
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
100+
P: 234
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

P: 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.