Hi there,
Wishing you all a happy new year and hoped you had a relaxing holidays.
Thanks for your advises. Here are the sql
1. DoCmd.RunSQL "INSERT INTO TProjectItems (ProjectID, ItemID,
Unit,
Qty, Item) values (Forms!FProjectItems.ProjectID," & Me!ItemID & ", tUnit,
tQty, tItem);"
2. 'DoCmd.RunSQL "INSERT INTO TProjectItems (ProjectID, ItemID,
itemdesc, Unit, Qty, Item) values (Forms!FProjectItems.ProjectID," &
Me!ItemID & "," & Me!ItemDesc & ", tUnit, tQty, tItem);"
3. xtemp = DLookup("[ProjItemID]", "TProjectitems", "[ProjectID] =
'"
& Forms!FProjectItems!ProjectID & "' AND [ItemID] =" & Me!ItemID)
DoCmd.RunSQL "UPDATE TProjectItems SET ItemDesc=" & Me!ItemDesc &
"
WHERE ProjectItemID = " & xtemp
Else
4. xtemp = DLookup("[ProjItemID]", "TProjectitems", "[ProjectID] = '" &
Forms!FProjectItems!ProjectID & "' AND [ItemID] =" & Me!ItemID)
DoCmd.RunSQL "DELETE * FROM TProjectitems WHERE ProjectID = '"
&
Forms!FProjectItems.ProjectID & "' AND [ItemID]=" & Me!ItemID
End If
AGAIN:
If you try to insert any literal text at all into a field of Text or Memo
datatype, what you're inserting must be - NO OPTIONS! - delimited, either
with
' or " as a text string delimiter. It doesn't matter whether the text
you're
inserting contains commas; if it contains ' you can use " as the
delimiter; if
it contains " you can use ' as the delimiter; or you can put two
consecutive "
marks within the string delimited by " in order to insert just one.
You also need to include the syntactically required commas and other
punctuation as text strings in building your SQL string. You're freely
mixing
VBA variables (tUnit, tQty, tItem) and SQL - that will NOT work, since the
SQL
engine has no way to see the values of these variables.
Just as an example, you could change (2) to
Dim strSQL As String
strSQL = "INSERT INTO TProjectItems (ProjectID, ItemID, " _
& "itemdesc, Unit, Qty, Item) Values (Forms!FProjectItems.ProjectID," _
& Me!ItemID & ",'" & Me!ItemDesc & "', '" & tUnit & "'," & tQty & ","_
& tItem & ");"
DoCmd.RunSQL strSQL
If Me!ItemDesc contains the text "Lions, Tigers and Bears, oh my!" the
resulting SQL statement will resemble
INSERT INTO tProjectItems (ProjectID, ItemID, itemdesc, Unit, Qty, Item)
VALUES (Forms!FProjectItems.ProjectID,251,'Lions, Tigers and Bears, oh
my!','Each', 1, 2545);
Setting the value of a SQL string will help in debugging as you can
actually
look at the SQL command which will be executed.
You have not (as requested) indicated the datatypes of these fields, but
I'm
guessing that ItemDesc is a text or memo field. Any others? Which control
contains the commas causing the problem?
And... more basically - WHY are you doing this the hard way? Any major
objections to just using a bound form, with no code at all? I admit,
sometimes
you must, and sometimes it's better - but it's not obvious in this case
that
you gain anything by programmatically doing what Access does for you.
John W. Vinson [MVP]