Set The Visibility of A Control Based Off A Combo Box Option

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Did anyone have a code snippet to make this happen. I have tried but I am new
to VB and am confused by some of its syntax. My code snippet that I have
tried Is as follows.

Private Sub Heard_About_Us__AfterUpdate()
If Heard_About_Us = "Other" Or "POSTED ELSEWHERE" Or "WORD OF MOUTH" Then
Heard_About_Us_Explain.IsVisible = True
Else
Heard_About_Us_Explain.IsVisible = False
End If
End Sub

The Combo Box Is Named Heard_About_Us and has 6 options in it.
The Control I want to Make Visible Based On the Combo Box Criteria is Called
Heard_About_Us_Explain .

Can U help Me Or anyone Else for that matter ?
 
Me.Private Sub Heard_About_Us__AfterUpdate()
If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = "POSTED ELSEWHERE" Or
Me.Heard_About_Us = "WORD OF MOUTH" Then
Me.Heard_About_Us_Explain.Visible = True
Else
Heard_About_Us_Explain.Visible = False
End If
End Sub
 
For Some Reason this Code Dosent Seem to Work. What is the Me. Thing before
the Sub name ?
 
It is not before the sub name. The sub name is Sub
Heard_About_Us__AfterUpdate(). It is to qualify the control name. Although
not always a necessity, it is a good idea that will avoid any ambiguity in
naming. Me is a shorthand qualifier that references the current form.
Rather than using:
Forms!MyFormName!MyControlName
You can use
Me.MyControlName
Or
Me!MyControlName

Just plain MyControlName will usually work, but it is better to qualify your
object references.

As to it not working, It looks okay, but you never know. Do you know how to
trace code in Debug mode?
 
I cant say as I do ... Like I said Relitively new .. I know this must be kind
of frustrating for you to answer the same questions over and over again.
Thank you so much
 
Not a problem at all.
So here is how you can debug your code to see what you are actuaill getting.
Open your VB Editor so the code you want to test is displayed.
Place the cursor where you want the code to stop. In this case, it will be
the first line of executable code. (not tags or dims, but a line of code that
does something)
Press F9. You will see that line of code change color.
Open the form in form view and do whatever action is necessary to make the
code execute. In this case, make a selection in the combo box.
Your code should then appear. If it does not, select the VB Editor from the
task bar.
You should then see the line of code you highlighted earlier is now yellow.
It is about to execute this code.
Press F8
The highlighted code will execute and the next line of code to execute will
be highlighted in yellow.
At any time the code is stopped, you can position your cursor over a
variable and it will display a box showing the current value. You can also
see the value of any object containing data or the setting of a property by
referencing it in the immediate window. For example:

?forms!MyFormName!Heard_About_Us
will display whatever the current selection is in the combo box.
?forms!MyFormName!Heard_About_Us.RowSource
will display the the string of the current value of the rowsource property.

Step through your code to be sure it is behaving as you expect.
Check you variable values and property settings to ensure they contain the
values you expect.
 
I just keep getting Syntax error Codes. For the first two lines of the Code

Me.Private Sub Heard_About_Us_AfterUpdate()
If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = "POSTED ELSEWHERE" Or
Me.Heard_About_Us = "WORD OF MOUTH" Then"
 
Lose the me. before the word Private (Klatuu - think you missed your typo)
and the speech mark after the word Then.


"Set The Visibility of A Control"
 
So theoretically this code should work

Private Sub Heard_About_Us_AfterUpdate()
If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = "POSTED ELSEWHERE" Or
Me.Heard_About_Us = "WORD OF MOUTH" Then
Me.Heard_About_Us_Explain.Visible = True
Else
Heard_About_Us_Explain.Visible = False
End If
End Sub

Because when I try to run this code I get all sorts of Compiler Errors

* COMPILER ERROR : EXPECTED EXPRESSION

And the following code is highlighted in Red
If Me.Heard_About_Us= "Other" Or Me.Heard_About_Us = "POSTED ELSEWHERE" Or
Me.Heard_About_Us = "WORD OF MOUTH" Then
 
That line of code needs to be all on one line in your VB Editor. If you need
to continue a line of code in VBA to another line in the editor so it will
all stay on the screen without scrolling, use the line continuation
character, which is an underline and must have a space before it:

If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = _
"POSTED ELSEWHERE" Or _
Me.Heard_About_Us = "WORD OF MOUTH" Then
Me.Heard_About_Us_Explain.Visible = True
Else
Me.Heard_About_Us_Explain.Visible = False
End If
End Sub
 
The perils of word wrap in newsgroup readers! That needs to be a single
line, so either move the 'Word of mouth' part back up onto the previous line
or, better is to use a line continuation character like this...

If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = "POSTED ELSEWHERE" Or
_
Me.Heard_About_Us = "WORD OF MOUTH" Then

The _ just tells Access to treat that as a single line. The space after the
Or is compulsory.


"Set The Visibility of A Control"
 
AWESOME GUYS ... BOTH OF YOU TRULY ROCK !!!!!

Klatuu said:
That line of code needs to be all on one line in your VB Editor. If you need
to continue a line of code in VBA to another line in the editor so it will
all stay on the screen without scrolling, use the line continuation
character, which is an underline and must have a space before it:

If Me.Heard_About_Us = "Other" Or Me.Heard_About_Us = _
"POSTED ELSEWHERE" Or _
Me.Heard_About_Us = "WORD OF MOUTH" Then
Me.Heard_About_Us_Explain.Visible = True
Else
Me.Heard_About_Us_Explain.Visible = False
End If
End Sub
 
Back
Top