Ask Your Question
0

Base ERROR - Open Form with macro

asked 2016-08-28 17:29:03 +0100

kirsch gravatar image

updated 2016-08-28 17:29:47 +0100

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 ?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2016-08-28 19:48:50 +0100

peterwt gravatar image

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.

edit flag offensive delete link more
0

answered 2016-08-28 18:40:48 +0100

Ratslinger gravatar image

updated 2016-08-28 18:41:32 +0100

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.

edit flag offensive delete link more

Comments

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 ?

padricLibre gravatar imagepadricLibre ( 2016-08-29 16:41:28 +0100 )edit

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.

Ratslinger gravatar imageRatslinger ( 2016-08-29 16:59:58 +0100 )edit

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.

padricLibre gravatar imagepadricLibre ( 2016-08-29 17:15:12 +0100 )edit

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'.

Ratslinger gravatar imageRatslinger ( 2016-08-29 17:24:07 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-08-28 17:29:03 +0100

Seen: 646 times

Last updated: Aug 28 '16