No Record Error

M

magmike

On a form, I have a button that opens another form only showing records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to click
the buttons for them and tell them they can only click that button when they
have a record pulled up. Is there an easier way to address this in the code
so that the users understands without having to call me?

Thanks!
 
R

Roger Carlson

If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
M

magmike

I assume it would consist of an IIf function that says if ID= < 1(upon form
load?) then (what?) to CommandButton -
Right?

Sorry that I am a hack.

Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
On a form, I have a button that opens another form only showing records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to click
the buttons for them and tell them they can only click that button when they
have a record pulled up. Is there an easier way to address this in the code
so that the users understands without having to call me?

Thanks!
 
M

magmike

Here is the error I receive, by the way:

---------------------------------------------------------------------------------
Syntax error (missing operator) in query expression '[RawLeads.CompanyID]='.
---------------------------------------------------------------------------------

Could we do an IIf [RawLeads.CompanyID] < 1 Then ______ in the LinkCriteria
section?




Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
On a form, I have a button that opens another form only showing records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to click
the buttons for them and tell them they can only click that button when they
have a record pulled up. Is there an easier way to address this in the code
so that the users understands without having to call me?

Thanks!
 
M

magmike

PS: Under which event would I attempt to disable the command button if no
record was selected?


Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
On a form, I have a button that opens another form only showing records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to click
the buttons for them and tell them they can only click that button when they
have a record pulled up. Is there an easier way to address this in the code
so that the users understands without having to call me?

Thanks!
 
R

Roger Carlson

I'd try the OnCurrent event of the Form AND the AfterUpdate event of the
textbox. Something like:

If IsNull(Me![RawLeads.CompanyID]) then
Me.ScheduleCB.Enabled = False
Else
Me.ScheduleCB.Enabled = True
End If

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
PS: Under which event would I attempt to disable the command button if no
record was selected?


Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



"magmike @netterweb.com>" <mike.kline<hates-spam-so-remove-this> wrote in
message news:[email protected]...
On a form, I have a button that opens another form only showing records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to click
the buttons for them and tell them they can only click that button when they
have a record pulled up. Is there an easier way to address this in the code
so that the users understands without having to call me?

Thanks!
 
M

magmike

Very nice. Thanks.

Roger Carlson said:
I'd try the OnCurrent event of the Form AND the AfterUpdate event of the
textbox. Something like:

If IsNull(Me![RawLeads.CompanyID]) then
Me.ScheduleCB.Enabled = False
Else
Me.ScheduleCB.Enabled = True
End If

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
PS: Under which event would I attempt to disable the command button if no
record was selected?


Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do
this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



"magmike @netterweb.com>" <mike.kline<hates-spam-so-remove-this> wrote in
message On a form, I have a button that opens another form only showing
records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to
click
the buttons for them and tell them they can only click that button
when
they
have a record pulled up. Is there an easier way to address this in the
code
so that the users understands without having to call me?

Thanks!
 
M

magmike

I started adding If statements for other controls on the form. For example,
I have a "Dial" button next to the Phone field. As expected, if there is no
data in the phone number field the button is disabled. However, if I add a
phone number to the Phone field and then navigate to the button (by hitting
tab), the button is not disabled. I have to refresh the form to activate the
button. The problem also works in the reverse. I can erase the data in the
field, navigate to the button, and it is still enabled until I refresh the
form. How can I rectify this?

Roger Carlson said:
I'd try the OnCurrent event of the Form AND the AfterUpdate event of the
textbox. Something like:

If IsNull(Me![RawLeads.CompanyID]) then
Me.ScheduleCB.Enabled = False
Else
Me.ScheduleCB.Enabled = True
End If

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
PS: Under which event would I attempt to disable the command button if no
record was selected?


Roger Carlson said:
If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do
this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



"magmike @netterweb.com>" <mike.kline<hates-spam-so-remove-this> wrote in
message On a form, I have a button that opens another form only showing
records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" & Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form, they of
course get errors which confuses them and requires a visit from me to
click
the buttons for them and tell them they can only click that button
when
they
have a record pulled up. Is there an easier way to address this in the
code
so that the users understands without having to call me?

Thanks!
 
M

magmike

Please disregard this note. I was able to solve the problem by using the
code below in the On Exit event of the fields mentioned below.

magmike @netterweb.com> said:
I started adding If statements for other controls on the form. For example,
I have a "Dial" button next to the Phone field. As expected, if there is no
data in the phone number field the button is disabled. However, if I add a
phone number to the Phone field and then navigate to the button (by hitting
tab), the button is not disabled. I have to refresh the form to activate
the button. The problem also works in the reverse. I can erase the data in
the field, navigate to the button, and it is still enabled until I refresh
the form. How can I rectify this?

Roger Carlson said:
I'd try the OnCurrent event of the Form AND the AfterUpdate event of the
textbox. Something like:

If IsNull(Me![RawLeads.CompanyID]) then
Me.ScheduleCB.Enabled = False
Else
Me.ScheduleCB.Enabled = True
End If

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



magmike @netterweb.com> said:
PS: Under which event would I attempt to disable the command button if
no
record was selected?


If you comment out the OnError line:
' On Error GoTo Err_ScheduleCB_Click
and run the code, it will return the actual error code. Then you can trap
that error in your code.

Err_ScheduleCB_Click:
If Err.Number = 3022 Then '(or whatever the code is)
'<handle the error here>
MsgBox "You MUST have a record selected"
Resume Exit_ScheduleCB_Click
Else
MsgBox Err.Description
Resume Exit_ScheduleCB_Click
End If
End Sub

OR

You can disable the button until a record is selected. (How you do
this
exactly, depends a lot on how your form works.)


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



"magmike @netterweb.com>" <mike.kline<hates-spam-so-remove-this> wrote in
message On a form, I have a button that opens another form only showing
records
relating to the record on the previous form. The sub is as follows:

Private Sub ScheduleCB_Click()
On Error GoTo Err_ScheduleCB_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "CB - F6 (New)"

stLinkCriteria = "[RawLeads.CompanyID]=" &
Me![RawLeads.CompanyID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ScheduleCB_Click:
Exit Sub

Err_ScheduleCB_Click:
MsgBox Err.Description
Resume Exit_ScheduleCB_Click

End Sub

However, if a user does not have a record open on the first form,
they of
course get errors which confuses them and requires a visit from me to
click
the buttons for them and tell them they can only click that button
when
they
have a record pulled up. Is there an easier way to address this in
the
code
so that the users understands without having to call me?

Thanks!
 
Top