Solved If the customer's name exists, Get his record to add data to his subform which related to another table (1 Viewer)

Hisoka

New member
Local time
Today, 14:21
Joined
Sep 20, 2023
Messages
17
Hello,
I have these two tables and this form,
tables.png
form and relationship.png

I want that if I write the name of an already existing customer, That is, the program will bring his record to the main form (his ID & Number...etc) to be able to modify his order in subform without having to search for the customer through the Navigation Buttons..

In other words, I need if i write the name of a customer that does not exist, program must add him as a new customer,
and if the customer already exists, program must get his info to current form To be able to modify his subform..

I appreciate any help...
 

Attachments

  • Database43.accdb
    808 KB · Views: 62

Ranman256

Well-known member
Local time
Today, 09:21
Joined
Apr 9, 2015
Messages
4,337
this Find screen would count the recs having given name.
if 0 exist, open the detail in dataentry mode
if 1 exists, the only exising rec, open that rec in detail form
if > 1 exists, open a LIST view of all recs that match, then user can choose the correct 1 and open detail from there.


Code:
Private Sub btnFind_click()
Dim iCt As Integer
Dim sWhere As String
Dim vFirst, vLast, vID
Const kTBL = "tClients"

vFirst = txtFirst
vLast = txtLast

sWhere = "[FirstName]='" & vFirst & "' and [LastName]='" & vLast & "'"
iCt = DCount("*", kTBL, sWhere)

Select Case iCt
   Case 0   'add new client
     DoCmd.OpenForm "frmClientDtl", , , , acFormAdd
     Forms!frmClientDtl!txtFirstName = vFirst
     Forms!frmClientDtl!txtLastName = vLast
    
   Case 1   'open the detail frm on only client
     vID = DLookup("[ID]", kTBL, sWhere)
     DoCmd.OpenForm "frmClientDtl", , , "[ID]=" & vID
  
   Case Else  'open a LIST VIEW of all hits, then user can select the 1 needed from that to open detail view
      DoCmd.OpenForm "frmClientList", , , sWhere
End Select
End Sub
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:21
Joined
May 21, 2018
Messages
8,529
Demo
 

Attachments

  • Database43_Majp.accdb
    808 KB · Views: 71

Hisoka

New member
Local time
Today, 14:21
Joined
Sep 20, 2023
Messages
17
Thank you, @Ranman256, Unfortunately, I did not know how to modify the code to suit my request. I will try it again.

Thank you, @MajP, Very nice idea. Combobox solve the problem. Thanks for the help..
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:21
Joined
Feb 19, 2002
Messages
43,275
You also need to fix your relationship it is incorrect.
1. Naming all your primary keys "ID" is not cool or efficient and does nothing except foster confusion. Use CustomerID and OrderID so you can easily distinguish relationships without having to open a relationships window. If it makes you feel more comfortable, add _FK as a suffix when using the PK as a FK.
2. Remove the existing relationship.
3. Then you have to change Customers to add the field CustomerID_FK and remove the field named CustomerName and redraw the relationship.

It is quite possible that you are confused because you are using table level lookups.
 

Users who are viewing this thread

Top Bottom