Skip to main content

Script for On Error Resume Next in IDEA

Hi All,
Someone help me please, I want to join database A (Primary) and database B (Secondary) with "All Records in Primary File". My trouble is when in database B there is no records (this is cause depend on automatic script before) then the macro is stop.
May anyone can help me? is there any script like in excel (On Error Resume Next) to join those databases?
thank you

Brian Element Sat, 02/20/2016 - 15:54

Hi mobbie,

Sorry for taking so long to get back to you.  Here is something you can try.  You first check to see if there are records in the secondary file, if there are you perform the join, if not you rename the primary file to the join fle name so any functions that rely on the join should work.

Hope this helps.

Brian

Sub Main
	Call JoinDatabase()	'@GEL2 - Sales transactions-Database (3).IMD
End Sub


' File: Join Databases
Function JoinDatabase
	Dim db As database
	Dim task As task
	Dim ProjectManagement As Object
	
	'check to see if the secondary file has records and if not rename the primary file to the new file name
	If getNoOfRecords("Secondary File.IMD") > 0 Then
		Set db = Client.OpenDatabase("Primary File.IMD")
		Set task = db.JoinDatabase
		task.FileToJoin "Secondary File.IMD"
		task.IncludeAllPFields
		task.IncludeAllSFields
		task.AddMatchKey "FIELD1", "FIELD1", "A"
		task.CreateVirtualDatabase = False
		dbName = "Join Databases.IMD"
		task.PerformTask dbName, "", WI_JOIN_ALL_IN_PRIM
		Set task = Nothing
		Set db = Nothing
	Else
		'rename the primary database to the new name
		Set ProjectManagement = client.ProjectManagement
			ProjectManagement.RenameDatabase "Primary File.IMD", "Join Databases"
		Set ProjectManagement = Nothing

	End If
	Client.OpenDatabase (dbName)
End Function

Function getNoOfRecords(sTempFilename As String) As Long
	Dim db As database
	Set db = Client.OpenDatabase(sTempFilename)
		getNoOfRecords = db.count
	Set db = Nothing

End Function