• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

Form text boxes should update when the combo box on the form changes

I don't know why I can't get this to work the way it should.  It seems like it should be simple.

I have a form with a combo box that lists client names. There are two text boxes on the form that should show the client's city and state once the client has been selected in the combo box.

What I've been trying is

1.  In the txtClientCity Control Source:  =DLookUp("[CCity]","tblClients","[ClientID] = Form![ClientID]")
     In the txtClientState Control Source: =DLookUp("[StateCode]","qryClientStateCode","[ClientID] = Form![ClientID]")

The above produces the correct results but the information only shows in the text boxes AFTER the record is saved.  I'd like it to show as soon as the combo box selection is made.

I've also tried using the same code in the After Update property of the combo box and now I'm trying it in the On Change property.

2.  
Private Sub cboClientID_Change()

Me.txtClientCity.Value = DLookup("[CCity]", "tblClients", "[ClientID] = Form![ClientID]")

Me.txtClientState.Value = DLookup("[StateCode]", "qryClientStateCode", "[ClientID] = Form![ClientID]")

End Sub

Open in new window


The problem with the second attempt is that it modifies the City and State text boxes for ALL records instead of the current record being created.

In case it's pertinent:
The combo box has Control Source ClientID from the form's source tblJobOrders.  Combo Box Row Source: SELECT tblClients.ClientID, tblClients.CName FROM tblClients; Bound column: 2 and Column Widths:  0";2"

Can anyone either fix my code or suggest an alternate way of populating two text boxes based on the selection of a combo box on a form?  I'd like them to populate as soon as the selection is made in the combo box (instead of doing so after the new record is saved).

Thanks so much!
0
fabi2004
Asked:
fabi2004
  • 2
  • 2
  • 2
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Me.txtClientCity.Value = DLookup("[City]", "tblClients", "[ClientID] = " & Form![ClientID])

and

Me.txtClientState.Value = DLookup("[StateCode]", "qryClientStateCode", "[ClientID] = " & Form![ClientID])

<<The above produces the correct results but the information only shows in the text boxes AFTER the record is saved.  I'd like it to show as soon as the combo box selection is made.>>

 Execute the statements in the AfterUpdate event of the combo and leave the text boxes unbound.   You'll also need to execute the same two statements in the OnCurrent event.

  A better approach would be to modify the forms recordsource and add the customer table along with a join.   As soon as you have a valid custID in the record, the city/state would then display because of the join.

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
OnChange BTW fires for every keystroke in a control. This occurs before the BeforeUpdate and AfterUpdate events.   Typically you want AfterUpdate for something like this, when you want to react to a change value.

OnChange would only be used if you somehow wanted to validate a value as it was being entered character by character (ie. check the length).

Jim.
1
 
PatHartmanCommented:
Jim's "better approach" is the best way to do this, especially on a continuous form.  For a bound control, which this will become, the continuous form will show the value that is in each record.  For an unbound control, which it is now, Access can store only a single value at a time and that is why you see the same value on every row.

One warning though, when you use this technique, make sure to set the "lookup" controls' Locked property to Yes to avoid accidental updating.  A user may think if he updates the value here it affects only a single record and that is not the case at all.
1
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fabi2004Author Commented:
Big sigh of relief.  I knew I was doing something dumb, but I'd stared at it so long that I kept missing the obvious.

I added the Client and States table along with the Job Order table to the form's source and it works like a charm.  The way it's supposed to.  

Thank you so much!

BTW  If I "lock" the combo box then I can't select from it.  Is that what you were referring to?  I've set it up so that the Control Source is tblJobOrders.ClientID and the Row Source is SELECT tblClients.ClientID, tblClients.CName FROM tblClients;  with Bound Column 1.  This combo box also allows a user to add a new client if it's not on the list via a dialog and pop-up form, so I don't know is locking it will break that part of it.  Sorry, I can open another question for this.  I closed the previous one before I started typing.
0
 
PatHartmanCommented:
You don't lock the combo, you lock the city and state controls.  Those are the "looked up" values.
1
 
fabi2004Author Commented:
Ah!  That makes sense.  Thank you so much.  I've never done that before and I'm sure I've dodged a bullet not having the underlying tables changed by accident before.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now