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!

How to set: If no data is entered on form, you can't continue?

Status
Not open for further replies.

McWhorter

Technical User
Jul 18, 2002
21
0
0
US
I have a form that allows for data entry. At the bottom is a control box that, when clicked, closes the form and opens a report.

Currently you can click the box without entering any data and it will close the form and open the report. How can I stop this? I want it to only open that report IF data is entered into the form. I have several fields marks as "required", but it still goes on...

Thanks for reading.
 
Try this..
in the on click event of the report opening button:

Sub ButtonOn_Click
If txtBox = "" Then 'Or would it be If txtBox Is Null?, I
think it's ""
MsgBox ("TxtBox Cannot be Empty, please enter the appropriate data and try again.")
txtBox.SetFocus
Else
DoCmd.OpenReport "rptMyReport" acPreview 'acpreview ... not sure exactly what it is, but VBA will tell you
End Sub

That should work! Get to know your ifs and thens.

-Josh ------------------
-JPeters
Got a helpful tip for Access Users? Check out and contribute to 'How to Keep Your Databases from becoming Overwhelming!'
thread181-293590
jpeters@guidemail.com
------------------
 
OK, I kind of got the IF-THEN statement to work. It works if I put a concrete statement like IF [fieldname] = "7" , but it wont work if i say IF [fieldname] = "" or Null.

Not sure why.
Here is the code I'm using.

If [FieldName] = "" Then
MsgBox ("Must enter data to Continue")
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
Dim stDocName As String
stDocName = "Last_SIRF_Entered"
DoCmd.OpenReport stDocName, acPreview
End If

I've tried:
If [FieldName] = Null Then
If [FieldName] is Null Then
If [FieldName] is Empty Then
If [FieldName] = "" Then
If [FieldName] is "" Then
None of them work. They all act as if the field is not null.
It does work if the following is entered and I enter "7" in that field.
If [FieldName] = "7"


Not sure what else to try.


 
You're trying to get some sort of an "or" statement. The ElseIf statement might work for you ...

Also, you could just default the value of that text or combo box in it's properties to "", that way if they haven't touched it, it's "" and you can use "" as your isnull value - voila!

Default Value is in the properties under the Data tab.

In your Code Above:
I'm assuming that you are working off of a form. Fieldname here should be the actual name of the combo box or text box that you are working with, not it's source data from a table.

Also, after the MsgBox, set the focus back to the text/combo box that needs data. ie: [FieldName].SetFocus Why the brackets, btw? Did you open up a recordset?

I've got tons more info in my head if you need more help.. just keep posting.

-Josh ------------------
-JPeters
Got a helpful tip for Access Users? Check out and contribute to 'How to Keep Your Databases from becoming Overwhelming!'
thread181-293590
jpeters@guidemail.com
------------------
 
All I was missing was to set the default value to ""

Now it works beautifully. I'll have to remember that the next time I need to use it.

Thanks for all of your quick responses and help.

Jeff
 
By adding the default value of "" makes it work. The problem now is that if you put a space in that field it continues on. There is still no data present, but a single space ruins it. Any more thoughts.

Jeff
 
How often is this going to be an issue? Can you user-train them not to hit space in that field?

-Josh ------------------
-JPeters
Got a helpful tip for Access Users? Check out and contribute to 'How to Keep Your Databases from becoming Overwhelming!'
thread181-293590
jpeters@guidemail.com
------------------
 
Complements of "SAMS Teach Yourself VB 6.0 in 21 days" Page 234:

If IsNull(txtHoursWorked) Then
intMsg = MsgBox("You Didn't enter hours worked!", vbOKOnly)
Else
intMsg = MsgBox("Thanks for entering hours worked!", vbOKOnly)
End If

"This If statement checks to ensure that the user typed something in the field before the program continues."
Straight out the book.

Hope that helps...

By the way the reason your space registers is because in ASCII a space is not Null it is considered a value.

So if you like your code you need to add another check for spaces.....

[peace]
 
Since you can't check that the data entered actually makes sense then you can only check for Null or blanks
if IsNull([field]) or Trim$([field]) = "" then
don't load report
send message
whatever
end if

Hope this helps Raymondo
raymondo@rossar.net
 
These are excellent suggestions, I actually prefer them to mine. I need to buy that book, "Sams: teach VB in 20 days" or whatever. I must buy it!

-Josh ------------------
-JPeters
Got a helpful tip for Access Users? Check out and contribute to 'How to Keep Your Databases from becoming Overwhelming!'
thread181-293590
jpeters@guidemail.com
------------------
 

Use the unload event of the form. Test one of your required field for nothing, you probably would want to use the nz function. If the result of the test is zero, you have no data. At that point set cancel = true,and set the forms focus to the first form variable, and then exit the event. Your form will not close and the curser should be on the first variable.
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
After some more testing, I have it working. I could not get any functions with the word "Null" to work at any time. Dont quite know why. But, the IF/Then statement checks to see if anything was entered. The field is set to "Required" so if spaces are entered, they still return a null value.

So, one way or the other, some type of charactor besides a space must be entered.

Thanks again for all of your useful suggestions.
My next problem is this one...
thread702-317588 one should be easy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top