473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DoCmd.RunSQL INSERT INTO


I am trying to insert an employee number into the EmpNbr field in my
main table from a form where I add a new employee to my employee table.
I was hoping this command would work, but it isn't.

DoCmd.RunSQL "INSERT INTO [tblCernerHours] VALUES ([EmpNbr])", 0

This is the only field I want to populate in the main table from this
form. there are three other fields in the main table that I want to
leave unpopulated.

Thanks in advance,

David

Nov 13 '05 #1
6 68476
If you build the query in the design grid, Access give this as the SQL.

DoCmd.RunSQL "INSERT INTO tblCernerHours ( EmpNbr ) SELECT 0 AS Expr1;"

--
Wayne Morgan
MS Access MVP
"David" <da****@mail.heartland-health.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

I am trying to insert an employee number into the EmpNbr field in my
main table from a form where I add a new employee to my employee table.
I was hoping this command would work, but it isn't.

DoCmd.RunSQL "INSERT INTO [tblCernerHours] VALUES ([EmpNbr])", 0

This is the only field I want to populate in the main table from this
form. there are three other fields in the main table that I want to
leave unpopulated.

Thanks in advance,

David

Nov 13 '05 #2
David wrote:
I am trying to insert an employee number into the EmpNbr field in my
main table from a form where I add a new employee to my employee table.
I was hoping this command would work, but it isn't.

DoCmd.RunSQL "INSERT INTO [tblCernerHours] VALUES ([EmpNbr])", 0


David, try something like this for your SQL clause:

"INSERT INTO [tblCernerHours] ([EmpNbr]) VALUES ( employee number you
wish to insert into the EmpNbr field goes here);"
basic syntax:
INSERT INTO
table to insert into
fields to populate, in parens
VALUES
values to dump in those fields, also in parens

....in that order
If EmpNbr is a string/character you need single quotes around the value.

HTH,

--
Terrell Miller
mi******@bellsouth.net

"Every gardener knows nature's random cruelty"
-Paul Simon RE: George Harrison
Nov 13 '05 #3
"Wayne Morgan" <co***************************@hotmail.com> wrote:
If you build the query in the design grid, Access give this as the SQL.

DoCmd.RunSQL "INSERT INTO tblCernerHours ( EmpNbr ) SELECT 0 AS Expr1;"


Trouble is Access doesn't support the VALUES functionality (which I use all the time)
in the design grid.

One of these days I'm going to build some kinda automated routine where I pick the
fields from a table and get a copy of a VALUES query in the clipboard for pasting in
the code.

No time today to do it so maybe tomorrow. Or next week. <smile>

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #4
Thank you Wayne, Terrell, and Tony for your responses. Terrell, I tried
your suggestion, and was able to get the new record inserted into the
main tbl., but the new data (EmpNbr) that was typed in the form for the
new employee is not being inserted into the new record. (It is being
inserted into the Emp. table first). Here is what puts a new blank
record in the main table:

DoCmd.RunSQL "INSERT INTO [tblCernerHours] ([EmpNbr]) VALUES
([EmpNbr]);"
Here is what my code looks like for the Enter button on my form:

Private Sub cmdEntNewRep_Click()
On Error GoTo Err_cmdEntNewRep_Click

DoCmd.GoToRecord , , acNewRec
DoCmd.RunSQL "INSERT INTO [tblCernerHours] ([EmpNbr]) VALUES
([EmpNbr]);"
cboSelEmp.Requery
cboSelEmp.SetFocus
cboSelEmp = ""

Exit_cmdEntNewRep_Click:
Exit Sub

Err_cmdEntNewRep_Click:
MsgBox "This Employee number is already in this database!",
vbCritical
Resume Exit_cmdEntNewRep_Click
End Sub

Thanks again for your time.
David

Nov 13 '05 #5
David wrote:
DoCmd.RunSQL "INSERT INTO [tblCernerHours] ([EmpNbr]) VALUES
([EmpNbr]);"


David, you need to concatenate a couple of strings here. You are trying
to insert the current value of the EmpNbr field *in the record you are
creating* into that field *in the record you are creating*.

But that field doesn't have a value yet, since you just created the
record. So you are taking a null field value and inserting it back into
the same field in the same record.

try "INSERT INTO [tblCernerHours] ([EmpNbr]) VALUES (" & 12345 & ")"

where 12345 is whatever the actual employee number happens to be.

So you are starting off with the first part of a string that includes
your SQL clause up to the left-parens after VALUES, then concatenating
in the actual value of your EmpNbr control on the form, then adding the
closing parens in a third string. All that gets rolled into one long
string, and that is executed by the RunSQL command.

HTH,

--
Terrell Miller
mi******@bellsouth.net

"Every gardener knows nature's random cruelty"
-Paul Simon RE: George Harrison
Nov 13 '05 #6
Hi Terrell, thanks for your help. I tried your last code, and it isn't
working. if you are interested in taking a look at it, I will send the
db to you. It is only 69 kb's zipped. My E-mail add. is:
da****@heartland-health.com

Thanks again for your good help.

David

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy_HR | last post by:
i have a table named tblClanovi now i would like to make a sql insert into command that will add new record in the database... Private Sub btnSpremi_Click() Dim broj Dim imepr broj =...
7
by: Richard Hollenbeck | last post by:
Help! I don't know why this isn't working: Private Sub Combo9_Change() Dim UsersCourseSelection As String UsersCourseSelection = Me("Combo9").Value Combo13.Visible = True 'the following...
3
by: | last post by:
I have the following lines in a macro: sql = "INSERT INTO IM (ITEM, DESC) VALUES (vitem, ndesc)" DoCmd.RunSQL sql Every time I execute the macro, I get a run time error saying there is a...
1
by: Bob Bridges | last post by:
I don't see it in the documentation, but it seems whenever I try an append command adding a value to a memo field, the query bombs. Here's a sample: DoCmd.RunSQL "INSERT INTO Who (Type, Name,...
3
by: deejayquai | last post by:
Hi I'm attempting to insert multiple records from one table into another based on rows selected in a multiple list box. This all seems to be working OK until I get to a field where the value is...
3
by: tricard | last post by:
Good afternoon, I am having troubles with an insert into statement in Access 2002 that I am performing through VBA. I am inserting a single record into a table with one yes/no field. For some...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
3
kcdoell
by: kcdoell | last post by:
Hello: I have a Insert Into command that currently works: DoCmd.RunSQL "INSERT INTO TypeEorD ( EorDName, EorD ) SELECT Trim( & '_' & Trim(Category)), Trim(CatType);" "Category" is a manual...
0
BenRatcliffe
by: BenRatcliffe | last post by:
Hi there, I was wondering if anyone could help me. I have a comlpex database with a number of forms that have data entered on them and then saved into the correct table etc. In this instance I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.