avu.code
avu.code

Reputation: 1

Refresh a Table in Access VBA in background?

I want to refresh the Table in Access automatically every time after the Command Button is clicked. My code below works fine, but it will switch/open the Table. I would like to make it stay in the Form window only, not switch to the Table. Any idea is appreciated:

    Private Sub Command0_Click()
    ''MsgBox Time
    Dim db As Database
    Set db = CurrentDb

    currID = Time
    n = "lala"

    db.Execute "INSERT INTO Table1 ([Task],[From],[To]) VALUES ('" & n & "',#" & currID & "#,#" & currID & "#)", dbFailOnError
    Set db = Nothing

    DoCmd.SelectObject acTable, "Table1"
    DoCmd.Requery
    DoCmd.GoToRecord acDataTable, "Table1", acLast

    End Sub

Upvotes: 0

Views: 6459

Answers (3)

4dmonster
4dmonster

Reputation: 3031

Try Me.Recordset.Requery this command does requery and keeps current record.

Upvotes: 0

Anthony Montalbano
Anthony Montalbano

Reputation: 1

Me.Requery

Me.Recordset.MoveFirst

worked for me as well and positioned the active record as I would generally prefer.

Upvotes: 0

AHeyne
AHeyne

Reputation: 3475

I assume that your form is also bound to Table1.

So instead of

DoCmd.SelectObject acTable, "Table1"
DoCmd.Requery
DoCmd.GoToRecord acDataTable, "Table1", acLast

you can use

Me.Requery
Me.Recordset.MoveLast

Upvotes: 1

Related Questions