Search code examples
vbams-access

Ask for new record on form load


When i load a form a want to ask my users if they want to make a new record otherwise i would like to load the last record, this because the form will fill in some data by default.

Private Sub Form_Load()
Dim Nieuw

Nieuw = MsgBox("New Record?", vbYesNo, "New Record?")

If Nieuw = 6 Then
    DoCmd.GoToRecord acDataForm, "Main", acNewRec
Else
    DoCmd.GoToRecord acDataForm, "Main", acLast
End If

End Sub

When I use this script I got the following error

I couldn't find how to solve this issue


Solution

  • Just use the defaults:

    Private Sub Form_Load()
    
        Dim Nieuw As VbMsgBoxResult
    
        Nieuw = MsgBox("New Record?", vbYesNo, "New Record?")
    
        If Nieuw = vbYes Then
            DoCmd.GoToRecord , , acNewRec
        Else
            DoCmd.GoToRecord , , acLast
        End If
    
    End Sub