Priority Of Functions

R

Raymond

Hi all,

I am having 2 functions for 2 buttons, 1 is DETAIL,1 is PRINT. Now, if
someone clicks PRINT button 1st, it gives an error message, "Null value
cannot be used as object", which is correct because some fields cannot be
left NULL in my form.

What I want is to have a code which gives a pop-up message that DETAIL
button is to be clicked first before PRINT button.

How can we do this?

I need this thing, since if the Print button is clicked, it gives the option
for DEBUG which would be a security lapse...

Your help would be acknowledged greatly, as always.

Thanks & Regards,
Raymond
 
W

Wayne-I-M

Private Sub ButtonName_Click()
If IsNull(Me.TextBoxName) Then
MsgBox "You can not have a Null", vbOKOnly, "There is an error"
Else
DoCmd.OpenReport "Report Name", acViewNormal, "", "", acNormal
End If
End Sub



Change ButtonName, ReportName,TexdtBoxName to what they are in your
application/form
 
R

Raymond

Wayne..the thing is that am having over 20 text-boxes so I can't give the
conditions for all of them to be null. Can't we do something like this:-

If(Print_Button_click) is used before than (Detail_Button_Click)
msgbox "please fill details first"

not textboxes in condition, rather than using functions as a whole..
 
W

Wayne-I-M

It is simple to add a button to do most things. I don't know what your
"detail" button so I can't say. Can you give some details.

The easy thing would be to hide (visible = No) the print button until your
Detail button is clicked.

Can you post details of what the Detail Button does
 
R

Raymond

The detail button actually opens another form which has several other
controls..
 
W

Wayne-I-M

You could use the form's before update with docmd.echo (if the intention is
to hold writing to the table) or just set the Print button to Visible = No
and then put this on the details button

Private Sub DetailButton_Click()
If IsNull(Me.textbox1) Then
MsgBox "Text Box 1 must be filled in", vbOKOnly, "Error"
ElseIf IsNull(Me.textbox2) Then
MsgBox "Text Box 2 must be filled in", vbOKOnly, "Error"
'add more ifs here if you need'
Else
Me.PrintButton.Visible = True
End If
End Sub


Don't forget that if (as I think your are) refering to another form you will
need to refer to that form in the Detail button click
 
B

BruceM

Here is something Albert Kallal wrote about data validation. His wording is
between the lines of asterisks. I can't recall if it was in a message to me
or if it was in a thread I was monitoring, I don't think it was posted on a
web site. The control names and error messages used are of course for
example only. You would use your own control names and error messages.

*********************
I use the following "general" code routine to give custom messages for
fields that are not filled out.

The code below is a great way to verify fields that you want to be requited.

Another nice feature is that after the given message, the cursor (focus)
moves to the field in question.

The code is used as follows:

in the forms before update event..you go:

Cancel = MyVerify.

And, then the two following routines need be put into the forms module. You
can see how in the first example, you just put in the list of controls that
you want requited, and also the text "error" message to display. Note
carefully how the full string is enclosed in quotes.

This routine is called in the forms Load event:

Private Function MyVerify() As Boolean

Dim colFields As New Collection

MyVerify = False

colFields.Add "TourDate,Tour date"
colFields.Add "Description,Description"
colFields.Add "City,City"
colFields.Add "cboProvince,Province"
colFields.Add "StartDate,Start date"
colFields.Add "EndDate,end date"

MyVerify = vfields(colFields)


End Function

Private Function vfields(colFields As Collection) As Boolean

Dim strErrorText As String
Dim strControl As String
Dim i As Integer

vfields = False

For i = 1 To colFields.Count
strControl = Split(colFields(i), ",")(0)
strErrorText = Split(colFields(i), ",")(1)
If IsNull(Me(strControl)) = True Then

MsgBox strErrorText & " is required", vbExclamation, AppName
Me(strControl).SetFocus
vfields = True
Exit Function
End If
Next i


End Function

*************************

In what I copied originally, this line:
MsgBox strErrorText & " is required", vbExclamation
was:
MsgBox strErrorText & " is required", vbExclamation, AppName

AppName seems to be a user-defined function that Albert had used, and it was
in the code when he copied it for the posting, but as I recall it generated
an error message, so I removed it.

Anyhow, I'm putting this out there as another way of validating. I think it
is convenient when there are more than a few controls to check.
 
J

John Spencer

A couple of ways to handle this.

One is to add an error handler to your print button code. That should keep
the error form dropping into debug mode.

Private Sub btnPrint_Click()

On Error GoTo ProcError

'Your Current Code here

Exit Sub

ProcError:
MsgBox "Please click Detail button first",,"Error " & err.Number
End Sub

Alternative is to check and see if the detail button form is open.

Private Sub btnPrint_Click()

On Error GoTo ProcError
'Assuming you are using Access 2000 or later:
If CurrentProject.AllForms("FormOpenedByDetailButton").IsLoaded Then

'Your Current Code here
else
Msgbox "Please click Detail Button before clicking Print button"
End if
Exit Sub

ProcError:
MsgBox "Please click Detail button first",,"Error " & err.Number
End Sub
If CurrentProject.AllForms("FormOpenedByDetailButton").IsLoaded Then

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
J

John W. Vinson/MVP

Hi all,

I am having 2 functions for 2 buttons, 1 is DETAIL,1 is PRINT. Now, if
someone clicks PRINT button 1st, it gives an error message, "Null value
cannot be used as object", which is correct because some fields cannot be
left NULL in my form.

What I want is to have a code which gives a pop-up message that DETAIL
button is to be clicked first before PRINT button.

How can we do this?

I'd just suggest that you have the PRINT button's Enabled property set
to No, so it will be greyed out and inactive. Set the Enabled property
to Yes in the click event of the DETAIL button - or in whatever event
is appropriate, such as the form's beforeupdate event.

You can also, of course, trap the error in the Print button's code and
simply exit the subroutine with a warning message, rather than letting
the default error handler come up.
 
Top