[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Adding record to a table from data entry in another unbound form

Posted on 2013-05-31
5
Medium Priority
?
1,798 Views
Last Modified: 2013-06-20
Someone helped me a while back with an issue where I was trying to add a record to an unbound table from a field data entry in a form.  Here's my code but it's broken and I can't figure out why.

When I try to close the form I get an error..."Too few parameters.  Expected 1"

Here's my code...

Private Sub Form_Close()

    'Code to create a new record in tblExample with values from combobox if that combination of data doesn't already exist in the table
    If Me.cboProjectName > "" Then
        If DCount("*", "tblProjectNames", "ProjectName = " & Chr(34) & Me.cboProjectName & Chr(34)) > 0 Then
        'MsgBox "This record already exists."
        Else
            MsgBox "This appears to be a new record which does not already exist in the program.  A new record is being created."
        Dim RS As DAO.Recordset
        Set RS = CurrentDb.OpenRecordset("tblProjectNames")
            RS.AddNew
            RS!ProjectName = Me.cboProjectName
            RS.Update
        End If
    End If
    'End of code to create a new record in separate table with values from combobox if that record doesn't already exist in the table


End Sub
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:SteveL13
ID: 39210858
Actually, here is what I am trying to accomplish.  I want the user to be able to type a value in a combobox and then be able to select that value for future records.  I guess I don't care if that value is entered into the unbound table.

What is the best method to accomplish this?
0
 
LVL 85
ID: 39211061
then be able to select that value for future records.
So you want to select a single value, and then use that value for other things (like searches, or something like that)?

If so, you can use a variable declared at the proper "scope". If this is all done on a Form, then just declare your variable in the General Declarations section of that Form's code module:

Public gMyVar As String

Then you can set that value as needed:

gMyVar = "Bob"

If you need greater scope, then you can use the hidden form technique, which has several advantages. To do that, create a form that contains several textboxs, and open that form when the database first opens (you can open it as Hidden to hide it from view). Once that's done, you can then set your values into those textboxes:

Forms("YourHiddenForm").SomeTextbox = "Bob"

To refer to that value from anywhere in your app, you'd do this:

Msgbox "The current user is " &  Forms("YourHiddenForm").SomeTextbox
0
 

Author Comment

by:SteveL13
ID: 39211340
No, I want to be able to either select an existing value OR type in a new value.  If I type a new value I want to add that value to the unbound table but my code is broken.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 39211499
You can modify your code below:

Dim RS As DAO.Recordset
        Set RS = CurrentDb.OpenRecordset("tblProjectNames")
            RS.AddNew
            RS!ProjectName = Me.cboProjectName
            RS.Update
        End If

To this:

Currentproject.Connection.Execute "INSERT INTO tblProjectNames(ProjectName) VALUES('" & Me.cboProjectName & "')"

That will insert a new record into the table.
0
 

Author Comment

by:SteveL13
ID: 39211604
This is crazy!  For some reason the following code which I think is the last suggestion, is creating TWO records in tblProjectNames. One which is correct or what I would expect, but the 2nd record being created has the key number of the drop-down in the ProjectName field.

????????

Code:

Private Sub Form_Close()

    'Code to create a new record in tblExample with values from combobox if that combination of data doesn't already exist in the table
    If Me.cboProjectName > "" Then
        If DCount("*", "tblProjectNames", "ProjectName = " & Chr(34) & Me.cboProjectName & Chr(34)) > 0 Then
            'MsgBox "This record already exists."
            Else
            MsgBox "This appears to be a new record which does not already exist in the program.  A new record is being created."
            CurrentProject.Connection.Execute "INSERT INTO tblProjectNames(ProjectName) VALUES('" & Me.cboProjectName & "')"
        End If
    End If
    'End of code to create a new record in separate table with values from combobox if that record doesn't already exist in the table

End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question