Learning SQL .... Help Please

T

TeeSee

The following SQL compiles and runs as expected.

strSQL = "INSERT INTO tblMaterialMasterHistory ( ChngeDate,
SISitemCode,"
strSQL = strSQL & " OldDiscount )"
strSQL = strSQL & " SELECT Date()as ChngeDate,
tblMaterialMaster.SISItemCode,"
strSQL = strSQL & " tblMaterialMaster.Discount"
strSQL = strSQL & " FROM tblMaterialMaster"
strSQL = strSQL & " WHERE ((tblMaterialMaster.SISItemCode) Like "
& """" & strItem & """)"

Now when I try to introduce a stand alone variable which was
established thru an input box I get error code 3061 "too few
parameters" Expected 1

Dim sglDiscount as Single

strSQL = "INSERT INTO tblMaterialMasterHistory ( ChngeDate,
SISitemCode,"
strSQL = strSQL & " OldDiscount, NewDiscount)"
strSQL = strSQL & " SELECT Date()as ChngeDate,
tblMaterialMaster.SISItemCode,"
strSQL = strSQL & " tblMaterialMaster.Discount, 'sglDiscount' as
NewDiscount"
strSQL = strSQL & " FROM tblMaterialMaster"
strSQL = strSQL & " WHERE ((tblMaterialMaster.SISItemCode) Like "
& """" & strItem & """)"
The above compiles and steps thru the entire code with no error but
does NOT update NewDiscount. It must be in the syntax.
Any thoughts please?
 
D

Douglas J. Steele

The SQL you've written is attempt to write the string "sqlDiscount' to your
history table.

Is that what you want, or are you actually trying to write the value
contained in a variable named sglDiscount? If it's the variable you want,
use

strSQL = "INSERT INTO tblMaterialMasterHistory ( ChngeDate,
SISitemCode,"
strSQL = strSQL & " OldDiscount, NewDiscount)"
strSQL = strSQL & " SELECT Date(), tblMaterialMaster.SISItemCode,"
strSQL = strSQL & " tblMaterialMaster.Discount, " & sglDiscount
strSQL = strSQL & " FROM tblMaterialMaster"
strSQL = strSQL & " WHERE ((tblMaterialMaster.SISItemCode) Like "
& """" & strItem & """)"

Note that unless strItem contains a wildcard character, there's no reason to
use Like (use = instead). Note, too, that I removed the aliases "ChngeDate"
and "NewDiscount" from your subselect: they serve no useful purpose.
 
T

TeeSee

The SQL you've written is attempt to write the string "sqlDiscount' to your
history table.

Is that what you want, or are you actually trying to write the value
contained in a variable named sglDiscount? If it's the variable you want,
use

strSQL = "INSERT INTO tblMaterialMasterHistory ( ChngeDate,
SISitemCode,"
    strSQL = strSQL & " OldDiscount, NewDiscount)"
    strSQL = strSQL & " SELECT  Date(), tblMaterialMaster.SISItemCode,"
    strSQL = strSQL & " tblMaterialMaster.Discount, " & sglDiscount
    strSQL = strSQL & " FROM tblMaterialMaster"
    strSQL = strSQL & " WHERE ((tblMaterialMaster.SISItemCode) Like"
& """" & strItem & """)"

Note that unless strItem contains a wildcard character, there's no reasonto
use Like (use = instead). Note, too, that I removed the aliases "ChngeDate"
and "NewDiscount" from your subselect: they serve no useful purpose.

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)










- Show quoted text -

Thanks Doug ... That did the trick. Just a couple of comments .... my
strItem does and most often will have wildcards in it and I found the
Date() as ChngeDate in the SQL test within a blank query grid SQL.
Thanks for bringing those to my attention.

With regards to the fields in the Append query is it imperative that
the order of the fields in the INSERT line is identical to the order
of the incoming data on the SELECT line?
 
J

John W. Vinson

With regards to the fields in the Append query is it imperative that
the order of the fields in the INSERT line is identical to the order
of the incoming data on the SELECT line?

Absolutely. That's the only way that Access can tell which value to put into
which field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error 3141 Cannot resolve 3
What's wrong w/ this SQL stmt? 1
Another SQL quandry 7
Problem with SQL 5
HelpWith SQL 1
Access 2002 SQL Problem 1
SQL Help Please 1
error 3061 with insert into sql 6

Top