How to avoid ms error message "You can't go to a specified record" (1 Viewer)

BobPaul01

New member
Local time
Today, 00:22
Joined
May 18, 2022
Messages
7
My form [Menu] has a combo box where ClientID is remembered for later use.

I then open a form: frmClientEdit

WHERE Condition= [ClientID]=[Forms]![Menu]![ClientID]

Works ok. Except if the user fails to choose a ClientID from the combo box before opening the form, then a Microsoft Visual Basic error message appears: “ Run-time error ‘2105’. You can’t go to the specified record”.

This is indeed true. But I want to avoid the error message by verifying that user has used the combo box and a code has been entered in [Forms]![Menu]![ClientID] prior to opening frmClientEdit.

How do I do that within the macro that opens frmClientEdit?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
18,972
With a macro? You could try adding an If/Then block to check if there's a value selected from the combobox.
 

Ranman256

Well-known member
Local time
Today, 01:22
Joined
Apr 9, 2015
Messages
4,102
docmd.setwarnings false
on error resume next
 

strive4peace

AWF VIP
Local time
Today, 00:22
Joined
Apr 3, 2020
Messages
857
hi @BobPaul01

when you open the form, use the WHERE condition and resolve the value so it doesn't reference the form you just came from.

DoCmd.OpenForm FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs

so, somthing like this in the code behind the Menu form:

DoCmd.OpenForm "frmClientEdit", , , Me.ClientID

This will filter the form for just that client. Alternately, if you want the form to show all the clients but start on that one in particular, you can pass the value in the open arguments and then use code on the Load event to position the record. Assuming that ClientID is a long integer, code would be something like this:

Rich (BB code):
Private Sub Form_Load()
  dim nClientID as long
   If Not IsNull(Me.OpenArgs) Then
      If IsNumeric(Me.OpenArgs) Then
         nClientID  = CLng(Me.OpenArgs)
         Me.Recordset.FindFirst  "ClientID =" & nClientID 
      End If
   End If
End Sub
 

strive4peace

AWF VIP
Local time
Today, 00:22
Joined
Apr 3, 2020
Messages
857
@BobPaul01, adding on ...

> verifying that user has used the combo box and a code has been entered

before using OpenForm, make sure your combo is filled.

Rich (BB code):
With Me.ClientID 
   If IsNull(.Value) Then 
      .SetFocus 
      MsgBox  "You must choose a client first",, "Can't open form"
      Exit Sub 
   Else 
      DoCmd.OpenForm  "frmClientEdit"
   End If 
End With
 

Users who are viewing this thread

Top Bottom