Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide or show control when form opens 2

Status
Not open for further replies.

ad2

Technical User
Dec 31, 2002
186
US
Hi,

When a form opens and the subject id field is blank, I want a command button to be hidden. If the subject id is filled in I want the button to show. The code I have below is not working - the button is visible if the subject id is blank or is filled in. Any ideas?

Thanks,
Ad2


'Private Sub Form_Load()
' If IsNull(SubID) Then
' cmdReport.Visible = False
' Else
' cmdReport.Visible = True
' End If
'End Sub
 
May be because "isNull" only checks for a null value. It will not pick up a space " ", and empty string "", or and empty field. The better check is

if trim(subID & " ") = "" then
this handles a null, space, and empty string
 
How are ya ad2 . . .

. . . or to check for both [blue]Null[/blue] and a null string [blue]""[/blue] :
Code:
[blue]   If Nz(subID, "") = "" Then[/blue]


Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Hi,

I've tried both you suggestion and the button is always visible.

I thought about using new record as a check instead of the subject id, but my code for that isn't working either:

If record = acNewRec Then
cmdReport.Visible = False
Else
cmdReport.Visible = True
End If
 
ad2 . . .

If the single quotations before each line shown in your post origination are truly there, then [blue]the code is inactive![/blue] . . . [blue]Remove them![/blue]

Or there's a problem with names. See the [blue]Name[/blue] property of the control to be sure . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Hi AceMan1,

No there aren't any single quotes in the code. Names are correct.

So far none of the suggestions have worked.

The button's visible property itself is set to Yes, which I think should be correct.

Thanks,
 
Have you tried

If me.newrecord Then
cmdReport.Visible = False
Else
cmdReport.Visible = True
End If

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Ken Reay,

Yes, I did try you excellent suggestion. But button was visible if on existing record or new record.

Ad2
 
Debug to see if your code is even firing. Put in a msgbox

msgbox "The Code is Firing"
msgbox "The value of subID is " & subID
msgbox "Record is new " & me.newrecord
 
Hi
OK, there is something odd here, since any of the methods suggested should work.

You initially said you wanted the button hidden if a given column was blank, but this then switched to hiding the button if on a new record.

You also said you have the code in the onload event of the form.

How do you actual initiate the addition of a new record?,


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Just figured it out. I had it on the wrong Event. I moved the new record code fron onLoad to onCurrent and it works fine.


Thanks everyone for your time!

Ad2
 
Hi

"I moved the new record code fron onLoad to onCurrent"

Just what I was leading up to, pleased you got it

thanks for the star by the way


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
As evidenced here, everybody has a favorite hack for checking whether a textbox holds data or not, which brings me to a question that I've had for a while:

How, other than importing data from another application, do you get a space or empty string in a textbox in Access?

You can't do it by entering the textbox and leaving without entering data.

You can't do it by entering entering data and then deleting it.

You can't do it by entering data and backspacing thru it.

You can't do it by entering spaces and then exiting the textbox.

All of these things leave a the textbox with a Null value, and IsNull() works just fine in testing for this.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
you can do it in code
calculated field
default value
import

or the most obvious to the common user

If you have a memo or text field and the following are met
1) required value = yes
2) Allow zero length = true

You can do it by entering entering data and then deleting it.

You can do it by entering data and backspacing thru it.

You can do it by entering spaces and then exiting the textbox



 
missinglinq . . .

It appears your missing the use of what you question.

As already shown by [blue]MajP[/blue], its easy for a bound textbox control to contain a [blue]Null String[/blue]. Fainting to unbound which typically returns [blue]Null[/blue], it easy to set it to a [blue]Null String[/blue]! There are other ways to set [blue]Null[/blue] or [blue]Null String ""[/blue], but I don't want to loose my point.

The codes . . .
Code:
[blue]   If trim(subID & " ") = "" then
      and
   If Nz(subID, "") = "" Then[/blue]
Both say: I don't care if its a [blue]Null[/blue] or a [blue]Null String[/blue], [purple]I'll let you know if there's any data in the control![/purple]
This appeals to me as a programmer and is exactly why I use it . . . I don't care! . . . [blue]I just wanna know if there's data in the control![/blue] . . . [blue]Null[/blue], [blue]Null String[/blue], or not! . . . and thats exactly what it does.

Consider a secnario where you validating in the [blue]BeforeUpdate[/blue] event of a form, and enumerating thru its controls . . . some numeric, some Text with AlllowZero Length=Yes, some unbound and set to [blue]Null String[/blue]. You'd acutally actually have to detect datatype, and if text, wether [blue]AllowZeroLength[/blue] was enabled or not! . . . Let alone asking posters these very questions before you decide what course to take.

These codes do us a big favor in there non-descrimination, and simply let us know if data has been entered!

I don't care what happens on import, I wanna know if there's any empty data!

[blue]Your Thoughts? . . .[/blue]



Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
But as I said there is three cases:
1. null
2. ""
3. " ", " ", " " etc.

this gets 2 cases only (1,2):
If Nz(subID, "") = "" Then

this gets all three:
if trim(subID & " ") = ""

If you are importing data from something like Excel or a text file, the blank space/s case can occur pretty easily.
 
Hi

I prefer the catch all

If Len(Trim(Nz(txtbox,"")&"")) = 0 Then

yes, I know it may not be necessary in some instances, but surely the point is to write robust code?

Just my opinion.

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
I prefer the catch all
Like the following ?
If Trim(txtbox & "") = "" Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
MajP,

I don't know WHICH way is the "proper" way, buy the TRIM statement you posed first WORKS and works well. No more isNull OR "" coding for me! Here's a star and thanks!

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top