Referencing data in a subform

Richard1941

Registered User.
Local time
Yesterday, 16:28
Joined
Oct 29, 2012
Messages
34
My form has a subform called subformDestinations that has one field Destinations. When I type in that field the record that is the record source for the main table gets updated correctly. Outside the subform there is a button to add the new value to a lookup table. I tried the following code in the click event:

Private Sub Command219_Click()
Dim strData As String '' Declare a String Variable
Dim sql As String '' sql String
strData = Me.Destinations
sql = "Insert into LUtblDestinations (Column1,FKID) Values (""" & strData & """);"
DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True
End Sub

However, the Me.Destinations causes Compile error: Method or data member not found. How do I refer to the data I want to use to update the other table?
 
The syntax for referencing a Control (like a text box) on a sub form is;

MainFormName!SubformControlName.Form!ControlName

Note that SubformControlName refers to the name of the Subform Control (which is the "window" that contains the sub form) not the name of the sub form itself. The Subform Control and the sub form itself may or may not have the same name. So in your case it would look something like;

Me!subformDestinations.Form!Destinations
 
We're almost there. Now it's complaining that I haven't supplied the right number of values for the table I'm updating. The table has only two columns. The first one is the primary key and defined by AutoNumber. The second is the field I'm trying to fill in. Do I need something other than FKID?
 
sql = "Insert into LUtblDestinations (Column1,FKID) Values (""" & strData & """);"

should be

sql = "Insert into LUtblDestinations ([FKID]) Values (""" & strData & """);"
 
We did it! It needed a bit more tweeking, but with your help I figured it out.
Once again, thanks much
 

Users who are viewing this thread

Back
Top Bottom