Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Posts
    18

    Unanswered: -

    -
    Last edited by TheGame7; 11-26-03 at 09:15.

  2. #2
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    465

    Re: VBA Code..Sub Forms and Forms

    Originally posted by TheGame7
    Hi,

    After using the search facility, I was able to discover how to refer to a subform when using VBA code:

    Forms![MainFormName].[SubformName].Form.[ControlName].Value = "Whatever"

    However, when I do this, if I open the form used for the sub form individually, it produces an error, or when using SQL, a parameter query.

    Is there any way around this?
    You have a common error in the code the righ is:

    Forms![MainFormName]![SubformName]![ControlName] = "Whatever"

    if SubformName is subform of MainFormName and ControlName is a Control of the SubformName.
    Saludos
    Norberto

  3. #3
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    465
    If you want set a reference to a control field of the subform from a form the way is this:
    Forms![Pedidos]![SubformPedidos]![Cantidad]
    or
    Forms![Pedidos]![SubformPedidos].Forms![Cantidad]
    its the same.
    But if you want to set a reference to control subform in a form the way is:
    Forms![Pedidos]![SubformPedidos].Form
    ok.
    Saludos
    Norberto

  4. #4
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    465
    Sorry but i thing that the problem is not in the code.....is in the form.
    When you open the form the query run and don't find the subform because is empty or close, then the query return an error.
    Saludos
    Norberto

  5. #5
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238
    Originally posted by TheGame7
    I don't think you understand me fully.

    It is something in the code, since if I access it as a sub form from the main form it WORKS!

    If I access the form alone, i.e. double clicking it from the list of forms, I get a parameter query (error)

    Anyone understand?
    How can I get it to work both ways?
    A form can be in one of 2 states - as a stand alone form or as a sub-form. The reason you get an error message when you open the sub form as a form is that it is looking for a parent form that you have referenced in SQL or VBA. No parent form is available so you get an error. You need a function to test to see how the form has been opened:

    Public Function IsSubForm(frm As Access.Form) As Boolean
    ' Check the parent property to see if one exists
    Dim strName As String
    On Error Resume Next
    strName = frm.Parent.Name
    IsSubForm = (Err.Number = 0)
    Err.Clear
    End Function

    to use the function in your form you use code such as:

    If IsSubForm(Me) Then
    ' It's a sub form - change SQL or anything else as required
    Else
    ' It's a stand alone form
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •