SQL Error Help

R

RayToddJr

The following SQL simply won't run.

I get the following error in the fourth line down. "Expecting End of
Statement".
Any suggestions as to what I've done wrong would be appreciated.

Thanks,

Ray.


strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber)"
strSQL = strSQL + "VALUES (" & lngPropertyID & "," & lngDefendantNameID
& "," & lngDocumentTypeID & ","""
strSQL = strSQL + "& lngDefendantTypeID & "," & txtLienDate & "," &
txtLienAmount & ",""
strSQL = strSQL + "& txtInstrumentNumber & "," & txtBookNumber & "," &
txtPageNumber & ")""
 
J

John W. Vinson

The following SQL simply won't run.

I get the following error in the fourth line down. "Expecting End of
Statement".
Any suggestions as to what I've done wrong would be appreciated.

Thanks,

Ray.


strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber)"
strSQL = strSQL + "VALUES (" & lngPropertyID & "," & lngDefendantNameID
& "," & lngDocumentTypeID & ","""
strSQL = strSQL + "& lngDefendantTypeID & "," & txtLienDate & "," &
txtLienAmount & ",""
strSQL = strSQL + "& txtInstrumentNumber & "," & txtBookNumber & "," &
txtPageNumber & ")""

You've got quite a few extra quotemarks. You need the final SQL string to have
# delimiters around the date value, quotemarks around any Text datatype
fields, and no delimiters around number fields. In any case you should end up
with an even number of " marks in each line and your fourth line contains an
extra " in front of the first ampersand.

Which fields are Text, if any? The fieldnames are all something ID or Number -
are these really numbers, or text fields containing an identifier?
 
R

RayToddJr

Hello John and thanks:

All of the fields except the following are numbers:

LienDate -Date
Instrument Number -Text
BookNumber -Text
PageNumber -Text

I guess I simply don’t understand the quote rules well enough in how they
should and shouldn’t be used. I have attempted to do as you stated however,
none of the information is being passed through in the sql.

This is how I intreperted what you wrote:

strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber) "
strSQL = strSQL + "VALUES(lngPropertyID,lngDefendantNameID,lngDocumentTypeID,"
strSQL = strSQL + "lngDefendantTypeID, #txtLienDate# , txtLienAmount ,"
strSQL = strSQL + "& txtInstrumentNumber & , & txtBookNumber & , &
txtPageNumber & )"

This is the resulting SQL string:

INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID, DocumentTypeID,
DefendantTypeID, LienDate LienAmount, InstrumentNumber, BookNumber,
PageNumber)
VALUES(lngPropertyID,lngDefendantNameID,lngDocumentTypeID,lngDefendantTypeID,
#txtLienDate# , txtLienAmount ,& txtInstrumentNumber & , & txtBookNumber & ,
& txtPageNumber & )
 
J

John Spencer

strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber) "
strSQL = strSQL + "VALUES(" & lngPropertyID & ", " & _
lngDefendantNameID & ", " & _
lngDocumentTypeID & ", " & _
lngDefendantTypeID & ", #" & txtLienDate & "#, " &
"""" & txtLienAmount & """, " & _
"""" & txtInstrumentNumber & """, " & _
"""" & txtBookNumber & """, " & _
"""" & txtPageNumber & """)"

You have to insert the values of the variables into the string and not the
name of variables. Also, you need to delimit the text strings with quote
marks and the date with # marks. One way to get quote marks into the string
is to put two together and then surround the two with quote marks.

Another way is to use an apostrophe ' as the text delimiter and surround that
with quotes.

"'" & txtInstrumentNumber & "', " & ...

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
D

Daryl S

You are also missing a comma - try this from John's (I added the comma after
LienDate):

strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate, "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber) "
strSQL = strSQL + "VALUES(" & lngPropertyID & ", " & _
lngDefendantNameID & ", " & _
lngDocumentTypeID & ", " & _
lngDefendantTypeID & ", #" & txtLienDate & "#, " &
"""" & txtLienAmount & """, " & _
"""" & txtInstrumentNumber & """, " & _
"""" & txtBookNumber & """, " & _
"""" & txtPageNumber & """)"
 
J

John W. Vinson

LienDate -Date
Instrument Number -Text
BookNumber -Text
PageNumber -Text

I guess I simply don’t understand the quote rules well enough in how they
should and shouldn’t be used. I have attempted to do as you stated however,
none of the information is being passed through in the sql.

The problem now is that you're enclosing the & concatenation operator INSIDE
the quoted strings, so it's being treated as a literal character, part of the
string.

What you're trying to do is assemble the string. Some of the pieces came from
your VBA variables; some are string constants delimited by " characters. The &
is the glue that sticks them together. The added complication is that in order
to have a doublequote character in the resulting string you need TWO
CONSECUTIVE doublequote characters within the string: that is, the string

"A, ""-B"

results in

A, "-B


strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID,
DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, BookNumber, PageNumber) "

The above is all ok, because you're just putting literal text into strSQL -
what's between the quotes is what you want in the string.

strSQL = strSQL + "VALUES(lngPropertyID,lngDefendantNameID,lngDocumentTypeID,"

But here's where you need to change it. What you want to do is assemble the
following pieces:

Values( << a literal text string
Whatever number is in lngPropertyID << the value of a variable
, << a literal comma as a text string
Whatever number is in lngDefendentNameID <<< the value of a variable

This should be (ignore the line wrap introduced by the newsgroup):

strSQL = strSQL & "VALUES(" & lngPropertyID & "," & lngDefendentNameID & "," &
lngDocumentTypeID & ","

strSQL = strSQL + "lngDefendantTypeID, #txtLienDate# , txtLienAmount ,"

And here you want to include the # delimiters, again, as string constants:

strSQL = strSQL & lngDefendentID & ", #" & txtLienDate & "#, " & txtLienAmount
& ","

strSQL = strSQL + "& txtInstrumentNumber & , & txtBookNumber & , &
txtPageNumber & )"

And for the text fields you need to use the "" trick to insert one " mark
within the string:

strSQL = strSQL & """ & txtInstrumentNumber & """, """ & txtBookNumber & """,
""" & txtPageNumber & """)"

The following code should work for you (using the _ line continuation
character instead of repeated strSQL =):

strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID," _
& "DocumentTypeID, DefendantTypeID, LienDate " _
& "LienAmount, InstrumentNumber, BookNumber, PageNumber) " _
& "VALUES(" & lngPropertyID & "," & lngDefendantNameID _
& "," & lngDocumentTypeID & "," & lngDefendantTypeID _
& ", #" & txtLienDate & "# ," & txtLienAmount _
& " ,""" & txtInstrumentNumber & """,""" & txtBookNumber & """, """ _
& txtPageNumber & """)"

It's confusing as all get out, but if you work through the logic of assembling
pieces it should begin to make sense.
 
R

RayToddJr

John:

Thank you for your very detailed and helpful reply. I am sure I will be
keeping by my side for future reference.

Again Thanks,

Ray.
 
D

David W. Fenton

Another way is to use an apostrophe ' as the text delimiter and
surround that with quotes.

"'" & txtInstrumentNumber & "', " & ...

But that causes problems with fields that have apostrophes in them,
e.g., last names like "O'Connor". Of course, you can have the same
problem with double quotes if you have a name like 'Robert "Biff"
Anderson", but that's a lot less common.

I use a named constant for the double quote so I don't have to muck
around with """ and """", the rules for which I've never fully
understood. My constant is named STR_QUOTE, and is defined thus:

Const STR_QUOTE = """"

Then I use it thus:

strSQL = "INSERT INTO taDEFENDANTS(PropertyID, DefendantNameID, " &
_
"DocumentTypeID, DefendantTypeID, LienDate "
strSQL = strSQL + "LienAmount, InstrumentNumber, " & _
="BookNumber, PageNumber) "
strSQL = strSQL + "VALUES(" & lngPropertyID & ", " & _
lngDefendantNameID & ", " & _
lngDocumentTypeID & ", " & _
lngDefendantTypeID & ", #" & txtLienDate & "#, " &
STR_QUOTE & txtLienAmount & STR_QUOTE & ", " & _
STR_QUOTE & txtInstrumentNumber & STR_QUOTE & ", " & _
STR_QUOTE & txtBookNumber & STR_QUOTE & ", " & _
STR_QUOTE & txtPageNumber & STR_QUOTE & ")"

I just find that a helluva lot easier to comprehend, even though
it's more verbose. If you wanted a shorter constant name, you could
use something like c_strQ or the like. I find it easy to see the
ALL-CAPS constant name, and my convention also dates from my use of
Access 2, which used all caps for the system constants, so I was
being consistent with Access right up to the point when they changed
to the "modern" way in A95-97). I have too much code to change, so I
just kept the old name.
 

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

SQL Error Help 1
SQL Error Help 2

Top