Possible? Form w/ show/hide prepop memo/textbox reliant upon checkbox / radio button? (1 Viewer)

mqnf

New member
Local time
Today, 08:49
Joined
Jan 3, 2019
Messages
4
Hi all,

I would greatly appreciate some guidance on this if you are so inclined. I have researched the issue and am unable to find a write-up. I don't know if what I am attempting is even possible.

I am preparing an evaluation form with checkboxes for the different items to be considered. When a checkbox is selected, I would then like for a textbox to appear which is uniquely tied to that specifically selected checkbox. That textbox would be pre-populated with text that explains more about whatever checkbox was selected.

The form, upon completion, will be used in a final report that hides all unselected checkboxes (and their respective textboxes).

As an example of the practical application:
Checkbox "cbTomatoes" is selected by the user.
Texbox "tbTomatoes" would then become visible on the form and subject to being printed as a Memo (or help file, etc.), and provide the following explanation "There are many varieties of cultivated tomatoes. One popular variety is Beefsteak tomato which on average weighs 1 pound. Blah, blah, blah..."

Here is some example code which is not working and yields an error message:
=====================
Private Sub cbTomatoes_Click()
If Me.cbTomatoes = -1 Then
Me.tbTomatoes.Visible = True
Else
Me.tbTomatoes.Visible = False
Me.cbTomatoes.Value = Null
End If

Private Sub Other_AfterUpdate()
If Me.cbTomatoes = -1 Then
Me.tbTomatoes.Visible = True
Else
Me.tbTomatoes.Visible = False
Me.cbTomatoes.Value = Null
End If

End Sub
=====================
The error message reads as follows:
The expression On Click you entered as the event property setting produced the following error: Invalid outside procedure. *The expression may not result in the name of the macro, the name of the user-defined function, or [Event Procedure]. *There may have been an error evaluating the function, event, or macro.

I am not using a macro. As for the event (On Click or After Update), when I use Code Builder, upon saving that code Access automatically populates the respective event with [Event Procedure], so the error message is confusing in that regard.

System: Win10, Access 2007

Many thanks for any assistance you might provide.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:49
Joined
Oct 29, 2018
Messages
21,447
Hi. Are you using a single view or a continuous view form?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:49
Joined
May 21, 2018
Messages
8,519
Code:
Private Sub cbTomatoes_Click()
If Me.cbTomatoes = -1 Then
Me.tbTomatoes.Visible = True
Else
Me.tbTomatoes.Visible = False
Me.cbTomatoes.Value = Null
End If
'There is no end sub here so the following line in invalid outside of a procedure.

Code:
Private Sub Other_AfterUpdate()
If Me.cbTomatoes = -1 Then
Me.tbTomatoes.Visible = True
Else
Me.tbTomatoes.Visible = False
Me.cbTomatoes.Value = Null
End If

End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:49
Joined
May 21, 2018
Messages
8,519
Whenever you get this error
*The expression may not result in the name of the macro, the name of the user-defined function, or [Event Procedure]. *There may have been an error evaluating the function, event, or macro.
The best thing is to go into vbe and select Compile. It normally will help point to the error.
 

mqnf

New member
Local time
Today, 08:49
Joined
Jan 3, 2019
Messages
4
Thank you both.

theDBGuy: It is a single form.
MajP: I added the End Sub. On the errors, I looked with fresh eyes and found somehow VBe inserted "Compare Database" at the very top of the form. Once I removed that, the error messages went away. Compile shows no errors.

However, I am still not getting pre-populated text in the textbox, and it is not becoming visible when cbTomatoes it clicked upon. Seems Visible setting to "No" in tbTomatoes Properties may be over-riding whatever VB code I use.

Anyone know how to cause a hidden textbox to appear when a checkbox is clicked/updated, and have that textbox appear with a pre-written permanent message?
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 08:49
Joined
Oct 29, 2018
Messages
21,447
Hi. If the checkbox is bound to a field, then I would recommend using its AfterUpdate event. For example:


Code:
Private Sub cbTomatoes_AfterUpdate()
    Me.tbTomatoes.Visible = Me.cbTomatoes
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:49
Joined
May 21, 2018
Messages
8,519
VBe inserted "Compare Database" at the very top of the form.
That should not be a problem. Unless it really said Compare Database instead of Option Compare Database.

Normally at the top of the module you should see

Option Explicit
Option Compare Database

You should always have option Explicit. The pain of not having that can be severe. This forces you to declare all variables and greatly aids in debugging

https://docs.microsoft.com/en-us/do...eference/statements/option-explicit-statement

Option Compare deals with how strings are compared (A = only A or, A = a & A =A)
https://docs.microsoft.com/en-us/of.../user-interface-help/option-compare-statement

If there is no option compare at the top then the comparison defaults to binary so (A = A, A<>a)
 

Users who are viewing this thread

Top Bottom