Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Batch/Entry/Detail into AM programmatically

Status
Not open for further replies.

Boris10

IS-IT--Management
Jan 26, 2012
97
KE
Hi Everyone, i have recorded a macro to create a batch, a new entry and add a detail to that enrtry in Asset Management Acquisition. The code that was recorded fails to run. I cant figure out why. I am getting the following errors:
1. You must Enter a least one detail before you can add the transaction
2. Method Insert of Object IAccpacView failed.

Below is the code :
Code:
On Error GoTo ACCPACErrorHandler

' TODO: To increase efficiency, comment out any unused DB links.
Dim mDBLinkCmpRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkCmpRW = OpenDBLink(DBLINK_COMPANY, DBLINK_FLG_READWRITE)

Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)

Dim temp As Boolean
Dim AMACQASST1batch As AccpacCOMAPI.AccpacView
Dim AMACQASST1batchFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0352", AMACQASST1batch
Set AMACQASST1batchFields = AMACQASST1batch.Fields

Dim AMACQASST1header As AccpacCOMAPI.AccpacView
Dim AMACQASST1headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0353", AMACQASST1header
Set AMACQASST1headerFields = AMACQASST1header.Fields

Dim AMACQASST1detail1 As AccpacCOMAPI.AccpacView
Dim AMACQASST1detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0354", AMACQASST1detail1
Set AMACQASST1detail1Fields = AMACQASST1detail1.Fields

Dim AMACQASST1detail2 As AccpacCOMAPI.AccpacView
Dim AMACQASST1detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0356", AMACQASST1detail2
Set AMACQASST1detail2Fields = AMACQASST1detail2.Fields

Dim AMACQASST1detail3 As AccpacCOMAPI.AccpacView
Dim AMACQASST1detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0355", AMACQASST1detail3
Set AMACQASST1detail3Fields = AMACQASST1detail3.Fields

AMACQASST1batch.Compose Array(AMACQASST1header)

AMACQASST1header.Compose Array(AMACQASST1batch, AMACQASST1detail1, Nothing, Nothing)

AMACQASST1detail1.Compose Array(AMACQASST1header, AMACQASST1detail2, AMACQASST1detail3, Nothing, Nothing, Nothing)

AMACQASST1detail2.Compose Array(AMACQASST1detail1)

AMACQASST1detail3.Compose Array(AMACQASST1detail1)


Dim AMACPT2 As AccpacCOMAPI.AccpacView
Dim AMACPT2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0374", AMACPT2
Set AMACPT2Fields = AMACPT2.Fields


AMACQASST1batch.Browse "(POSTFLAG!=1 and POSTFLAG !=2)", 1

AMACQASST1batchFields("ACQBATCH").PutWithoutVerification ("85")       ' Batch Number

AMACQASST1batch.Read
AMACQASST1batch.Browse "(POSTFLAG!=1 and POSTFLAG !=2)", 1

AMACQASST1batchFields("ACQBATCH").PutWithoutVerification ("2147483647")   ' Batch Number

AMACQASST1batch.Browse "(POSTFLAG!=1 and POSTFLAG !=2)", 0
AMACQASST1batch.Fetch
Dim AMACQASST3batch As AccpacCOMAPI.AccpacView
Dim AMACQASST3batchFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0352", AMACQASST3batch
Set AMACQASST3batchFields = AMACQASST3batch.Fields

Dim AMACQASST3header As AccpacCOMAPI.AccpacView
Dim AMACQASST3headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0353", AMACQASST3header
Set AMACQASST3headerFields = AMACQASST3header.Fields

Dim AMACQASST3detail1 As AccpacCOMAPI.AccpacView
Dim AMACQASST3detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0354", AMACQASST3detail1
Set AMACQASST3detail1Fields = AMACQASST3detail1.Fields

Dim AMACQASST3detail2 As AccpacCOMAPI.AccpacView
Dim AMACQASST3detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0356", AMACQASST3detail2
Set AMACQASST3detail2Fields = AMACQASST3detail2.Fields

Dim AMACQASST3detail3 As AccpacCOMAPI.AccpacView
Dim AMACQASST3detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0355", AMACQASST3detail3
Set AMACQASST3detail3Fields = AMACQASST3detail3.Fields

AMACQASST3batch.Compose Array(AMACQASST3header)

AMACQASST3header.Compose Array(AMACQASST3batch, AMACQASST3detail1, Nothing, Nothing)

AMACQASST3detail1.Compose Array(AMACQASST3header, AMACQASST3detail2, AMACQASST3detail3, Nothing, Nothing, Nothing)

AMACQASST3detail2.Compose Array(AMACQASST3detail1)

AMACQASST3detail3.Compose Array(AMACQASST3detail1)


Dim AMACQHEAD4header As AccpacCOMAPI.AccpacView
Dim AMACQHEAD4headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0353", AMACQHEAD4header
Set AMACQHEAD4headerFields = AMACQHEAD4header.Fields

Dim AMACQHEAD4detail1 As AccpacCOMAPI.AccpacView
Dim AMACQHEAD4detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0354", AMACQHEAD4detail1
Set AMACQHEAD4detail1Fields = AMACQHEAD4detail1.Fields

Dim AMACQHEAD4detail2 As AccpacCOMAPI.AccpacView
Dim AMACQHEAD4detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0356", AMACQHEAD4detail2
Set AMACQHEAD4detail2Fields = AMACQHEAD4detail2.Fields

Dim AMACQHEAD4detail3 As AccpacCOMAPI.AccpacView
Dim AMACQHEAD4detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "AM0355", AMACQHEAD4detail3
Set AMACQHEAD4detail3Fields = AMACQHEAD4detail3.Fields

AMACQHEAD4header.Compose Array(Nothing, AMACQHEAD4detail1, Nothing, Nothing)

AMACQHEAD4detail1.Compose Array(AMACQHEAD4header, AMACQHEAD4detail2, AMACQHEAD4detail3, Nothing, Nothing, Nothing)

AMACQHEAD4detail2.Compose Array(AMACQHEAD4detail1)

AMACQHEAD4detail3.Compose Array(AMACQHEAD4detail1)


AMACQASST3batch.Browse "(POSTFLAG=0 OR POSTFLAG=4)", 1
AMACQASST3batch.Init
AMACQHEAD4headerFields("ACQENTRY").PutWithoutVerification ("-2147483647")   ' Entry Number
AMACQHEAD4header.Browse "", 1
AMACQHEAD4header.Fetch
temp = AMACQHEAD4header.Exists
AMACQHEAD4header.Init
temp = AMACQHEAD4header.Exists
AMACQASST3batchFields("BATDESC").PutWithoutVerification ("Last BAtch ever for Macro")   ' Batch Description
AMACQASST3batch.Update

AMACQASST3batchFields("BTHDATE").PutWithoutVerification (DateSerial(2012, 12, 31)) ' Batch Date

AMACQASST3batch.Update

AMACQASST3batchFields("TXTYPE").PutWithoutVerification ("1")          ' Transaction Type

AMACQASST3batch.Update
temp = AMACQHEAD4header.Exists
AMACQHEAD4header.Init
temp = AMACQHEAD4header.Exists
AMACQHEAD4headerFields("ENTRYDESC").PutWithoutVerification ("Test Batch Entry")   ' Entry Description
AMACQHEAD4headerFields("DATEBUS").PutWithoutVerification (DateSerial(2012, 12, 30)) ' Posting Date


AMACQHEAD4headerFields("DEFTEMP").Value = "HEQUIP"                    ' Default Template
AMACQHEAD4headerFields("TRANSDATE").Value = DateSerial(2012, 12, 1)   ' Transaction Date
AMACQHEAD4headerFields("AQUCODE").Value = "OPACQ"                     ' Acquisition Code

AMACQHEAD4detail1.Browse "", 1

AMACQHEAD4detail1Fields("ACQLINE").PutWithoutVerification ("-2147483647")   ' Line Number

AMACQHEAD4detail1.Browse "", -1
AMACQHEAD4detail1.Fetch
AMACQHEAD4headerFields("ACCTID").Value = "20000000"                   ' Account
temp = AMACQHEAD4detail1.Exists
AMACQHEAD4detail1.RecordClear
temp = AMACQHEAD4detail1.Exists
AMACQHEAD4detail1.RecordCreate 0
AMACQHEAD4header.Update
AMACQHEAD4detail1Fields("OPDATE").Value = DateSerial(2012, 12, 31)    ' Last Depreciation Date
AMACQHEAD4detail1Fields("BKDPTIME").PutWithoutVerification ("1")      ' Accumulated Depr. Count
AMACQHEAD4detail1Fields("BKDPYTIM").PutWithoutVerification ("1")      ' Year Depreciation Count
AMACQHEAD4detail1Fields("PROCESSCMD").PutWithoutVerification ("2")    ' Process Command Code

AMACQHEAD4detail1.Process

AMACQHEAD4detail1Fields("ASSETNO").PutWithoutVerification ("HEQU-02323")   ' Asset ID
AMACQHEAD4detail1Fields("PROCESSCMD").PutWithoutVerification ("0")    ' Process Command Code
AMACQHEAD4detail1Fields("AUTONXNO").PutWithoutVerification ("2323")   ' Auto. Number

AMACQHEAD4detail1.Process

AMACQHEAD4detail1Fields("ACCSET").Value = "HQUIP"                     ' Account Set
AMACQHEAD4detail1Fields("ACQDATE").Value = DateSerial(2012, 12, 10)   ' Acquisition Date
AMACQHEAD4detail1Fields("RETIREDATE").PutWithoutVerification (DateSerial(2022, 12, 9))  ' Retirement Date

AMACQHEAD4detail1Fields("BKSDATE").Value = DateSerial(2012, 12, 11)   ' Start Date
AMACQHEAD4detail1Fields("BKLIFE").Value = "60.000"                    ' Book Estimated Life
AMACQHEAD4detail1Fields("OPDATE").Value = DateSerial(2012, 12, 30)    ' Last Depreciation Date
AMACQHEAD4detail1Fields("BKLTPERD").Value = "201308"                  ' Last Depreciation Period
AMACQHEAD4detail1Fields("BKVALUE").PutWithoutVerification ("100000.000")   ' Book Functional Value

AMACQHEAD4detail1Fields("ASSETDESC").PutWithoutVerification ("This is Asset Description")   ' Asset Description
AMACQHEAD4detail1Fields("COSTCENT").PutWithoutVerification ("HEQUIP")   ' Cost Center
AMACQHEAD4detail1Fields("RETIREDATE").PutWithoutVerification (DateSerial(2017, 12, 11)) ' Retirement Date
AMACQHEAD4detail1Fields("BKMETHOD").PutWithoutVerification ("DB")     ' Method
AMACQHEAD4detail1Fields("BKLTDP").PutWithoutVerification ("10000.000")   ' Last Period Depreciation
AMACQHEAD4detail1Fields("BKDP").PutWithoutVerification ("10000.000")   ' This Period Depreciation
AMACQHEAD4detail1Fields("BKYDP").PutWithoutVerification ("10000.000")   ' This Year Depreciation
AMACQHEAD4detail1Fields("BKACUDP").PutWithoutVerification ("10000.000")   ' Accumulated Depreciation
AMACQHEAD4detail1Fields("BKNETVAL").PutWithoutVerification ("90000.000")   ' Net Value
AMACQHEAD4detail1Fields("RATEDATE").PutWithoutVerification (DateSerial(2012, 12, 10)) ' Rate Date
AMACQHEAD4detail1Fields("BUDGCODE").PutWithoutVerification ("NULL")   ' Budget Code
AMACQHEAD4detail1Fields("BKDPTIME").PutWithoutVerification ("0")      ' Accumulated Depr. Count
AMACQHEAD4detail1Fields("BKDPYTIM").PutWithoutVerification ("0")      ' Year Depreciation Count
AMACQHEAD4detail1Fields("BKSVAL").PutWithoutVerification ("100000.000")   ' Book Source Value
AMACQHEAD4detail1Fields("BKSNET").PutWithoutVerification ("90000.000")   ' Source Net Value
AMACQHEAD4detail1Fields("BKSLTDP").PutWithoutVerification ("10000.000")   ' Source Last Depreciation
AMACQHEAD4detail1Fields("BKSDP").PutWithoutVerification ("10000.000")   ' Source This Period Depreciation
AMACQHEAD4detail1Fields("BKSYDP").PutWithoutVerification ("10000.000")   ' Source This Year Depreciation
AMACQHEAD4detail1Fields("BKSACDP").PutWithoutVerification ("10000.000")   ' Source Accumulated Depreciation
AMACQHEAD4detail1Fields("BKDPRATE").PutWithoutVerification ("20.000")   ' Depreciation Rate
AMACQHEAD4detail1Fields("BKSINITVAL").PutWithoutVerification ("100000.000")   ' Source Original Purchase Cost
AMACQHEAD4detail1Fields("BKFINITVAL").PutWithoutVerification ("100000.000")   ' Functional Original Purchase Cos

AMACQHEAD4detail1.Insert

AMACQHEAD4detail1Fields("ACQLINE").PutWithoutVerification ("-1")      ' Line Number

AMACQHEAD4detail1.Read

AMACQHEAD4header.Insert
AMACQASST3batch.Update

Exit Sub

ACCPACErrorHandler:
  Dim lCount As Long
  Dim lIndex As Long

  If Errors Is Nothing Then
       MsgBox Err.Description
       MsgBox Err.Source
  Else
      lCount = Errors.Count

      If lCount = 0 Then
          MsgBox Err.Description
          MsgBox Err.Source
      Else
          For lIndex = 0 To lCount - 1
              MsgBox Errors.Item(lIndex)
          Next
          Errors.Clear
      End If
      Resume Next

  End If

End Sub

Can anyone poin me in the right direction. Thank you in advance!
 
1. Remove all the temp= lines, they're junk from the recording.
2. You have 2 AMACQHEAD4header.Init statements, there should only be one, and I would normally use AMACQHEAD4header.RecordGenerate, False.
2. Exactly which line gives the error?
 
Hi Tuba, Thank you for the reply. I have removed one of the .Init lines. The problem is persisting.
The error is raised on this line:
Code:
AMACQHEAD4detail1.Insert

 
I see:
AMACQHEAD4header.Update

But nothing before that where Tuba and I are expecting a AMACQHEAD4header record to be created. The .init might be creating the record but I doubt it.

Run rvSpy as well to get a better idea of what the Views are doing when the generate the error.
 
.Init is the old method if creating (initializng) a record on older versions in older versions of Accpac.
But since he does not say which version of Accpac we would have to guess eh?
 
The bottom line is you need to get rid of the crap lines, like the .Browses and .RecordExists, and reduce it to:

Batch.recordgenerate, true
Header.recordgenerate, false
Header.Put
Detail.recordgenerate, false
Detail.put
Detail.insert
header.insert
batch.update
 
Thanks to everyone especially Tuba. I will try it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top