I'm getting this error message: Number of query values and destination fields are not the same.
This is the code: - INSERT INTO tblDateDifference ( DateDifference )
-
SELECT tblContracts.EndDate As EndDate,
-
IIf([Date()] > EndDate, DaysToCompletion(EndDate), DaysCompleted(EndDate)) AS DateDifference,
-
FROM tblContracts;
Can someone please tell me why I'm getting this error message? PLEASE?!?! lol
9 3341
Delete tblContracts.EndDate As EndDate, from the select statement. You only want the value returned from the iif statement to be inserted, correct.
No the select statement is necessary...then the IIf statements put the selected date into a module based on their values.
You only have one destination field listed, DateDifference, in the INSERT INTO statement. Anything listed in the SELECT statement SQL will try to insert into the feilds designated in the INSERT INTO clause. In your SELECT statement you have two values DateEnd and DateDifference, hence the error message. You will either have to add DateEnd into your list of destination fields or drop it from the SELECT statement.
If you are trying to insert datedifference only for certain enddates you need to incorporate enddate into a WHERE clause
DateDifference is what it's being assigned to I thought? I thought I was saying Insert into blah blah
Select blah blah as EndDate (I thought this was assigning it with a temporary name) then saying iif(so and so....blah blah come up with value) then assign the object I'm inserting into AS DateDifference
Is this incorrect?
Well anyways I wrote the code using a different method which worked just as well...but I'd still like to know about the proper way to write that and if I didn't understand it correctly if you happen to view this again. Thanks MVeda :)
The code I used is posted below in case anyone else needs help with this...it's code for a module:
Public Function DaysToCompletion() As Long
'Opening tblContracts as recSet1
Dim con1 As ADODB.Connection
Dim recSet1 As ADODB.Recordset
Set con1 = CurrentProject.Connection
Set recSet1 = New ADODB.Recordset
recSet1.Open "tblContracts", con1
'Declaring UntilCompletion as the amount of days until completion
Dim UntilCompletion As Long
'Looping until EOF (until the last record for EndDate in tblContracts...so
'someone else would have declared recSet1.Open
'"tblWhateverYourTableNameIs", con1 which means
'connection1 and then to open a field in that recordset you type
'recSet1.Fields("fieldname"))
Do Until recSet1.EOF
' End Date must be in quotes or will not work
UntilCompletion = DateDiff("d", Date, recSet1.Fields("EndDate"))
Debug.Print UntilCompletion
recSet1.MoveNext
Loop
'Clearing recSet1 and Con1 (Connection1)
recSet1.Close
con1.Close
Set con1 = Nothing
Set recSet1 = Nothing
End Function
Cheers,
Kosmös
You are inserting into one field, DateDifference. however you select two fields to insert, tblContracts.EndDate As EndDate AND the result of the iif statement as DateDifference.
It is the "AS dateDifference" that assigns the temporary name to the result of iif.
the part that I suggested you drop, tblContracts.EndDate As EndDate, is assigning the temporary name of EndDate to TblContracts.EndDate, and is not neccessary for the iif statement to function.
NeoPa 32,556
Expert Mod 16PB
Kosmos,
MSeda is right on the button here.
He has perfectly correctly (and quite clearly) answered your original question.
If you read it again carefully you should understand what was wrong with your SQL code.
Kosmos
As both NeoPa and MSeda have explained you have been given the answer. To explain it further You cannot do the two actions in the one INSERT statement. You will have to separate them out into two separate queries. Please find below the code as it should be written. -
INSERT INTO tblDateDifference ( DateDifference )
-
SELECT DaysToCompletion(EndDate) AS DateDifference
-
FROM tblContracts
-
WHERE Date() > tblContracts.EndDate;
-
-
INSERT INTO tblDateDifference ( DateDifference )
-
SELECT DaysCompleted(EndDate) AS DateDifference
-
FROM tblContracts
-
WHERE Date() <= tblContracts.EndDate;
-
Mary
Thanks...yeah I just hadn't understood exactly how things worked...the books availaible for my company were not very helpful in starting off...but I found a very good one recently that allows me to do more module based coding which I prefer more
By the way if anyone was following the code I put in to open the form...
you can just do:
Sub openForm()
DoCmd.openForm "frmCustomer"
End Sub
But it's better to open a form the other way because it's just better to practice it that way in case you are opening forms in other databases....this code assumes you are only working in the current database
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robert McEuen |
last post by:
Using Access 97, Windows XP
I'm receiving a Numeric Field Overflow error during text import that I
did not receive before I split my database. Another thread I found
suggested that the cause of...
|
by: ralamo |
last post by:
When i execute the following insert query, the above mentioned error
is coming. Anything wrong in the query?
INSERT INTO ECN_1 (Old_SbPartNo, Old_PartDesc, Old_ManPartNo,
Old_Manuf,...
|
by: Nathan Bloomfield |
last post by:
Hi there,
I am having difficulty with a piece of code which would work wonders
for my application if only the error trapping worked properly.
Basically, it works as follows:
- adds records...
|
by: windandwaves |
last post by:
Hi Gurus
Does anyone know how I set the error trapping to option 2 in visual basic.
I know that you can go to tools, options and then choose on unhandled errors
only, but is there a VB command...
|
by: Ecohouse |
last post by:
I have a main form with two subforms. The first subform has the child
link to the main form identity key.
subform1 - Master Field: SK
Child Field: TrainingMasterSK
The second subform has a...
|
by: rn5a |
last post by:
During registration, users are supposed to enter the following
details: First Name, Last Name, EMail, UserName, Password, Confirm
Password, Address, City, State, Country, Zip & Phone Number. I am...
|
by: Shokoth |
last post by:
Hi
I have been trying to run this code, and what it is doing is inserting records into a table called tblRequestByService. the values that it is inserting are taken from the form itslef (They are...
|
by: cypriot |
last post by:
Hi. I am developing an application program in java. I use MsAccess for keeping data.
I had a problem with sql insert method.
public void AddPatient()
{
String...
|
by: hyperpau |
last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding.
I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com).
Ergo, I...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |