doCmd.RunSQL Help

J

JB

Upon button click, I need to set the value of a control
on the current form to the max of field within a table
plus one.

I've tried the following but keep getting errors. (BTW-
the query actually works fine when run separately.) Any
help?

Me!housingInvoiceNbr = DoCmd.RunSQL("SELECT max
(housingInvoiceNbr)+1 as myInvoice from tblHouseStay")

Thanks.
JB
 
D

Dirk Goldgar

JB said:
Upon button click, I need to set the value of a control
on the current form to the max of field within a table
plus one.

I've tried the following but keep getting errors. (BTW-
the query actually works fine when run separately.) Any
help?

Me!housingInvoiceNbr = DoCmd.RunSQL("SELECT max
(housingInvoiceNbr)+1 as myInvoice from tblHouseStay")

RunSQL only works for action queries that don't return a value. You can
extract the value you want, though, by using the DMax() function:

Me!housingInvoiceNbr = _
DMax("housingInvoiceNbr", "tblHouseStay") + 1

In case there is no record in the table, yet, you may want to wrap that
in an Nz() function expression:

Me!housingInvoiceNbr = _
Nz(DMax("housingInvoiceNbr", "tblHouseStay"), 0) + 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top