Hi Ananth,
Instead of updating country in one table when you already have it stored
in another, why not just link to that information whenever you need it?
When there are null values (Suppliers that don't have a record in
[Supplier Country], you could do something like this in your query:
field --> SuppCountry: IIF(
Isnull([Supplier Country Primary Key field]),
“No country codeâ€, [country field])
If you have multiple countries for each supplier -- now that I look at
your structure, I suppose that is why they are seperated
-- and you
want to have a comma list, here is a function you can use to do that
(this code goes into a general module):
'~~~~~~~~~~~~~~~~
Function LoopAndCombine( _
pTablename As String, _
pIDFieldname As String, _
pTextFieldname As String, _
pValueID As Long, _
Optional pWhere As String, _
Optional pDeli As String, _
Optional pNoValue as string) As String
'crystal 8-3-06
'NEEDS REFERENCE
'Microsoft DAO Library
'Set up error handler
On Error GoTo Proc_Err
'dimension variables
Dim r As dao.Recordset, mAllValues As String, S As String
Dim mValueDeli As String
If Len(pValueDeli) > 0 Then _
mValueDeli = pDeli Else mValueDeli = ","
mAllValues = ""
S = "SELECT [" & pTextFieldname & "] " _
& " FROM [" & pTablename & "]" _
& " WHERE [" & pIDFieldname _
& "] = " & pValueID _
& IIf(Len(pWhere) > 0, " AND " & pWhere, "") _
& ";"
'open the recordset
Set r = CurrentDb.OpenRecordset(S, dbOpenSnapshot)
'loop through the recordset until the end
Do While Not r.EOF
If Not IsNull(r(pTextFieldname)) Then
mAllValues = mAllValues _
& " " & r(pTextFieldname) & mValueDeli
End If
r.MoveNext
Loop
If len(mAllValues) = 0 then
mAllValues = nz(pNoValue,"")
end if
Proc_Exit:
'close the recordset
r.Close
'release the recordset variable
Set r = Nothing
LoopAndCombine = Trim(mAllValues)
Exit Function
'if there is an error, the following code will execute
Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " LoopAndCombine"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit
End Function
'~~~~~~~~~~~~~~~~
pTablename As String --> "[Supplier Country]"
pIDFieldname As String --> "[Supplier Code Fieldname]"
pTextFieldname As String --> "[country_Fieldname]"
pValueID As Long --> [Supplier Code]
Optional pWhere As String --> if you want to further specify records
within a matching Supplier Code, if not --> just put comma to skip
argument if you are specifying optional arguments after this
Optional pDeli As String -- optional if you want something in
addition to space around each value -- a comma is used is nothing is
specified
Optional pNoValue as string --> what to return if there are no records
'~~~~~~~~~~~~~~~~
so, in a query, here is what you would do:
SuppCountry: LoopAndCombine(
"[Supplier Country]",
"[Supplier Code Fieldname]",
"[country_Fieldname]",
[Supplier Code Fieldname],
,
,
"No Country Code")
If your Supplier Code Field is not a long integer, you will need to
adjust the data types of the function parameter pValueID accordingly --
as well as add the proper delimiter to the string that is built.
For instance, if the Supplier Code Field is a string...
change
mAllValues = mAllValues _
& " " & r(pTextFieldname) & mValueDeli
to
mAllValues = mAllValues _
& " '" & r(pTextFieldname) & "'" & mValueDeli
Warm Regards,
Crystal
*
have an awesome day
*
MVP Access
Remote programming and Training
strive4peace2006 at yahoo.com
*
Ananth wrote:
I am comparing 2 Tables.
Table-A=Supplier Master
Table-B=Supplier Country
Both the Tables are linked to Supplier Code.
I am updating the Country Field in Table-A
When there is no matching Supplier code in Table B, the country Field
in Table-A is to be replaced with “No country codeâ€.
I tried using IIF, is null etc, but no success
I would consider any help a great one, at this moment pl