VB icon

AS400 to Excel

Email
Submitted on: 1/19/2015 1:44:00 AM
By: Dan Belluscio (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VBA MS Excel
Views: 2440
 
     This code uses a value in cell B2 in Excel as a lookup on the AS400 and returns values to cells C2 and D2 in Excel You must add a reference to: Microsoft ActiveX DAta Objects 2.0 library
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: AS400 to Excel
' Description:This code uses a value in cell B2 in Excel as a lookup on the AS400 and returns values to cells C2 and D2 in Excel
You must add a reference to:
Microsoft ActiveX DAta Objects 2.0 library
' By: Dan Belluscio (from psc cd)
'
' Inputs:Put the value you would like to lookup in cell B2
then run macro
'
' Assumes:DSN-Less connection
'**************************************

Dim CN As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim strSQL As String
Sub GetNameCity2()
CN.Open "Driver={Client Access ODBC Driver (32-bit)}; System=typeyouras400ipaddress-or-as400namehere; Uid=typeyouras400Namehere; Pwd=typeyouras400passwordhere;" ' open connection to database
'this section retrieves the name and site
'PLTFILES# is the library
'ONETI561 is the file
'NAME, CITY, ADRNUM are the fields to retrieve
RS.Open strSQL, CN
strSQL = "select NAME, CITY, ADRNUM from PLTFILES#.ONETI561 where PRADDR = 'Y' AND ADRNUM = '" & Range("B2").Value & "'"
RS.Open strSQL, CN
If RS.BOF Or RS.EOF Then
 msgbox "Could not find lookup value."
Else
 RS.MoveFirst
 Range("C2").Value = RS.Fields(0)
 Range("D2").Value = RS.Fields(1)
End If
 
RS.Close 'Close recordset
CN.Close 'Close connection
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.