Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2006, 08:07 AM
Registered User
 
Join Date: Jul 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default DoCmd.GoToRecord , , acNewRec doesnt create new re

Hi,

I'm having a problem when using DoCmd.GoToRecord , , acNewRec in the following code.

It doesn't create a new record. Instead, it changes the current record in the recordset.

What i really want to do is create a new record.

Private Sub Engine2_Click()
On Error GoTo ErrorHandler

    Dim strSearch As String

    Dim intAC_ID As Integer
MsgBox "In engine 2 click"
    'For text IDs
    'strSearch = "[______ID] = " & Chr$(34) & Me![cboSelect] & Chr$(34)

    'For numeric IDs
    'strSearch = "[______ID] = " & Me![cboSelect]
    strSearch = "[AE_AC_ID] = " & Me![cmbAE_AC_ID] & _
        " AND [AE_E_NO] = 2 "

    intAC_ID = Me![cmbAE_AC_ID]

    'Find the record that matches the control

    Me.RecordsetClone.FindFirst strSearch
    If (Me.RecordsetClone.NoMatch = True) Then
        MsgBox "couldn't find a match creating new record"
        DoCmd.GoToRecord , , acNewRec
        txtAE_E_NO = 2
        cmbAE_AC_ID = intAC_ID
        Me![cmbEngine2] = Null
        MsgBox "creating New record cmbAE_AC_ID = " & Me![cmbAE_AC_ID]

    Else
        MsgBox " Found: Bookmarking???"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End If


ErrorHandlerExit:
    Exit Sub

ErrorHandler:
    MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
    Resume ErrorHandlerExit

End Sub
 
Old August 21st, 2006, 06:03 AM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi David,

When you need to do something like adding a new record while you're working in a bound form, what you want to do is create the new record in the "background". If you need to position to the new record, use bookmarking once the record has been added.

Here's the excerpt I noticed:

    Me.RecordsetClone.FindFirst strSearch
    If (Me.RecordsetClone.NoMatch = True) Then
        MsgBox "couldn't find a match creating new record"
        DoCmd.GoToRecord , , acNewRec
        txtAE_E_NO = 2
        cmbAE_AC_ID = intAC_ID
        Me![cmbEngine2] = Null
        MsgBox "creating New record cmbAE_AC_ID = " & Me![cmbAE_AC_ID]

    Else
        MsgBox " Found: Bookmarking???"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End If


Assuming a table name of "tblEngine" (for example purposes only, this is what I'd do:

#1 Declare a string for a SQL statement
      Dim strSQL As String

#2 Modify the section for the .FindFirst logic - something like this:

    Me.RecordsetClone.FindFirst strSearch
    If (Me.RecordsetClone.NoMatch = True) Then
        MsgBox "couldn't find a match creating new record"
        Me![cmbEngine2] = Null
        MsgBox "creating New record cmbAE_AC_ID = " & Me![cmbAE_AC_ID]
        strSQL = ""
        strSQL = strSQL & "INSERT INTO tblEngine "
        strSQL = strSQL & "("
        strSQL = strSQL & "AE_E_NO, "
        strSQL = strSQL & "AE_CD_ID "
        strSQL = strSQL & ")"
        strSQL = strSQL & " VALUES "
        strSQL = strSQL & "("
        strSQL = strSQL & "2, "
        strSQL = strSQL & intAC_ID
        strSQL = strSQL & ")"
        CurrentProject.Connection.Execute (strSQL)
        Me.Bookmark = Me.RecordsetClone.Bookmark
    Else
        MsgBox " Found: Bookmarking???"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End If


I'm kind of guessing with the column names, but this should hopefully give a better idea.

Hope it helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
DoCmd.GoToRecord Error in Access VBA myth12345 Access VBA 4 February 16th, 2006 01:58 AM
GoToRecord Error using VBA tied to Command Button pkaptein1 Access 5 May 28th, 2005 09:49 AM
DoCmd.GoToRecord Error in Access VBA myth12345 Access VBA 1 October 30th, 2004 07:29 AM
DoCmd.GoToRecord Error in Access VBA myth12345 Access VBA 1 October 28th, 2004 10:27 AM
DoCmd.GoToRecord Error myth12345 VB How-To 0 October 28th, 2004 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.