ApplyFilter method of DoCmd object in OnOpen event property questi

G

Guest

i want to enable my user to open a form subject to the condition the value of
the "Patient Number" on it is equal to a value selected by the user on the
'Switchboard' form called 'Command and Control Center' via an unbound combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
K

Ken Snell [MVP]

Concatenate the value into the string:

DoCmd.ApplyFilter "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]

If Patient Number is a text field, then this:

DoCmd.ApplyFilter "Me.[Patient Number] = '" & [Forms]![Command and Control
Center]![SelectPatient] & "'"
 
R

Rob Oldfield

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'
 
G

Guest

hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient Number
i chose on my switchboard. i wonder what it could be now?

-ted


Rob Oldfield said:
The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



Ted said:
i want to enable my user to open a form subject to the condition the value of
the "Patient Number" on it is equal to a value selected by the user on the
'Switchboard' form called 'Command and Control Center' via an unbound combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
K

Ken Snell [MVP]

Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


Ted said:
hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


Rob Oldfield said:
The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



Ted said:
i want to enable my user to open a form subject to the condition the
value of
the "Patient Number" on it is equal to a value selected by the user on
the
'Switchboard' form called 'Command and Control Center' via an unbound combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
G

Guest

hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


Ken Snell said:
Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


Ted said:
hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


Rob Oldfield said:
The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user on
the
'Switchboard' form called 'Command and Control Center' via an unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
K

Ken Snell [MVP]

sorry... you don't use Me in the string.

DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]

--

Ken Snell
<MS ACCESS MVP>

Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


Ken Snell said:
Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter
to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


Ted said:
hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k
help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and
Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and
Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user
on
the
'Switchboard' form called 'Command and Control Center' via an
unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and
Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
G

Guest

ken,

now it's giving me an error (red circle w/ X) message "Error Executing
Coomand" or some such verbiage?!

here's the infamous code....

Private Sub Form_Load()
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

-ted



Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


Ken Snell said:
Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


Ted said:
hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user on
the
'Switchboard' form called 'Command and Control Center' via an unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
J

Jeff Conrad

Humm, I just noticed Ted that you have several other people
besides myself helping on this.

You might want to adapt Ken's code by placing this in the
Open event to cancel the form from opening. Just an idea.

'*************Code Start*************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo ErrorPoint

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
Cancel = True
Else
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End If

ExitPoint:
Exit Sub

ErrorPoint:
' Unexpected Error
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " & Err.Description _
, vbExclamation, "Unexpected Error"
Resume ExitPoint

End Sub
'*************Code End*************

--
Jeff Conrad
Access Junkie
Bend, Oregon

Ken Snell said:
sorry... you don't use Me in the string.

DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]

--

Ken Snell
<MS ACCESS MVP>

Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


Ken Snell said:
Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter
to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k
help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and
Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and
Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user
on
the
'Switchboard' form called 'Command and Control Center' via an
unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and
Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
R

Rob Oldfield

Try changing your code to...

Private Sub Form_Load()
dim crit as string
crit="[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
msgbox crit
DoCmd.ApplyFilter , crit
End Sub

What does that give you?

I'm also with Ken on modifying your openform action on Command and Control
Center to pass the where string there.


Ted said:
ken,

now it's giving me an error (red circle w/ X) message "Error Executing
Coomand" or some such verbiage?!

here's the infamous code....

Private Sub Form_Load()
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

-ted



Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


Ken Snell said:
Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user on
the
'Switchboard' form called 'Command and Control Center' via an unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
G

Guest

yes, jeff, you noticed correctly -- happening as it has through waiting for
someone to pick up a thread and then deciding that perhaps it ought've been
cast in a different light when none did.

my brain's begun to morph into spaghetti just a bit with all this banter
back and forth and i'm wondering if all this is really worth it.

thanks for all the bandwidth in any event....

-ted


Jeff Conrad said:
Humm, I just noticed Ted that you have several other people
besides myself helping on this.

You might want to adapt Ken's code by placing this in the
Open event to cancel the form from opening. Just an idea.

'*************Code Start*************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo ErrorPoint

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
Cancel = True
Else
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End If

ExitPoint:
Exit Sub

ErrorPoint:
' Unexpected Error
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " & Err.Description _
, vbExclamation, "Unexpected Error"
Resume ExitPoint

End Sub
'*************Code End*************

--
Jeff Conrad
Access Junkie
Bend, Oregon

Ken Snell said:
sorry... you don't use Me in the string.

DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]

--

Ken Snell
<MS ACCESS MVP>

Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


:

Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter
to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k
help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and
Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and
Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user
on
the
'Switchboard' form called 'Command and Control Center' via an
unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and
Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
G

Guest

i'm a little bit above vba newbie rob, so i tried pasting jeff conrad's code
into the on open event and things became a little confusing 4me 'cause i had
some other code in there. i decided i'd paste it into your onload event
property and things seem to be shaping up. 'pon selecting a patient number
from the c&c center, i click on the switchboard's menu bar (which uses the
openform command) to take the user to the 'diagnostic' tests form which opens
to the selected patient number. the code's below:

Private Sub Form_Load()
On Error GoTo ErrorPoint

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
Cancel = True
Else
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and
Control Center]![SelectPatient]
End If

ExitPoint:
Exit Sub

ErrorPoint:
' Unexpected Error
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " & Err.description _
, vbExclamation, "Unexpected Error"
Resume ExitPoint

End Sub

the only problemo i find is that when i (deliberately) did not enter a
choice into the combobox on the switchboard, following the "Please select a
Patient Number from the list provided before contiuing" message which offers
the user the "Ok" button to click, we get the "Unexpected Error" message #438
"Object doesn't support this property or method" immediately upon clicking
the "Ok". it wastes no time getting to this point and did not let the user
(in this case yours truly) return to the s'board and make my selection?

looking lots better though....


-ted




Rob Oldfield said:
Try changing your code to...

Private Sub Form_Load()
dim crit as string
crit="[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
msgbox crit
DoCmd.ApplyFilter , crit
End Sub

What does that give you?

I'm also with Ken on modifying your openform action on Command and Control
Center to pass the where string there.


Ted said:
ken,

now it's giving me an error (red circle w/ X) message "Error Executing
Coomand" or some such verbiage?!

here's the infamous code....

Private Sub Form_Load()
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

-ted



Ted said:
hey, ken!

when i use the following

Private Sub Form_Load()
DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and Control
Center]![SelectPatient]
End Sub

even before the form's completely opened, i get an Enter Parameter Value
parameter query asking for the value of Me.Patient Number

whatamidoingwrongthistime?

-ted


:

Use the Load event of the form, not the Open event.

Alternatively, you could modify the OpenForm action step in the
switchboard's code to use the WhereCondition argument to pass the filter to
the form being opened.

--

Ken Snell
<MS ACCESS MVP>


hi guys,

"Patient Number" is an 8 digit LongInteger type field.

i have modified my string to look like this

DoCmd.ApplyFilter , "Me.[Patient Number] = " & [Forms]![Command and
Control
Center]![SelectPatient]

note i placed a ',' after the ApplyFilter per an example in the a2k help
documentation.

here's the reason i'm writing though: it doesn't open to the Patient
Number
i chose on my switchboard. i wonder what it could be now?

-ted


:

The filter needs to be a string that will look something like [Patient
Number]=13

The fact that you're including the reference to the form control is
stopping
that string being built. Try...

DoCmd.ApplyFilter "Me.[Patient Number] = "&[Forms]![Command and Control
Center]![SelectPatient]

or

DoCmd.ApplyFilter "Me.[Patient Number] = '"&[Forms]![Command and Control
Center]![SelectPatient]&"'"

...if Patient Number is a text field where it would need to end up as
[Patient Number]='13'



i want to enable my user to open a form subject to the condition the
value
of
the "Patient Number" on it is equal to a value selected by the user on
the
'Switchboard' form called 'Command and Control Center' via an unbound
combox.
so i use this string in the OnOpen property

DoCmd.ApplyFilter "Me.[Patient Number] = [Forms]![Command and Control
Center]![SelectPatient]"

can somebody venture a guess as to why it doesn't do the job.

-ted
 
J

Jeff Conrad

Hi Ted,

The error occurs because of this line:

Cancel = True

The code I gave you was for the Open event of the Form.
The Open event has a Cancel argument that allows you to
stop the form from opening, but the Load event does not
have such an argument. Hence the error.

My code will work if you use the Open event instead as I
posted before. You mentioned you had some difficulty with
integrating this into your form because you already had
some code in the Open event. If you post ALL the code you
currently have in that form's Open event here, I will adjust
the code for you and then you can just Copy/Paste it
into your Open event.
 
R

Rob Oldfield

Ted said:
i'm a little bit above vba newbie rob,

Fair enough. Just checking. Looks like Jeff has a good fix on what you're
trying to do so I'll leave you in his capable hands.
 
G

Guest

hey jeff!

mighty nice offer :)

here's the OnOpen code i'm alluding to....

Private Sub Form_Open(Cancel As Integer)
LAS_EnableSecurity Me
DoCmd.Maximize
On Error GoTo Error_Handler:
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
Me.Protocol_Title.DefaultValue = """" & DLookup("ProtocolTitle",
"tblDefaults") & """"
Dim MyVar As Integer
If DLookup("FinalAnswer", "tblDefaults") = False Then
MyVar = MsgBox("The correct Protocol ID and Title have not been entered into
the database." & vbNewLine _
& "Notify the Administrator. At this time you can not enter data into the
database.", 64, "Warning -- Read Before Proceeding!")
Cancel = True
End If
Exit Sub
Error_Handler:
If Err.Number = 2467 Then
Exit Sub
Else
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.description & "'" & _
" Please contact your System Administrator."
End If
End Sub

-ted



Jeff Conrad said:
Hi Ted,

The error occurs because of this line:

Cancel = True

The code I gave you was for the Open event of the Form.
The Open event has a Cancel argument that allows you to
stop the form from opening, but the Load event does not
have such an argument. Hence the error.

My code will work if you use the Open event instead as I
posted before. You mentioned you had some difficulty with
integrating this into your form because you already had
some code in the Open event. If you post ALL the code you
currently have in that form's Open event here, I will adjust
the code for you and then you can just Copy/Paste it
into your Open event.

--
Jeff Conrad
Access Junkie
Bend, Oregon

Ted said:
i'm a little bit above vba newbie rob, so i tried pasting jeff conrad's code
into the on open event and things became a little confusing 4me 'cause i had
some other code in there. i decided i'd paste it into your onload event
property and things seem to be shaping up. 'pon selecting a patient number
from the c&c center, i click on the switchboard's menu bar (which uses the
openform command) to take the user to the 'diagnostic' tests form which opens
to the selected patient number. the code's below:

Private Sub Form_Load()
On Error GoTo ErrorPoint

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
Cancel = True
Else
DoCmd.ApplyFilter , "[Patient Number] = " & [Forms]![Command and
Control Center]![SelectPatient]
End If

ExitPoint:
Exit Sub

ErrorPoint:
' Unexpected Error
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " & Err.description _
, vbExclamation, "Unexpected Error"
Resume ExitPoint

End Sub

the only problemo i find is that when i (deliberately) did not enter a
choice into the combobox on the switchboard, following the "Please select a
Patient Number from the list provided before contiuing" message which offers
the user the "Ok" button to click, we get the "Unexpected Error" message #438
"Object doesn't support this property or method" immediately upon clicking
the "Ok". it wastes no time getting to this point and did not let the user
(in this case yours truly) return to the s'board and make my selection?

looking lots better though....
 
J

Jeff Conrad

Hi Ted,

I obviously cannot completley test everything in this code, but
I believe this should all work. Copy/Paste into your Form's Open
event. Do this on a backup copy first!

'************Code Start************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
' Stop the form from opening
Cancel = True
Else
' Patient ID was selected on main form
' Run Security Code
LAS_EnableSecurity Me
' Maximize the form
DoCmd.Maximize
' Apply a filter to match chosen Patient Number
DoCmd.ApplyFilter , "[Patient Number] = " & _
[Forms]![Command and Control Center]![SelectPatient]
' Fill in Protocol ID value
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
' Fill in Protocol Title
Me.Protocol_Title.DefaultValue = """" & _
DLookup("ProtocolTitle", "tblDefaults") & """"
' Check to see if Final Answer has been entered
If DLookup("FinalAnswer", "tblDefaults") = False Then
' Missing information
MsgBox "The correct Protocol ID and Title have not " _
& "been entered into the database." & vbNewLine _
& "Notify the Administrator. At this time you can " _
& "not enter data into the database.", 64 _
, "Warning -- Read Before Proceeding!"
' Stop the form from opening
Cancel = True
Else
' All clear, continue with form opening
End If
End If

ExitPoint:
Exit Sub

Error_Handler:
If Err.Number = 2467 Then
' Ignore
Else
' Unexpected Error
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.Description & "'" & _
" Please contact your System Administrator."
End If
Resume ExitPoint

End Sub
'************Code End************
 
G

Guest

roger that, jeff!

will get back with (hopefully positive) outcome news to ya tomorrow.

-ted
 
G

Guest

hey jeff,

i pasted the batch into the OnEvent property and tried it out.....it does
seem to do what we wanted, i am happy to report :)

i would really like to be on the receiving end of a 'fishing pole' vs. just
a 'fish' here, so to speak, so i'd like to try to understand what the guding
principle to getting it to work in the OnOpen event (and with the
pre-existing code) is. it'll make a better vba-er out of me, if you have the
time to go into it.

in either event, i gotta thank you guys for all your great help and
enthusiasm.
one day i hope to be in a position to do the same.

with best regards,

-ted
 
J

Jeff Conrad

hey jeff,

Hi Ted, comments inline....
i pasted the batch into the OnEvent property and tried it out.....it does
seem to do what we wanted, i am happy to report :)

I assume you meant to say the Open event of the form??
Glad to hear the revised code is working.
i would really like to be on the receiving end of a 'fishing pole' vs. just
a 'fish' here, so to speak, so i'd like to try to understand what the guding
principle to getting it to work in the OnOpen event (and with the
pre-existing code) is. it'll make a better vba-er out of me, if you have the
time to go into it.

Ahhh the old, "teach a man to fish kinda thing, huh?"

Ok, Ted, put on yer waders, and grab yer gear, cause we're goin' fishin!

Private Sub Form_Open(Cancel As Integer)

Notice that the Open event in this line of code has a Cancel argument.
This means if we ever want to cancel the form from opening for some
reason we should use the Open event.
On Error GoTo Error_Handler

We use this line to tell Access to go to the Error_Handler part of the
code if we encounter any strange errors. As a good practice you should
ALWAYS have error handling in EVERY code procedure. Period.
If IsNull(Forms![Command and Control Center]!SelectPatient) Then

This line uses the built-in Access function called IsNull to test to see
if anything was entered was selected from the combo box on our main
form. Since we are testing this on a different form from the one running
this code, we must use the syntax of:
Forms!NameOfFormHere!ControlNameHere
If, however, this control was on this form we could have just used:
Me.ControlNameHere

As good programming practice I always believe you should avoid
spaces in names of objects, fields, controls, etc.
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"

This displays a message box to the user. Always present
meaningful message boxes to your users if they do something
wrong. The _ character denotes a line continuation in VBA.
You can put all that code on one line, but for newsgroup posts
it is usually easier to make smaller blocks of code for copy/paste.
' Stop the form from opening
Cancel = True

Here we tell Access to prevent this form from opening since the
sure did not select a Patient ID from the main form combo box.

This is the second "half "of our If-Else-End If statement. Think of
this part as the False area of the test.

The basic construct of the If-Else-End If block is this in its most
simplest terms.

If Something yadda, yadda Then
' Do something here
Else
' It didn't happen so do something else here
End If

When writing a If-Else-End If block it is a good idea to immediately
put in all the parts (If, Then, Else, End If) so you do not forget them
later on. This is quite easy to forget when making very long blocks
of code with multiple If-Else-End If tests.
' Patient ID was selected on main form
' Run Security Code
LAS_EnableSecurity Me

This code is obviously something custom made in your application, most
likely in a module. I have no idea what it does so I cannot comment it.
' Maximize the form
DoCmd.Maximize

This code maximizes the form to fill the entire screen area.
' Apply a filter to match chosen Patient Number
DoCmd.ApplyFilter , "[Patient Number] = " & _
[Forms]![Command and Control Center]![SelectPatient]

Here we Filter the current form's record source to limit the display.
We only show the record(s) that match the criteria selected from
the combo box. Since there are spaces in your field and object
names, we have to enclose them in brackets []. Otherwise, Access
will get confused and cough up a hairball on your keyboard. Not
a pretty sight let me tell you!
' Fill in Protocol ID value
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
' Fill in Protocol Title
Me.Protocol_Title.DefaultValue = """" & _
DLookup("ProtocolTitle", "tblDefaults") & """"

Here you appear to be filling in some text box values on the form with
existing information in a table called tblDefaults.
' Check to see if Final Answer has been entered
If DLookup("FinalAnswer", "tblDefaults") = False Then
' Missing information
MsgBox "The correct Protocol ID and Title have not " _
& "been entered into the database." & vbNewLine _
& "Notify the Administrator. At this time you can " _
& "not enter data into the database.", 64 _
, "Warning -- Read Before Proceeding!"

Here you appear to be making one last test before making a final
commitment to open the form. If your test proves true (meaning
FinalAnswer is False in the table) you display a message to the user
and stop the form from opening. Again, this is all wrapped up
inside another If-Else-End If block.
' Stop the form from opening
Cancel = True

You prevent the form from opening here.
Else
' All clear, continue with form opening

When this part of the code is reached it means we have passed
all of our tests and we can now proceed with opening the form.

This End If is the closing block for the second test.

This End If is the closing block for the first test.
ExitPoint:
Exit Sub

Since we should always be having proper error handling in our
code, we naturally have to have a proper exit area for the code.
As a good rule, we should only have one spot to enter code, and
one spot to exit code. The exit code is also the place we should
perform any cleanup of this procedure. If we have opened and/or
used objects we need to close them and release their memory.

"If you open something: close it. If you take something out: put it back
when finished." Your mother was right all along.
Error_Handler:
If Err.Number = 2467 Then
' Ignore

Here you appear to be possibly expecting Error number 2467 at
some point. This error will be raised if Access cannot find an object
you are referring to in some code somewhere. I'm not sure why
you think you may be getting this error, but I can only guess it has
something to do with the custom security routine. At any rate, you
are telling Access to ignore this error.
Else
' Unexpected Error
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.Description & "'" & _
" Please contact your System Administrator."
End If

Here we tell Access to display a nice message box to the user if
we encounter something we had not planned. This type of code
should be in most of your error handling routines. There are times
when we have On Error Resume Next procedures so this would
not be needed, but in most cases we want to know what went
wrong with our code. This is obviously something we did not plan
for so we want to display a custom message that would be easier
for users to report rather than a scary looking Access message.
Resume ExitPoint

At the end of every error handling routine we want to tell Access
to exit out of our code through the one and *only* one exit door.
This way the code will jump up to the exit area, do any cleanup
stuff, and gracefully exit out of the procedure. No "leaving the seat up"
or other bad stuff will result.

Finally, our ending statement for the code.

In summary, to adapt this code for other data entry forms in your
application you simply need to follow the same logic. If you want
the user to make a selection in the combo box just follow this
generic coding:

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
' Stop the form from opening
Cancel = True
Else
' Whatever existing code you already have present in the Open event.
End If

As a side note, since you had two If tests in this particular form, you
could also have coded it like so:

'************Code Start************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
' Stop the form from opening
Cancel = True
Else
' Check to see if Final Answer has been entered
If DLookup("FinalAnswer", "tblDefaults") = False Then
' Missing information
MsgBox "The correct Protocol ID and Title have not " _
& "been entered into the database." & vbNewLine _
& "Notify the Administrator. At this time you can " _
& "not enter data into the database.", 64 _
, "Warning -- Read Before Proceeding!"
' Stop the form from opening
Cancel = True
Else
' All clear, continue with form opening
' Patient ID was selected on main form
' Run Security Code
LAS_EnableSecurity Me
' Maximize the form
DoCmd.Maximize
' Apply a filter to match chosen Patient Number
DoCmd.ApplyFilter , "[Patient Number] = " & _
[Forms]![Command and Control Center]![SelectPatient]
' Fill in Protocol ID value
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
' Fill in Protocol Title
Me.Protocol_Title.DefaultValue = """" & _
DLookup("ProtocolTitle", "tblDefaults") & """"
End If
End If

ExitPoint:
Exit Sub

Error_Handler:
If Err.Number = 2467 Then
' Ignore
Else
' Unexpected Error
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.Description & "'" & _
" Please contact your System Administrator."
End If
Resume ExitPoint

End Sub
'************Code End************

Just to show an alternative.
in either event, i gotta thank you guys for all your great help and
enthusiasm.
one day i hope to be in a position to do the same.

Study these links and you should be there in no time:

http://www.ltcomputerdesigns.com/JCReferences.html

You're very welcome, class dismissed.
 
G

Guest

jeff,

i would like to enhance the user's ability to use this to include being able
to open to a record in the underlying table which matches a joint criterion

' Apply a filter to match chosen Patient Number
[Forms]![Command and Control Center]![SelectPatient] And [Cycle_] = 0

[note the interest in using just those records having a "Cycle_" value of
zero.]
it's giving us a error 13 "Type Mismatch" though.??

-ted


Jeff Conrad said:
hey jeff,

Hi Ted, comments inline....
i pasted the batch into the OnEvent property and tried it out.....it does
seem to do what we wanted, i am happy to report :)

I assume you meant to say the Open event of the form??
Glad to hear the revised code is working.
i would really like to be on the receiving end of a 'fishing pole' vs. just
a 'fish' here, so to speak, so i'd like to try to understand what the guding
principle to getting it to work in the OnOpen event (and with the
pre-existing code) is. it'll make a better vba-er out of me, if you have the
time to go into it.

Ahhh the old, "teach a man to fish kinda thing, huh?"

Ok, Ted, put on yer waders, and grab yer gear, cause we're goin' fishin!

Private Sub Form_Open(Cancel As Integer)

Notice that the Open event in this line of code has a Cancel argument.
This means if we ever want to cancel the form from opening for some
reason we should use the Open event.
On Error GoTo Error_Handler

We use this line to tell Access to go to the Error_Handler part of the
code if we encounter any strange errors. As a good practice you should
ALWAYS have error handling in EVERY code procedure. Period.
If IsNull(Forms![Command and Control Center]!SelectPatient) Then

This line uses the built-in Access function called IsNull to test to see
if anything was entered was selected from the combo box on our main
form. Since we are testing this on a different form from the one running
this code, we must use the syntax of:
Forms!NameOfFormHere!ControlNameHere
If, however, this control was on this form we could have just used:
Me.ControlNameHere

As good programming practice I always believe you should avoid
spaces in names of objects, fields, controls, etc.
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"

This displays a message box to the user. Always present
meaningful message boxes to your users if they do something
wrong. The _ character denotes a line continuation in VBA.
You can put all that code on one line, but for newsgroup posts
it is usually easier to make smaller blocks of code for copy/paste.
' Stop the form from opening
Cancel = True

Here we tell Access to prevent this form from opening since the
sure did not select a Patient ID from the main form combo box.

This is the second "half "of our If-Else-End If statement. Think of
this part as the False area of the test.

The basic construct of the If-Else-End If block is this in its most
simplest terms.

If Something yadda, yadda Then
' Do something here
Else
' It didn't happen so do something else here
End If

When writing a If-Else-End If block it is a good idea to immediately
put in all the parts (If, Then, Else, End If) so you do not forget them
later on. This is quite easy to forget when making very long blocks
of code with multiple If-Else-End If tests.
' Patient ID was selected on main form
' Run Security Code
LAS_EnableSecurity Me

This code is obviously something custom made in your application, most
likely in a module. I have no idea what it does so I cannot comment it.
' Maximize the form
DoCmd.Maximize

This code maximizes the form to fill the entire screen area.
' Apply a filter to match chosen Patient Number
DoCmd.ApplyFilter , "[Patient Number] = " & _
[Forms]![Command and Control Center]![SelectPatient]

Here we Filter the current form's record source to limit the display.
We only show the record(s) that match the criteria selected from
the combo box. Since there are spaces in your field and object
names, we have to enclose them in brackets []. Otherwise, Access
will get confused and cough up a hairball on your keyboard. Not
a pretty sight let me tell you!
' Fill in Protocol ID value
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
' Fill in Protocol Title
Me.Protocol_Title.DefaultValue = """" & _
DLookup("ProtocolTitle", "tblDefaults") & """"

Here you appear to be filling in some text box values on the form with
existing information in a table called tblDefaults.
' Check to see if Final Answer has been entered
If DLookup("FinalAnswer", "tblDefaults") = False Then
' Missing information
MsgBox "The correct Protocol ID and Title have not " _
& "been entered into the database." & vbNewLine _
& "Notify the Administrator. At this time you can " _
& "not enter data into the database.", 64 _
, "Warning -- Read Before Proceeding!"

Here you appear to be making one last test before making a final
commitment to open the form. If your test proves true (meaning
FinalAnswer is False in the table) you display a message to the user
and stop the form from opening. Again, this is all wrapped up
inside another If-Else-End If block.
' Stop the form from opening
Cancel = True

You prevent the form from opening here.
Else
' All clear, continue with form opening

When this part of the code is reached it means we have passed
all of our tests and we can now proceed with opening the form.

This End If is the closing block for the second test.

This End If is the closing block for the first test.
ExitPoint:
Exit Sub

Since we should always be having proper error handling in our
code, we naturally have to have a proper exit area for the code.
As a good rule, we should only have one spot to enter code, and
one spot to exit code. The exit code is also the place we should
perform any cleanup of this procedure. If we have opened and/or
used objects we need to close them and release their memory.

"If you open something: close it. If you take something out: put it back
when finished." Your mother was right all along.
Error_Handler:
If Err.Number = 2467 Then
' Ignore

Here you appear to be possibly expecting Error number 2467 at
some point. This error will be raised if Access cannot find an object
you are referring to in some code somewhere. I'm not sure why
you think you may be getting this error, but I can only guess it has
something to do with the custom security routine. At any rate, you
are telling Access to ignore this error.
Else
' Unexpected Error
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.Description & "'" & _
" Please contact your System Administrator."
End If

Here we tell Access to display a nice message box to the user if
we encounter something we had not planned. This type of code
should be in most of your error handling routines. There are times
when we have On Error Resume Next procedures so this would
not be needed, but in most cases we want to know what went
wrong with our code. This is obviously something we did not plan
for so we want to display a custom message that would be easier
for users to report rather than a scary looking Access message.
Resume ExitPoint

At the end of every error handling routine we want to tell Access
to exit out of our code through the one and *only* one exit door.
This way the code will jump up to the exit area, do any cleanup
stuff, and gracefully exit out of the procedure. No "leaving the seat up"
or other bad stuff will result.

Finally, our ending statement for the code.

In summary, to adapt this code for other data entry forms in your
application you simply need to follow the same logic. If you want
the user to make a selection in the combo box just follow this
generic coding:

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
' Stop the form from opening
Cancel = True
Else
' Whatever existing code you already have present in the Open event.
End If

As a side note, since you had two If tests in this particular form, you
could also have coded it like so:

'************Code Start************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler

If IsNull(Forms![Command and Control Center]!SelectPatient) Then
' No Patient ID entered on main form
MsgBox "Please select a Patient Number from " _
& "the list provided before continuing." _
, vbInformation, "Which Patient?"
' Stop the form from opening
Cancel = True
Else
' Check to see if Final Answer has been entered
If DLookup("FinalAnswer", "tblDefaults") = False Then
' Missing information
MsgBox "The correct Protocol ID and Title have not " _
& "been entered into the database." & vbNewLine _
& "Notify the Administrator. At this time you can " _
& "not enter data into the database.", 64 _
, "Warning -- Read Before Proceeding!"
' Stop the form from opening
Cancel = True
Else
' All clear, continue with form opening
' Patient ID was selected on main form
' Run Security Code
LAS_EnableSecurity Me
' Maximize the form
DoCmd.Maximize
' Apply a filter to match chosen Patient Number
DoCmd.ApplyFilter , "[Patient Number] = " & _
[Forms]![Command and Control Center]![SelectPatient]
' Fill in Protocol ID value
Me.Protocol_ID.DefaultValue = DLookup("ProtocolID", "tblDefaults")
' Fill in Protocol Title
Me.Protocol_Title.DefaultValue = """" & _
DLookup("ProtocolTitle", "tblDefaults") & """"
End If
End If

ExitPoint:
Exit Sub

Error_Handler:
If Err.Number = 2467 Then
' Ignore
Else
' Unexpected Error
MsgBox "An unanticipated error has occurred." & _
"The error number is " & Err.Number & _
" and the description is '" & Err.Description & "'" & _
" Please contact your System Administrator."
End If
Resume ExitPoint

End Sub
'************Code End************

Just to show an alternative.
in either event, i gotta thank you guys for all your great help and
enthusiasm.
one day i hope to be in a position to do the same.

Study these links and you should be there in no time:

http://www.ltcomputerdesigns.com/JCReferences.html

You're very welcome, class dismissed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top