Hello John
Many thanks for your reply. I'm glad my little outburst hasn't got me
blacklisted!
I now understand that the code I need would have to take account of the
form's underlying recordset - in terms of any WHERE clause and/or any
multiple tables.
Before I go further I should explain what I want to do - because in fact I
can see two ways of solving this, one of which is along the lines of my
question in this post and the other is along the line of a post that I made
on the FormsCoding newsgroup last Friday (at 14:17), and it is probably only
worth persevering with one of the two approaches.
Essentially I want to add a button to the form header of a particular form
(which opens in continuous form view) which, when clicked, will add a
user-specified value to a particular field (called [stmt_datee]) in the
detail section for each of the records that are currently displayed: this
set of records will be form's opening dataset as further amended by the user
implementing various filters (using filter-by-selection).
The approach I was asking about on the FormsCoding newsgroup was to set the
value directly on the form by 'cycling' through the filtered recordset. In
the form header I have a textbox called [recs] with control source
=Count([ID1]) (ID1 is the key field from the underlying table, so [recs] is
a reliable count of the number of records being displayed), and the code I
had was:
Private Sub Command37_click()
Dim sdate As Date
Dim inc As Integer
sdate = InputBox("Enter statement date")
DoCmd.GoToRecord , , acFirst
[stmt_datee].Value = sdate
For inc = 1 To [recs].Value - 1
DoCmd.GoToRecord , , acNext
[stmt_datee].Value = sdate
Next inc
End Sub
But this doesn't work: sometimes it just sets the [stmt_datee] value in the
first or one other record, and sometimes I get a "Cannot go to the specified
record" message - even when there are, say, 4 records displayed.
I hope this makes sense, and that help me see (a) which approach to use and
(b) where I'm going wrong!
Many thanks
Les
John Spencer said:
Although Steve's answer may have been a bit terse, it was correct. You
really did not give enough information to provide a more definitive
answer.
I would say that yes it is possible in many (not all) cases. The
implementation depends on having some knowledge of VBA and SQL beyond
using the query grid. You will probably need to build the query's where
clause on the fly.
The VBA might be as simple and straightforward as the following or it
might need to be more complex - for instance
-- does the query for the form have a where clause
-- are there multiple tables involved for the form
Dim DbAny as DAO.Database
Set DbAny = CurrentDb()
StrWhere = Forms!FormName!Filter
strSQL = "UPDATE SomeTable SET SomeField = SomeValue" & _
" WHERE " & strWhere
DbAny.Execute strSQL, DbFailonError
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County