What is the 'best' way to auto populate "UDF" City and "UDF" State from "UDF" Zip Code

I am currently using the following (which works), but is there a 'better' way to do this?

(I don't like hardcoding the connection string with username and password.)

'-----------------------------------------------------------------------------------------------------------------------------------------------------------------

'Thomas Graff, Eastern Technologies, Inc. 2/6/2018
'This script is designed to look up the state and city from information in the Zip Code maintenance table
' and then populate the City and State fields.

'Init VARs
EPAZIP = ""

retVal = oBusObj.GetValue("UDF_EPA_ZIPCODE$", EPAZIP)


Set SQLDB = CreateObject("ADODB.Connection")
SQLDB.OPEN("Provider=SQLNCLI11;User ID=<id>;PWD=<pw>;Initial Catalog=MAS_SYSTEM; Data Source=<server>;")
Set rs = CreateObject("ADODB.Recordset")
rs.open "SELECT City, StateCode, CountryCode from SY_ZipCode where ZipCode like '"+EPAZIP+"'",sqldb,1,1
if rs.eof = false then
 retVal = oBusObj.setValue("UDF_EPA_City$", rs("City").value)
 retVal = oBusObj.setValue("UDF_EPA_State$", rs("StateCode").value)
end if
rs.close

'-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks,

-Tom

  • 0

    Hey Tom,

    You could just use Sage 100's objects for this instead.  Assuming the above is attached to the Validation of the UDF_EPA_ZIPCODE field?

    retVal = oBusObj.GetValue("UDF_EPA_ZIPCODE$", EPAZIP) ' could use value instead of EPAZIP

    Set oZip = oSession.AsObject(oSession.GetObject("SY_ZipCode_svc"))

    retVal = oZip.Find(EPAZIP)

    If retVal = 1 Then

        retVal = oZip.GetValue("City$", EPACITY)

        retVal = oZip.GetValue("StateCode$", EPASTATE)

        retVal = oBusObj.SetValue("UDF_EPA_City$", EPACITY)

        retVal  - oBusObj.SetValue("UDF_EPA_State$", EPASTATE)

    End If

    Hope this helps

    Elliott

  • 0 in reply to jepritch

    Awesome thanks!