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 November 25th, 2008, 03:43 PM
Registered User
 
Join Date: Nov 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert new record using onnotinlist

HI,

I originally posted this in the wrong forum, so I moved it here.

I am pretty new to Access and am struggling with something.

I have three tables I am working with.

tblCommuter
tblLocation
tblJunctionCommuterLocation

I have a form based on tblCommuter and a subform based on tblJunctionCommuterLocation. I want to write a new record into tblJunctionCommuterLocation and currently it won't let me add the new data.

I am currently trying to add a record into the field LocationId, which is a lookup field based on a query.The lookup field allows me to make new write in the form of an address while generating new autonumbers for the LocationId. Is this clear?

Eventually I want to also have the code write the same data to tblLocation by generating an AutoId to use as the Primary Key for that table, but I first just need to figure how to get the code to allow me to insert into tblJunctionCommuterLocation.

The code I am trying to use is below. Any help is appreciated thank you.

Paul



Private Sub LocationID_NotInList(NewData As String, Response As Integer)

    Dim strsql As String, x As Integer
    Dim LinkCriteria As String

    x = MsgBox("Do you want to add this value to the list?", vbYesNo)

If x = vbYes Then
        strsql = "Insert Into tblJunctionCommuterLocation ([LocationID]) values (" & NewData & ")"

        Response = acDataErrAdded
    Else
        Response = acDataErrContinue

    End If

End Sub


 
Old November 25th, 2008, 04:12 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

If this is an unbound combo box, it is pretty easy to add the item to the list. The issue is when it is bound like this. Normally what I do is open the Location Form in Modal, and have the user fill it out to add the new location (in your case) using the proper form. Then requery the combo box on the On Close event of the Add New Location Form. How exactly you do that part is up to you.

Simply inserting the new location like this may get these results...

User types in "samp" lokking for the location "Sample", then hits return.

Your message box pops up, then user selects Yes, and "samp" is entered as a new location.

You still haven't updated your combo box.

Did any of that help?

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old November 25th, 2008, 04:19 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

That may also be a non-updatable query issue as well. How did you create the form / sub-form? If you used the auto form wizard, then the code I said should work without me seeing more. You just need to use the regular Add New Location form that you normally use (in Modal to prevent strays) and then refresh your current form. Also, your primary key field should be autonumber, so you wouldn't insert that, is that correct? Just insert the Location, and the LocationID is generated for you. You can't insert an autonumber. Your query results should also not display the ID, just meaningful data.



mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert where record doesn't already exist Neal SQL Server 2000 3 February 4th, 2008 10:09 AM
how Insert record into two tables mfarooqw ASP.NET 1.0 and 1.1 Basics 2 July 10th, 2007 05:38 PM
Insert record disabled jking Access 9 January 10th, 2006 04:31 PM
Insert new Record priority05 Classic ASP Databases 2 September 6th, 2004 12:26 PM





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