Hi Kabing,
I'm having a bit of a play with your dB now. I'll show you where I'm up to
I attached it to the "On Record Change" event of the New_Location_Data_Entry form. But it doesn't appear to work.
Yes that event has a trick with it to get it to work. It is explained in the "Forms and Dialogs" pdf by C. Benitez P18
http://www.geocities.com/rbenitez22/ Basically, as the event calls the macro from both the form and the controller the "paths" change so you need an "if" / "else" statement.
I have used a similar event "before record action" which fires when you do a save (the one you have fires whenever you navigate away to another "location" record). The code for this event then calls the (renamed) macro "Copy_Location_Name_To_Field_Trips_Data_Entry_Form". Here is the code.... note: I did not rename "MainForm"
Code: Select all
Sub BeforeRecordAction_Save_Locations (event As Object)
rem ..... As before record action triggers in two different ways this macro is nessicary.
rem .... In this case the macro is called by "before record action"
Dim Form As Object
Dim Control As Object
Dim ControllerName As String
Dim FormName as String
ControllerName="com.sun.star.form.FmXFormController"
FormName = "com.sun.star.comp.forms.ODatabaseForm"
If Event.Source.ImplementationName=ControllerName Then
Form=Event.Source.Model
Rem MsgBox "FROM FORM CONTROLLER:"
Rem Just before save completes the Location Name is coppied to Field Trip Form
If Form.IsNew = True then
Rem This is where it calls the other sub
Copy_Location_Name_To_Field_Trips_Data_Entry_Form
End if
elseIf Event.Source.ImplementationName=FormName Then
Rem
Rem The sub just exits each time it is called from the form. I could have put the code in this section rather than above if wanted to
exit sub
Rem MsgBox "FROM FORM CONTROLLER:
' Form=Event.Source
End If
End Sub
With your version the problem would also be with the line that has "boundfield.string". As we are getting the value from a textbox we can just use:
Code: Select all
Location_Name = oForm.GetByName("txtLocationName").Text
The other code is as follows note I have renamed the sub..
Code: Select all
Rem Macro to copy the Boundfield of a listbox in one form to the combobox entry field in another form.
Rem Trigger from the end of the "State_Listbox_Copy_and_Reset macro and the "After_Record_ListBox" macros could be a good choice.
Sub Copy_Location_Name_To_Field_Trips_Data_Entry_Form
Dim tmp as Object
Dim oForm as object
Dim Location_Name as string
Rem... This is getting the required data from the boundfield of the second listbox.
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
Location_Name = oForm.GetByName("txtLocationName").Text
msgbox "transferring" & Location_Name
tmp = thisComponent.Parent.FormDocuments
if tmp.HasByName("Field Trips Data Entry") then
tmp = tmp.getByName("Field Trips Data Entry")
if not IsNull(tmp.Component) then
tmp = tmp.Component.DrawPage.Forms.getByName("FieldTripsInfo")
if tmp.HasByName("cbxLocation") then
if tmp.GetByName("cbxLocation").currentvalue = "" then
tmp.getByName("cbxLocation").Boundfield.UpdateString(Location_Name)
else if tmp.GetByName("cbxLocation").currentvalue <> Location_Name then
If MsgBox("The combobox in the ""Field Trip"" form already has something different in it. This form is about to close. Would you like to exit this form without changing that Combobox?",4,"ComboBox Already Has An Entry!") =6 Then
Exit Sub
Else
tmp.getByName("cbxLocation").Boundfield.UpdateString(Location_Name)
End If
end if
end if
endif
endif
endif
End Sub
I have also put a "Save record and exit" button on your location form which does a "save" and closes down the location form. I'm also putting some other tricks in there which I will show you a bit later. I'll PM it back to you in a bit later today hopefully.
I did manage to get your "after record change" macro for the listbox's to work properly. Remember we were having problems with the
stuff. My fault, I forgot it wasn't an integer we were dealing with but a string so that line should be:
. Here is the whole sub:
Code: Select all
Sub After_Record_Listbox (oEv as object)
dim oForm as object
Rem Using the event to get to the form.
oForm=oEv.Source
Rem Calling limit items again
Rem You will need to change the number in GetInt(4) to reflect the column number from your data table that holds the "countries" listbox bound field.
limitItems ( oForm, oForm.GetString(6) )
oForm.getbyname("lbxState").reset()
' msgbox oForm.GetString(6)
End sub
Try it out. You will now see the "state" listbox display something as you change records. (I have gone and changed the "add data only" option of the mainform back to "No" to get back to only using one copy of that form, the "NEW" one).
Anyway to put some of these changes in now by copying them off this post you could.....
1/ Rename the form back to "Mainform"
2/ Copy the (newly named) "Copy_Location_Name_To_Field_Trips_Data_Entry_Form" sub over the other one
3/ Copy into your macro the "Before record action" sub and have it triggered by that "mainform" event from your "locations" form (don't use "before record change")
4/ Copy in the updated "after record change" sub and make sure that the correct event from the mainform is firing it. (don't use "after record action")
5/ Go to your "Mainform" properties and change "add data only" back to "no" so you can check step 4 is doing its job properly.
I'll let you know when I have a bit more.
Cheers
Voo