A problem opening a form so it doesn't add a new record

J

John Doe

Here is some code pasted below that seems to be giving me real
problems.

Here's the situation:

I have two tables. Table 1 is the main table and Table 2 is on the
MANY side of a one-to-many relationship.

On the form in question, I have a listbox that is populated by a query
that gets only the records from Table 1 that have a record in Table 2
with a specific status field value. Table 2 may have many records
associated with the record in Table 1, but there will be ONLY one of
those records that has this status, and that's the one to select. So
far it seems that works OK.

The idea is to select one of the items in the listbox and then there
are controls on the bottom half of the form that enable you to edit
the information. That edited information will go into Table 2.

The form is based on a query and it also seems to work OK. That query
uses the ID of the item selected in the listbox to get the appropriate
record information from Table 1 and Table 2.

My problem is that new records get added to Table 2 when I don't want
that to happen. I want to only edit the record in Table 2 that
corresponds to the selected item in the Listbox.

The form is opened from a switchboard that is set to open the form in
Edit Mode.

If I set up the form so it allows only edit (Allow Additions = NO)
then I get the following when I try to open the form:

Run Time error '2105'
You can't go to the specified record.
[End] [Debug]

If I click on [Debug], then I am placed in the Form_Load procedure at
the line:
Me!cmdExitForm.SetFocus

That seems weird since that's trying to set focus on a button and is
not trying to go to a record.

If I add DoCmd code in the Form_Current procedure to go to the first
record (it's commented out in the code below), then I get the same
error but this time it places me at the DoCmd line.

If I set the form so that Allow Additions = YES, then all this works
OK, but then the form opens with a new record and when an item is
selected in the listbox, I can edit the information but when I save
the record it gets added to Table 2 as a new record and the record I
WANTED to edit is left unchanged.

HELP!
All I want to do is edit the information in Table 2 when I select an
item in the listbox.

Any ideas where I should look to fix this?

Here is the code:
'===============================================
Private Sub Form_Current()

DoCmd.Close acForm, frmSwitchboard

'DoCmd.GoToRecord , , acFirst

End Sub 'Form_Current
'===============================================

Private Sub Form_Load()

Me!cmdExitForm.Enabled = True 'Always active

'Set focus to close button so undo button can be disabled
Me!cmdExitForm.SetFocus

Me!cmdUndo.Enabled = False
Me!cmdSave.Enabled = False
Me!cmdSaveGoToNextForm.Enabled = False

End Sub 'Form_Load
'===============================================
 
K

Ken Snell [MVP]

Be sure that the form's Data Entry property is set to No.


--

Ken Snell
<MS ACCESS MVP>


John Doe said:
Here is some code pasted below that seems to be giving me real
problems.

Here's the situation:

I have two tables. Table 1 is the main table and Table 2 is on the
MANY side of a one-to-many relationship.

On the form in question, I have a listbox that is populated by a query
that gets only the records from Table 1 that have a record in Table 2
with a specific status field value. Table 2 may have many records
associated with the record in Table 1, but there will be ONLY one of
those records that has this status, and that's the one to select. So
far it seems that works OK.

The idea is to select one of the items in the listbox and then there
are controls on the bottom half of the form that enable you to edit
the information. That edited information will go into Table 2.

The form is based on a query and it also seems to work OK. That query
uses the ID of the item selected in the listbox to get the appropriate
record information from Table 1 and Table 2.

My problem is that new records get added to Table 2 when I don't want
that to happen. I want to only edit the record in Table 2 that
corresponds to the selected item in the Listbox.

The form is opened from a switchboard that is set to open the form in
Edit Mode.

If I set up the form so it allows only edit (Allow Additions = NO)
then I get the following when I try to open the form:

Run Time error '2105'
You can't go to the specified record.
[End] [Debug]

If I click on [Debug], then I am placed in the Form_Load procedure at
the line:
Me!cmdExitForm.SetFocus

That seems weird since that's trying to set focus on a button and is
not trying to go to a record.

If I add DoCmd code in the Form_Current procedure to go to the first
record (it's commented out in the code below), then I get the same
error but this time it places me at the DoCmd line.

If I set the form so that Allow Additions = YES, then all this works
OK, but then the form opens with a new record and when an item is
selected in the listbox, I can edit the information but when I save
the record it gets added to Table 2 as a new record and the record I
WANTED to edit is left unchanged.

HELP!
All I want to do is edit the information in Table 2 when I select an
item in the listbox.

Any ideas where I should look to fix this?

Here is the code:
'===============================================
Private Sub Form_Current()

DoCmd.Close acForm, frmSwitchboard

'DoCmd.GoToRecord , , acFirst

End Sub 'Form_Current
'===============================================

Private Sub Form_Load()

Me!cmdExitForm.Enabled = True 'Always active

'Set focus to close button so undo button can be disabled
Me!cmdExitForm.SetFocus

Me!cmdUndo.Enabled = False
Me!cmdSave.Enabled = False
Me!cmdSaveGoToNextForm.Enabled = False

End Sub 'Form_Load
'===============================================
 

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