Chris Bellingha
New member
- Local time
- Today, 05:25
- Joined
- Apr 5, 2007
- Messages
- 7
I have quite a complex database that seems to work fine apart from one small snag. I have a table which holds the following information:
REP2: Postcode, Rep Name, Region
and a table (for customers) which has a field in which to enter the postcode. I wish the database to automatically assign a rep and region based on the postcode I enter for a customer. The postcodes I hold in REP2 are simply the first 4 characters of a british postcode (before the space).
So what I want to happen is for the database to take the postcode from the customers table, trim it down to the first 4 characters, compare this to REP2, then fill in the two fields on the customers form for rep name and region.
Currently I can get the fields automatically updating but unfortunately they all update with information from the first record of REP2.
This is my code:
Private Sub Postcode_AfterUpdate()
Dim varsalesregion1, varsalesrep1 As Variant
varsalesregion1 = DLookup("[region]", "REP2", "left([postcode],4)=[REP2]![postcode]")
varsalesrep1 = DLookup("rep name", "REP2", left([postcode],4)=[REP2]![postcode]")
Sales_Region1.Value = varsalesregion1
Sales_Rep1.Value = varsalesrep1
End Sub
REP2: Postcode, Rep Name, Region
and a table (for customers) which has a field in which to enter the postcode. I wish the database to automatically assign a rep and region based on the postcode I enter for a customer. The postcodes I hold in REP2 are simply the first 4 characters of a british postcode (before the space).
So what I want to happen is for the database to take the postcode from the customers table, trim it down to the first 4 characters, compare this to REP2, then fill in the two fields on the customers form for rep name and region.
Currently I can get the fields automatically updating but unfortunately they all update with information from the first record of REP2.
This is my code:
Private Sub Postcode_AfterUpdate()
Dim varsalesregion1, varsalesrep1 As Variant
varsalesregion1 = DLookup("[region]", "REP2", "left([postcode],4)=[REP2]![postcode]")
varsalesrep1 = DLookup("rep name", "REP2", left([postcode],4)=[REP2]![postcode]")
Sales_Region1.Value = varsalesregion1
Sales_Rep1.Value = varsalesrep1
End Sub