Base ERROR - Open Form with macro

I’m having a problem opening several forms by macro :

Opening a first Form “F_Contact.” by macro works fine :

Sub Ouvre_Recherche_par_nom( oEvent as variant )
ThisDatabaseDocument.FormDocuments.getByName(“F_Contact”).open
End Sub

Opening a other Form " New_Contact" with a other similar macro is not working :

Sub Ouvre_New_Contact( oEvent as variant )
ThisDatabaseDocument.FormDocuments.getByName(“New_Contact”).open
End Sub

gives an ERROR, Type: com.sun.star.container.NoSuchElementException Message: New_Contact !

I probaly missunderstand some programming details.
Does anybody know the trick ?

Here is a macro which can be called with the name of the form you want to open:

SUB FormChange( sFormName )
  Dim ObjTypeWhat
  Dim ObjName As String
  Dim sName as String
  Dim sTitle As String
  Dim iStart As Integer
'Remove comment on these next four lines if you want to close the currently open form  '
'   sTitle = ThisComponent.Title  '
'   iStart = Instr(sTitle,":") + 2  '
'   sName = Mid(sTitle, iStart)  '
'   ThisDatabaseDocument.FormDocuments.getbyname( sName ).close  '
  ObjName = sFormName
  ObjTypeWhat = com.sun.star.sdb.application.DatabaseObject.FORM
  If ThisDatabaseDocument.FormDocuments.hasbyname(ObjName) Then 'Check the form exists'
     ThisDataBaseDocument.CurrentController.Connect() 'If the form exists connect to the database'
     ThisDatabaseDocument.CurrentController.loadComponent(ObjTypeWhat, ObjName, FALSE) 'Open the form'
  Else
      MsgBox "Error! Wrong form name used. "+chr(10)+"Form Name = " & ObjName
  End if
End Sub

With this in place you would use this statement: FormChange("New_Contact"). If the form name used is not found an error will display.

Ratslinger, a bit off topic here but I see you are able to post code in its proper format. I have tried the 101010 option but it puts stuff in a single line like your use statement in red. You also have the grey box with proper formatting. How can I do the latter ?

If no new line character in pasted data then single line. Sometimes need to add spaces at beginning of each line to get completely correct. In Answer you can see what will be displayed but not in Comments.

Thanks, are you saying enter “char(13)” or something like that into the code? When I tried it before the code was properly displayed when I copied it from Notepad but was converted into a single line. Sorry to be thick headed here. Or is it cntrl+x or something. Don’t know the proper cntrl sequences.

You possibly had “Word Wrap” (or equivalent) on in Notepad. As these comments create unnecessary messages to the question originator and others, if more questions please ask as ‘new’.

Modify your Sub to -


Sub Ouvre_New_Contact( oEvent as variant ) 
temp() = ThisDatabaseDocument.FormDocuments.ElementNames
count = ThisDatabaseDocument.FormDocuments.Count
MsgBox count
For i = 0 to count -1
MsgTxt = MsgTxt & Chr$(13) &  temp(i)
Next
MsgBox MsgTxt

ThisDatabaseDocument.FormDocuments.getByName("New_Contact").open 
End Sub

This will show the number of Forms in your Database and the names of the Form Documents. If the Name is there you should be able to open it.