Form creates new record! Why?


T

Tony Williams

I have created a simple find form. It has one control which is a combo box
based on a field (txtsurname) in my table (tblcontacts). I have a command
button that when the user has selected the surname of the record they want
to find opens up a form (frmcontacts). However when the form opens to show
the records it also adds a new record with the same name. Why is this
happening?
The code behind the OnClick event of the command button is

Private Sub Command2_Click()
On Error GoTo Command2_Click_Err
Dim stLinkCriteria As String
stLinkCriteria = "[txtsurname]=" & "'" & Me![txtSurname] & "'"

If IsNull(DLookup("[txtsurname]", "tblContacts", stLinkCriteria)) Then
MsgBox "There is no Contact with this Surname.", _
vbInformation, "No Matching Record"
Else
DoCmd.OpenForm "frmContacts", , , "[txtSurname]=" & "'" &
Me![txtSurname] & "'"
End If

Command2_Click_Exit:
Exit Sub

Command2_Click_Err:

Response = acDataErrContinue
Resume Command2_Click_Exit

End Sub

Thank you
Tony
 
Ad

Advertisements

B

Brian Bastl

Tony,

Your search form should be unbound, meaning that it has no record source.

The second option is to add an unbound combo to the Header in frmContacts
(using the Wizard) and select the option to "Find a record on my form based
on the value I selected in my combo box".

HTH,
Brian
 
Ad

Advertisements

T

Tony Williams

Thanks Brian found the problem it was bound to my field txtsurname!
Thanks
Tony
Brian Bastl said:
Tony,

Your search form should be unbound, meaning that it has no record source.

The second option is to add an unbound combo to the Header in frmContacts
(using the Wizard) and select the option to "Find a record on my form
based
on the value I selected in my combo box".

HTH,
Brian


Tony Williams said:
I have created a simple find form. It has one control which is a combo
box
based on a field (txtsurname) in my table (tblcontacts). I have a
command
button that when the user has selected the surname of the record they
want
to find opens up a form (frmcontacts). However when the form opens to
show
the records it also adds a new record with the same name. Why is this
happening?
The code behind the OnClick event of the command button is

Private Sub Command2_Click()
On Error GoTo Command2_Click_Err
Dim stLinkCriteria As String
stLinkCriteria = "[txtsurname]=" & "'" & Me![txtSurname] & "'"

If IsNull(DLookup("[txtsurname]", "tblContacts", stLinkCriteria))
Then
MsgBox "There is no Contact with this Surname.", _
vbInformation, "No Matching Record"
Else
DoCmd.OpenForm "frmContacts", , , "[txtSurname]=" & "'" &
Me![txtSurname] & "'"
End If

Command2_Click_Exit:
Exit Sub

Command2_Click_Err:

Response = acDataErrContinue
Resume Command2_Click_Exit

End Sub

Thank you
Tony
 

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