Here is a full example of the VBA sample code with a loop. This VBA code loops through an Excel spreadsheet and inserts records in the MS Dynamics GP Payroll Posting Accounts table UPR40500. In this case we only insert accounts for Gross Pay.
The VBA sample code also create a record set named (rs) from the GL00100 table that will provide the GL account index for the full account. The full GL account is matched to the record in the GL00100 table to get the account index.
Comment lines begin with an apostrophe (‘).
Public Sub GetAccounts()
‘Define ADO connection object and recordset object
Dim cn As ADODB.Connection, rs As ADODB.Recordset
‘set cn (connection) to hold database connection string
‘set rs (recordset) to hold the results for the retrieved record set ‘from SQL
Set cn = New ADODB.Connection
cn.Open “Provider=’SQLOLEDB’;Data Source=’dh-PC’;Initial Catalog=’TWO’;” & _
“Integrated Security=’SSPI’”
Set rs = New ADODB.Recordset
‘strSQL will hold the select SQL statemment to run
‘rs.open opens the record set with strSQL, and connection cn
Dim strSQL As String
‘ declare other variables
Dim strDept As String
Dim strPosition As String
Dim strPayCd As String
Dim strMainAcct As String
Dim strPayType As Integer
Dim strAcctIndx As Integer
Dim strFullAcct As String
Dim rsFullAcct As String
‘Read cells from payroll accounts sheet starting in row 2
Dim i As Integer
i = 2
strDept = ActiveWorkbook.Sheets(“PRA”).Cells(i, 1).Value
strPosition = ActiveWorkbook.Sheets(“PRA”).Cells(i, 2).Value
strPayCd = ActiveWorkbook.Sheets(“PRA”).Cells(i, 3).Value
strMainAcct = ActiveWorkbook.Sheets(“PRA”).Cells(i, 4).Value
strPayType = ActiveWorkbook.Sheets(“PRA”).Cells(i, 5).Value
Let strSQL = “select * from GL00100″
rs.Open strSQL, cn, adOpenStatic, adLockReadOnly, adCmdText
‘Use a MsgBox to display a messge and field contents for debuging, ‘remove the apostrophe in from the MsgBox statement below to see ‘how it works.
‘MsgBox strSQL
‘Process all the rows in the spreadsheet
Do Until IsEmpty(Cells(i, 1))
‘step through the record set and perform required actions on the records
‘reference the fields using the column name from the SQL table
Do While Not rs.EOF
‘retrieve the GL account index from the first row of the dataset
strFullAcct = strDept & strMainAcct & strPosition
rsFullAcct = Left(rs!ACTNUMBR_1, 3) & Left(rs!ACTNUMBR_2, 4) & Left(rs!ACTNUMBR_3, 2)
If strFullAcct = rsFullAcct Then intID = rs!ACTINDX
‘move to the next record in the recordset
rs.MoveNext
Loop
‘strAcctIndx holds the account index retrieved from the record set
‘set the department, position, pay code, pay type 1 = gross pay
strAcctIndx = intID
‘adjust department and position code strings to proper length
strDept = Right(strDept, 2)
strPosition = Right(strPosition, 2)
‘create the sql string to insert the record into the Payroll posting accounts table
strSQL = “INSERT INTO UPR40500 (DEPRTMNT,JOBTITLE,PAYROLCD,UPRACTYP,ACTINDX) VALUES”
strSQL = strSQL & “(” & strDept & “,” & strPosition & “,’” & strPayCd & “‘,” & strPayType & “,” & strAcctIndx & “)”
‘MsgBox strSQL
cn.Execute strSQL
i = i + 1
strDept = “”
strPosition = “”
strPayCd = “”
strMainAcct = “”
strPayType = 0
strAcctIndx = 0
strFullAcct = “”
rsFullAcct = “”
strDept = ActiveWorkbook.Sheets(“PRA”).Cells(i, 1).Value
strPosition = ActiveWorkbook.Sheets(“PRA”).Cells(i, 2).Value
strPayCd = ActiveWorkbook.Sheets(“PRA”).Cells(i, 3).Value
strMainAcct = ActiveWorkbook.Sheets(“PRA”).Cells(i, 4).Value
strPayType = ActiveWorkbook.Sheets(“PRA”).Cells(i, 5).Value
Loop
MsgBox “Import Completed”
‘close the open record set
rs.Close: Set rs = Nothing
‘close the connection to the database
cn.Close: Set cn = Nothing
End Sub