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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requiring a textbox, but only if its visible

Status
Not open for further replies.

rutkus

Programmer
Aug 28, 2001
41
0
0
US
How would I go about making a textbox required?? but only if a certain criteria is met and the textbox then becomes visible in the form.

Should i just make it required in the table?? maybe a validation rule?? Or should(could?) this be done through coding? Maybe something in the GotFocus "stage"??(sorry dont know terminology)

All help would be appreciated.

Thanks
Omar Qureshy
DSC Networks
 
I'm sure there are many ways to do this. I don't know what your condition to show the text box is. But if it was a simple boolean operator try this...

If Condition Then
Me.txtBox.Visible=True
Else
Me.txtBox.Visible=False
End If

The Forms AfterUpdate event could then be used to check the text box
For example...
If Len(Me.txtBox)=0 Then
msgbox "Please fill in the text box!"
DoCmd.CancelEvent
End If

These are just a couple of quick thoughts I had. Hope they help
 
Hey KenG,

I had the condition part setup but i wasnt sure about making the textbox "required". Ive never used Len, but it seems its for a number(??), i need an explanation(string), I dont know if that makes sense, if i still need to use Len, then = 0 works for string as well?? how about = Null or ="" ??? Just throwing things out here

Is this the best way to go about forcing someone to enter text in a textbox?? how about in table form?? although im aiming at making it form only for users.

I hope I make sense here, thanks for your help.

Omar Qureshy
DSC Networks
 
The Len() function will return the length of a string, so if the user enters nothing in the txtBox then Len(txtBox)=0

I don't know if you can make a field conditionally required at the table level. I think either a field is required or it is not.

You can use IsNull() or IsEmpty() or check if the txtBox = "". I've just grown accustomed to using Len()=0.
 
Dear rutkus, dear keng


First of all you cannot make a field be conditionally required at table level. So Keng is right saying a field is required or not.
Marking a field on table level as required, means it is required in all cases.
So if there are conditions a field is not required, it is not required in all cases and that's why it is then not marked required.

To have the field in the textbox ONLY be required if the textbox is visible, you need to add this to your if-clause

If Len(Me.txtBox)=0 AND Me.txtBox.Visible=True Then
msgbox "Please fill in the text box!"
DoCmd.CancelEvent
End If
otherwise this would give the user the message even if the textbox is not shown, making it hard for him to enter data. ;-)

FYI
the function len(string) calculates the number of letters in a string.

If there is a default text in the textbox, you should try:

if me.txtbox.text = "default text" AND me.txtbox.visible = true then


Hope this helps


Astrid
 
hmmmmm,

maybe im not doing this right, heres how my code looks:

If Len(Me.Processing_Comments) = 0 And Me.Processing_Comments.Visible = True Then
MsgBox "Please Explain Yourself"
DoCmd.CancelEvent
End If

I try it both with and without the .Visible = True and nothing happens, you can enter info, not enter info, tab through, whatever you want.

Maybe its my programming, i dont know, any suggestions would be great, thank you

Omar Qureshy
DSC Networks
 
Thanks Sawatzky for correcting my last post.

Are you first setting .Visible = False if the condition is not met?
What is the default setting for the text box (if you haven't changed it, it will be set to True)

If the text box is visible then the user can use it.
Use the On_Current event for the form to check the condition for the record and set the text box .visible = false/true.

Then the BeforeUpdate event will check if the text box is filled if it is visible.
 
Dear rutkus,

what is the exact behaviour you expect of the textbox:

1) After it got the focus it must be filled before user can leave it?
2) it must be filled before user can leave the form
3) it must be filled when a certain button is pressed?
4 ) other possibilities...

You should think about this to find out which event should be catched to check whether the box is filled or not.

regards Astrid

 
hey kenG and sawatzky,

The default setting for the textbox is .visible = false, the textbox only shows up on the form if the approveddate is more than a week past the receiveddate.

I have that part programmed though, and the textbox only appears when the criteria is met. My problem is that i need to make it, so the user is required to enter something in the textbox, an explanation of why it took longer than a week to process, if the textbox is visible. Writing this, ive just realized, well how about if the person enters the wrong date, the textbox pops up and then it wont let them go back and change the date. I guess it will once they enter something in, but then when they fix it, the textbox will dissappear and the data will stay in the table.

hhhmmmmmmmmm, this may end up causing some unnecessary entries, maybe i should also set it, so if the explanation is being set back to .visible = false then a msgbox asks if this explanation remains valid. I figure, if the explanation is needed then the textbox wouldnt be set back to .visible = false, but if the person goes back to the date after theyve been forced to enter something, and im sure theyll just enter some random letters or something to that effect, unless theyre really dumb...

So i guess that property should be set in the approveddate_afterupdate event. Maybe something like:

if len(me.processing_comments) <> 0 AND me.processing_comments.visible = false then
msgbox &quot;Whatever&quot;
end if

well this is just a thought, i hope ive been clear, sometimes i just type as things rush through my head, let me know your opinions please, i really hope theres a feasible way to go about this.

Omar Qureshy
SUN SPOTS
 
OK, new thought, unrelated yet somewhat along the same lines.

In my form, the users enter an invoice, received and approved date.

I wanted to set it so if they entered a received date that was before the invoice date, it would msgbox them and have them reenter a date, as that would be impossible, same thing between received and approved date.

Heres my code, set during the receiveddate afterupdate event:

If [InvoiceDate] > [ReceivedDate] Then
MsgBox &quot;How Can You Receive An Invoice Before It Was Created??&quot;
Form_InvoiceLog.ReceivedDate.SetFocus
End If

Heres my problem, it doesnt setfocus on the receiveddate, it just continues to the next textbox. Maybe theres a better way to go about telling it to stay on the receiveddate until the user enters a date beyond the invoice date. Maybe i should use the LostFocus event?? If i remember correctly, even if i get it to setfocus back on the receiveddate, the user would still be able to just tab to the next textbox, since there was no update, for the afterupdate event to check for the criteria. Does this make sense??

Thanks for your help guys,

Omar Qureshy
DSC Networks
 
Dear rutkus,

In fact this problem seems to be a typical Lost_focus event thing.

But Perhaps you could also set the textbox 's property validation rule (in german it is called Gültigkeitsregel) to > [invoice date] .
And then you could use the validation message property to show the users, what you want from them.

HTH

regars Astrid


 
Hey sawatzky,

Thanks for the idea with the validation rule, but when i tried it in access(table design view) it said i couldnt use another column as a validation rule, so im assuming you meant in VB, ive been trying to figure it out but dont quite understand how to set validation rules through code.

Heres what i have so far:

Private Sub ReceivedDate_LostFocus()

Dim RecValidRule As String
Dim RecValidText As String
Dim RecTblName As String
Dim intX As Integer

RecTblName = &quot;Invoices&quot;
RecValidRule = &quot;ReceivedDate > InvoiceDate&quot;
RecValidText = &quot;Enter a Received Date that is later than the Invoice Date.&quot;
intX = SetTableValidation(RecTblName, RecValidRule, RecValidText).Rece

End Sub

Function SetTableValidation(RecTblName As String, _
RecValidRule As String, RecValidText As String) _
As Integer

Dim dbs As Database, tdf As TableDef

Set dbs = CurrentDb
Set tdf = dbs.TableDefs(RecTblName)
tdf.ValidationRule = RecValidRule
tdf.ValidationText = RecValidText
End Function

The error i get is on the Set tdf = dbs.tabledef(RecTblName) line.

It says this item not found in collection, i had a somewhat similar error when working with querydef, but i checked the references section and the DAO 3.6 library is being used.

If anyone knows how to set validation rules like this, or perhaps set them in access, even if youre using another field in its' validation rule.

Any and all help would be appreciated.

Thank You
Omar Qureshy
DSC Networks
 
Dear rutkus,

If you want to set the validation rule for your textbox you won't do it on table level or query level but on Form level.

As you want people not to type in false data into your textbox, it is the textbox' work to controle that.

1. possibility
You could use the textbox_gotfocus event to actualize the value of your validation rule.

priate sub textbox1_gotFocus()

textbox1.validationrule = &quot;>&quot; & cdate(Invoicedate)

end sub

The validationtext can be set at design time.

2. Possibility
You could use the lostfocus event

private sub textbox1_lostfocus()
if cdate(textbox1.text) < cdate(nvoicedate) then
msgbox(&quot;your message&quot;)
end if
end sub


regards Astrid

PS.: I allways try to explain my forms, what I want them to do, the way I would explain it to a 3 year old child:
Thinking a lot on how to say it the easiest possible.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top