Working code from one form gives "An exception occurred" error on another form

I successfully use “Password Code” in one form, but when I try to use in in a different form I get “Error” on “Code Line Error”. It’s frustrating, and I can’t figure out why this is happening. Both are Main forms, not Sub Forms. Can anyone shed some light on this? Thanks.

                                   *Password Code*

SUB Confirm_Password()
Dim oForm As Object
oForm = ThisComponent.Drawpage.Forms.getByName(“MainForm”)
oPassword = oForm.getByName(“Txt_Password”)
oConfirm = oForm.getByName(“Lbl_Confirm”)
Password = oPassword.Text
Confirm = oConfirm.Label
If Password = Confirm Then
ChangeFrm_Modify
Else
oPassword.Text = “”
msgbox (“Try again!”)
End If
End Sub

                                          *Error*

BASIC runtime error.
An exception occurred
Type: com.sun.star.container.NoSuchElementException
Message: .

                                        *Code Line Error*

oPassword = oForm.getByName(“Txt_Password”)

The “NoSuchElementException” usually means that one of the controls on the form, or the main form itself, has not been named properly or does not exist on the form.

  • Verify proper form and control names and that they exist, especially the main form having name “MainForm” and the two controls with names “Txt_Password” and “Lbl_Confirm”. The getByName method is case sensitive so check that the names match exactly as the code (or change the code to match control names). Also check for empty spaces before or after the names.
  • The oForm (“MainForm”) can be more correctly referenced by using the getByIndex(0) method instead of the getByName() method. The main form is typically at index 0, but may not always have the name “MainForm”.
  • Also check that the referenced controls are actually in the main form and not in a subform! In form edit, click form / form navigator, examine list - or - right click the “Txt_Password” control / Form Properties, verify form name. To change the location of the control, it can either be redrawn in the correct form, or just “drag and drop” it into the correct form in the Form Navigator list. Save the changes and save the odb and restart to ensure the changes.

Thank you sooo much for your informative response. This helped my understanding of things a lot. The issue here was in:

it should have been txt_Password. I stared at this for an amount of time that I’m not doing to disclose, and still missed it. I’ve fixed it, and it is now working the way that it’s supposed to.

Thanks again, you’re awesome!!