Results 1 to 10 of 10
  1. #1
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169

    VBA to update field contents...Error 91

    I know this is probably something basic but my VBA knowlege is pretty limited. I am trying to update a field After Update in a form using the following code but getting an error message - "Run-time error '91': Object variable or With block variable not set"



    I guess I am missing something. Thanks for any help.

    Code:
    Private Sub Form_AfterUpdate()
        Dim MyForm As Form
            MyForm!Updates = MyForm!Updates & Chr(13) & Chr(10) & _
            "***Response Updated***"
            MyForm.Lastupdated = Now()
            MyForm.LastUpdatedBy = Environ("USERNAME")
            Exit Sub
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Hint: what form do you intend this code to work on?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    I was thinking I have to declare the form name somehow but not sure. The form is frmQandRsubform. Dim frmQandRsubform as form?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't typically use the Form type (I'd be more likely to use a string variable, but that's just me). You'd need a new line like:

    Set MyForm = Forms!frmQandRsubform
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    JIm,

    I agree with Paul on this. using the FORM type in visual basic is cumbersome because the syntaxes related to it and its members have changed from version to version.

    is this code behind the actual form that you're updating? what control are you changing? one on a subform, or one on the main form? for instance, to change a control on a subform, this would be needed:
    Code:
    me.subformcontrol.form.controls("updates") = value
    there is really no need to declare anything.

    and...staying with your code, yes, it will error on this line:
    Code:
    MyForm!Updates = MyForm!Updates & Chr(13) & Chr(10) & _
    because the MyForm object is not associated with anything by using the SET statement.

  6. #6
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Thanks Adam. I am trying to update a value in a table associated with the main form when the subform is udpated. I have data fields in the subform that point to the table. So, I have the code on the After Update event of the subform to update the value in the table via the field on the subform. Does this make sense?

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by jpkeller55 View Post
    I am trying to update a value in a table associated with the main form when the subform is udpated. I have data fields in the subform that point to the table. So, I have the code on the After Update event of the subform to update the value in the table via the field on the subform.
    Quote Originally Posted by jpkeller55 View Post
    Does this make sense?
    No. How about answering these...

    *whats the source of the subform (record source, that is)
    *whats the source of the main form
    *whats the control name thats providing the update to the table
    *where is that control? on the main or the sub?
    *do you actually know that the sub's afterupdate event is firing? if so, how do you know?

  8. #8
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Adam, see my responses in blue...

    *whats the source of the subform (record source, that is) - qryQandR

    *whats the source of the main form - qryQuestion
    *whats the control name thats providing the update to the table - Updates
    *where is that control? on the main or the sub? - sub
    *do you actually know that the sub's afterupdate event is firing? I assume so but don't know for sure. if so, how do you know?

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    let's say that on the AFTER UPDATE of the sub, you want to popuplate 2 controls on the main with NOW() and USERNAME.

    if that is so, you will need:
    Code:
    sub_afterupdate
    
    me.parent.form.controls("NOWcontrol") = NOW()
    me.parent.form.controls("USERNAMEcontrol") = environ("username")
    just trying to get you one step closer to your own solution here. you can use the 'parent' property on any subform, anywhere. it is much much easier than messing with objects and form names. e.g. - these two do the same thing:
    Code:
    me.parent
    
    forms("mainform")
    does that help at all?

    <edit>
    to update a control on the main from UPDATES on the sub:
    Code:
    me.parent.control = me.updates

  10. #10
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Thanks Adam...clearly I need to do some homework. Will let you know if I get this resolved.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 11-09-2011, 11:34 AM
  2. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM
  3. Replies: 3
    Last Post: 07-21-2010, 02:41 AM
  4. Remove contents from each field
    By nancyszn in forum Access
    Replies: 7
    Last Post: 08-06-2009, 03:41 PM
  5. Fields and their contents
    By Ham in forum Access
    Replies: 0
    Last Post: 02-06-2009, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums