Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 29th, 2004, 09:00 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Command Button VB Code

I have a Form that displays account name and number with a sub form that displays sales information for a specific account, selected by the main form combo box. The form is based on a multi table query.

What I would like to do is put some code behind a command button, that updates one field in all the records - eg Archive which is a true/false field. eg I have ten records of sales information displayed that I would like to set the archive field to true (basically updating the underlying table of sales info)

Does anyone have any ideas - it would be greatly appreciated

taraj

Reply With Quote
  #2 (permalink)  
Old July 29th, 2004, 01:51 PM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

The button's On Click event would basically have a RunSQL command such as this:

DoCmd.RunSQL "UPDATE tblMyTable SET tblMyTable.fArchive = True;"

Where you put your names instead of tblMyTable and fArchive.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
Reply With Quote
  #3 (permalink)  
Old July 29th, 2004, 02:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Taraj:

Another way to go...

Every bound form in an .mdb is always bound to a DAO recordset. You can clone a sub form's recordset in code, and update any field values you like. Calling the Update method on the cloned recordset automatically updates your bound form. No requery necessary.

~~~~Code~~~~

Private Sub Command0_Click()
    Dim rst As DAO.Recordset

    ' Clone the sub form's recordset
    Set rst = Me!fsubChildren.Form.RecordsetClone

    rst.MoveFirst

    ' Update sub form field values
    With rst
      .Edit
      !Archive = True
      .Update
    End With

End Sub

HTH,

Bob



Reply With Quote
  #4 (permalink)  
Old July 29th, 2004, 02:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Note:

One advantage of the clone appraoch is the cloned recordset contains only the records currently displayed in the subform, already filtered by the parent form's primary key;

Greg's approach works fine too, but since you're using a parent/child relationship, be sure and add a where conditioin to your update statement.

DoCmd.RunSQL "UPDATE tblMyTable SET tblMyTable.fArchive = True " & _
             "WHERE tblMyTable.YourLinkChildField = " & Me!fsubChildren.Parent.YourLinkMasterField

Execuing a SQL statement will also require refreshing the subform with either of the following:

Me!fsubChildren.Form.Refresh
Me!fsubChildren.Form.Requery

HTH,

Bob

Reply With Quote
  #5 (permalink)  
Old July 30th, 2004, 03:51 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Guys
Thanks very much for the information - off to try it now - will get back if any problems

Taraj



Reply With Quote
  #6 (permalink)  
Old July 30th, 2004, 05:19 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bob

This is the code that I have used as per your advise - Have I got it entered correctly as the .edit fails? I've probably done something stupid

Dim rst ADODB.Recordset

       Set rst=Me!Archive_Sales_Subform1.Form.RecordsetClone

       rst.movefirst

With rst

       .edit
       !Archive = true
       .update
End with

Your help would be greatly appreciated

Tara J


Reply With Quote
  #7 (permalink)  
Old July 30th, 2004, 05:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Taraj,

Bound form's in .mdb projects use a DAO recordset (not ADO). The RecordsetClone property copies this native DAO recordset object and returns a reference to the copy:

Dim rst As DAO.Recordset

Access's default data access object library is ADO 2.x, but bound foms in .mdbs still use DAO recordsets. Its one of those things Microsoft never got around to rearchitecting. (The RecordsetClone property in an .adp project does return a reference to an ADO recordset.)

Set a reference to the Microsoft DAO 3.6 Object Library if you havn't.

HTH,

Bob

Reply With Quote
  #8 (permalink)  
Old July 30th, 2004, 08:46 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bob

Thanks for the information - I've set the references up as required.

However I get a can't find field error message on the following:

set rst=Me!Archive_Sales_Subform1.Form.RecordsetClone

The Archive_Sales_Subform1 = Form Name
Field to change is Archive

If I do set rst = RecordsetClone - the button works and checks only the first record on the form.

Could you advise me if I have the syntax right on your suggested code

Many thanks

Reply With Quote
  #9 (permalink)  
Old July 30th, 2004, 11:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Taraj,

Quote:
quote:However I get a can't find field error message on the following:
I was able to reproduce your error by misspelling my subform name, though there may be other causes:

   Run-time error '2465' : Microsoft Access can't find the field 'fssubChildren' referred to in your expression."

Should the '1' be there in your subform name?

The subform reference syntax is correct. The fully qualified version would be:

set rst = Forms("frmParentForm").Controls("Archive_Sales_Sub form1").Form.RecordsetClone

"Archive_Sales_Subform1" references the subform container control, 'Form' references the actual subform object the subform control contains, and whose properties are exposed. The Controls collection is the default property of the Form object, so it doesn't need to be referenced explicity.

Quote:
quote:the button works and checks only the first record on the form.
I initially ran my code against a recordset that contained a single record. To iterate through all the records in the subforms recordset use:

' Update sub form field values
With rst
   Do Until .EOF
      .Edit
      !Archive = True
      .Update
      .MoveNext
   Loop
End With

Also, here's a little debugging routine to view the records in your recordset while you're working in the code window:

' Clone the sub form's recordset
Set rst = Me!Archive_Sales_Subform1.Form.RecordsetClone

rst.MoveFirst

' Dump recordset to debug window
Do Until rst.EOF
   For Each fld In rst.Fields
     Debug.Print fld.Name & vbTab & fld.Value
   Next
   rst.MoveNext
Loop

rst.MoveFirst ' Return to first record in recordset before processing.

Let me know if this helps.

Bob



Reply With Quote
  #10 (permalink)  
Old July 30th, 2004, 01:02 PM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bob

Many thanks for your help - it has worked perfectly

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Access help - VB Code for command button pari22 Access VBA 11 September 7th, 2007 11:13 AM
How to Click a web button using VB code AJD Access VBA 6 June 6th, 2006 07:32 AM
How to Click a button on a web page in VB code AJD Beginning VB 6 0 June 2nd, 2006 12:08 PM
cannot insert OLE command button event code janefu Excel VBA 1 January 10th, 2006 04:33 AM
VB 6: Coolbar Dropdown Button Code craigharrison Pro VB 6 0 October 16th, 2003 02:48 AM



All times are GMT -4. The time now is 01:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.