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

Suggestions for Requiring Entries

Status
Not open for further replies.

rutkus

Programmer
Aug 28, 2001
41
US
I have a hidden textbox and I'm trying to make it "required", but only if its visible.

I used the Len argument, heres my code:

this is in the BeforeUpdate event
If Len(Me.Processing_Comments) = Null Or 0 Then
MsgBox "Please Explain Yourself"
Cancel = True
End If

I figured the best way to do this would be to have it read the number of characters that have been written into the textbox and if it comes back null or 0 then cancel the update.

BUT, its only working if I type a letter in and then erase the letter. If anyone has any ideas or other ways of going about this, Id really appreciate it.

Thanks
HI-Priest
 
In you table set the field as required.

jaz
 
Having re-read your post I realize you have it hidden so it won't always be visible therefore it isn't always required. So what you can do is place a hidden checkbox on your form and based on what makes the object visible set the check box to True. If the checkbox is then true in your BeforeUpdate Event of the form you have the required data you need so

If OKToClose = True Then
GoTo SubExit
Else
Cancel = Not OKToClose
end if
Hope that gives you some ideas? Use though the BeforeUpdate Event for the form in this case.

jaz
 
Hey jaz,

Thanks alot for your help in my other post, setting the code in the beforeupdate event stopped it from moving to the next textbox when the criteria was met.

As far as this go, maybe im a bit confused...

You say set a checkbox to be checked if/when the textbox becomes visible? How would this make the textbox required?? I dont quite understand your code, sorry.

I can see setting something up in the beforeupdate event, so that IF a character is in the textbox then the checkbox is checked. But, I dont know much about coding that, Id have to sit and read help files for awhile.

Thanks though and if you could expand on your explanation, it would help clear things up for me,

Peace
HI-Priest

 
OK here goes, not knowing what makes your object visible I am suggesting you try this. Once whatever triggers the object to become visible your next line of code would be blnOKToClose=0 *remember to change this back to true* if for any reason the object gets re-hidden.

Now on the before update event of the form level you can check if you are true *meaning your object is filled* if not then it's false so cancel the event.

Just remember to place the Dimension like this.

Option Compare Database
Option Explicit

Private blnOKToClose As Boolean

So whatever now triggers your object from being un-hidden add blnOKToClose = 0 now on the BeforeUpdateEvent of the object the one you require data in check if there is a value and if there is blnOKtoClose = -1 now on the BeforeUpdate Event of the form check to see if you got what you need.

If blnOkToClose = 0 then
MsgBox"Place Message here."
Me("MyField").setfocus
Cancel=true
end if

Hope that gives you an idea?

jaz

 
How about this, it's not rocket science, but it does work:
If you have a command button that closes the form, or does something else, put something like this in the On Click event:
Code:
If Me.Processing_Comments.Visible Then
   If IsNull(Me.Processing_Comments) Then
   MsgBox ("Please Explain Yourself")
   End If
End If

 
Alright, some of that makes sense, some of it doesnt...

Where/how/when do i define blnoktoclose?? ive never used a boolean, does it return the data as a number defined as the set of characters?? or what?

Heres more detail on my situation,

I have two textboxes with dates, if, in the second textbox the person enters a date beyond 7 day from the date on the first textbox then the third textbox pops up and a msgbox appears asking them to explain their delay. From there I need to "require" the user to enter something in the textbox when its visible.

hhhhmmmmmmmmm, something else that just popped in my head, how about if they want to go back and change the date, like if they put in the wrong year or something, hhhmmmmmmmm

considering how im going about this, if they enter random info to satisfy the "requirement" then go back and change the date, the textbox will become hidden again and the random info will stay in the record.

any suggestions??

HI-Priest
 
Boolean is simply Yes/No. Do I have data in there? blnOKToClose=Yes simple as that so based on yes/no determines if we have correct data.

We need a place to store the results of Yes/No so we will make it private by placing the statement mentioned above at the top of the module so no matter where we change the yes or no from within our module we have the proper results stored. So Private blnOKToClose As Boolean goes at the top of your module.

Now as you mention on the second date box is where you make the third object visible so at that point is where I would make blnOKToClose = 0 which is false *our hidden object is now open but we yet have data in it* but if they correct themselves and our criteria is changed to a correct number then blnOKToClose= -1 *true* we got a good number so we don't need the hidden box open but we need to get out of here.

I would probably code the above on the OnChange Event of the second object this way you can handle the fact if they change the date as you mentioned you can also change blnOKToClose to tue/false and you need to re-hide the object you can handle it on that event.

Now on the object that became visible you want to tell the form OK I got what I need so on the BeforeUpdate Event of the now visible object you can do something like this
If Len(Me(&quot;MyObject&quot;))<>0 then
blnOKToClose=-1 'True
Else
MsgBox&quot;Whatever&quot;
cancel=true
end if

Finally on the BeforeUpdate Event of the Form just check if it's OK to Close?
If blnOKToClose=0 then
'* We got a False so we are missing our data
MsgBox&quot;Whatever&quot;
Me(&quot;Whatever&quot;).setfocus 'if you like
Cancel=True
Goto SubExit
Else
'We got everything I'm outta here
end if

Bear in mind this is just a suggestion as mentioned above something simple would work also, I prefer to control those what if's this way just my preference.

jaz
 
Maybe something like this

1. Create new sub procedure - place it in a form's module just below the Option Explicit statement

'procedure will check the difference between two dates
'and if >7, display third text box,

Sub CheckDateDiff()
If Isnull(Date1) Or IsNull(Date2) Then
txt3=Null
txt3.Visible=False
Exit Sub
End If

If Date2 - Date1 > 7 then
txt3.Visible=True
msgbox &quot;Explain...&quot;
Else
txt3=Null
txt3.Visible=False
End If
End Sub

In both Date1 and Date2 AfterUpdate events call this sub

Sub Date1_AfterUpdate()
CheckDateDiff
End Sub

In the Form_BeforUpdate event

Sub Form_BeforeUpdate(Cancel as Integer)
If txt3.Visible=True Then
Cancel = True
End If
End Sub



 
I forgot one statement on BeforeUpdate

Sub Form_BeforeUpdate(Cancel as Integer)
If txt3.Visible=True and IsNull(txt3) Then
Cancel = True
End If
End Sub




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top