Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Query From Button Click Event 1

Status
Not open for further replies.

hammertounge

Programmer
Jan 9, 2002
12
0
0
CA
How do I ru a query from a buttons click event. I have a database with a primary key I must increment manually. I select max(key) from table to get the highest or latest key and then I want ot add one to it and put it in the form tstbox.value but I am lost as to how to run the query from the click event of the add new record button.

Thanks in advance:)
 
DoCmd.OpenQuery "YourQueryName" runs the query "YourQueryName". I'm a little confused as to exactly what you're doing, but this code will run the query.

Hope this helps.

The Missinglinq "It's got to be the going,
not the getting there that's good!"
-Harry Chapin
 
Hi there,

This will set you on the right track.

Under your command button, place this code on the onclick event, go to code builder.


Private Sub YourCommandButtonName_Click()
Dim maxkey As Long
Dim strSQL As String
Dim dbs As Database
Dim rst As Recordset


Set dbs = CurrentDb
strSQL = "SELECT YourTableName.key FROM YourTableName;"
Set rst = dbs.OpenRecordset(strSQL)
rst.MoveLast
maxkey = rst.Fields("key") + 1

Me.YourTxtField = maxkey

End Sub

Let me know if you have any problems.

Peter
Remember- It's nice to be important,
but it's important to be nice :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top