 |
| Access VBA Discuss using VBA for Access programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 24th, 2003, 02:48 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Syntax Error
I have the following attached to the Click Event of a Command Button. When I click the button, I get a syntax error, missing operator in the code line vcc = .....
I know I'm doing something stupid, but I can't figure it out. HELP!
Dim vcontact, vsubject, vcc, veng As String
Dim vnum As Variant
vnum = Forms![frmProdApproval]![ECNNo]
vsubject = "Suggested Revision of ECN No. " & vnum
veng = DLookup("[engid]", "approval", "[ecnno]= forms![frmProdApproval]![ecnno]")
vcc = DLookup("[email]", "password", "[Name] = " & veng)
vcontact = DLookup("[email]", "password", "[Name]=forms![frmProdApproval]![change by]")
DoCmd.SendObject , , acFormatRTF, vcontact, vcc, , vsubject, , True
Thanks,
Ray
|
|

October 24th, 2003, 02:59 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dlookup sintax is
DLookup («expr», «domain», «criteria»)
you are passing
vcc = DLookup("[email]", "password", "[Name] = " & veng)
which is the equivalent of
DLookup («expr», «domain», «criteria» & veng)
in Criteria, pass a string instead of appending to the value
let me know how it goes
Sal
|
|

October 25th, 2003, 12:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Hi Ray,
If you were using a numeric value as your criteria, vcc = DLookup("[email]", "password", "[Name] = " & veng) would be the correct syntax. Then, the original criteria expression:
"[Number] = " & veng
evaluates to:
"[Number] = " & 1
which, when concatenated, evaluates as:
"[Number] = 1"
which is a correct numeric criteria clause for DLookup.
However, since your [Name] field is apparently a string, you need syntax like:
vcc = DLookup("[email]", "password", "[Name] = '" & veng & "'")
Here, the original criteria expression:
"[Name] = '" & veng & "'")
evaluates to:
"[Name] = '" & "Smith" & "'")
which, when concatenated, evaluates as:
"[Name] = 'Smith'"
The following lists the syntax for using string, numeric, or date criteria with the Domain Aggregate functions. The syntax is the same whether referencing a control or a variable. You simply use a vanilla SQL WHERE clause without the WHERE statement. Its also a good idea to trap for Null when using the Domain Aggragates, which is what the Nz function is doing below:
Referencing controls
' String criteria
intEmployeeeID = Nz(DLookup("[EmployeeID]", "tblEmployees", _
"[LastName] = '" & Me.txtLastName & "'"), 0)
' Numeric criteria
strLastName = Nz(DLookup("[LastName]", "tblEmployees", _
"[EmployeeID] = " & Me.txtEmployeeID), "")
' Date/Time criteria
intEmployeeID = Nz(DLookup("[EmployeeID]", "tblEmployees", _
"[HireDate] = #" & Me.txtHireDate & "#"), 0)
Referencing variables
' String criteria
strLastName = Me.txtLastName
intEmployeeID = Nz(DLookup("[EmployeeID]", "tblEmployees", _
"[LastName] = '" & strLastName & "'"), 0)
' Numeric criteria
intEmployeeID = Me.txtEmployeeID
strLastName = Nz(DLookup("[LastName]", "tblEmployees", _
"[EmployeeID] = " & intEmployeeID), "")
' Date/Time criteria
datHireDate = Me.txtHireDate
intEmployeeID = Nz(DLookup("[EmployeeID]", "tblEmployees", _
"[HireDate] = #" & datHireDate & "#"), 0)
HTH,
Bob
|
|

October 25th, 2003, 12:37 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
I should probably mention that the method of handling string criteria as posted above (while frequently used) will only work if the value of 'veng' does not itself contain an apostrophe. So only use this method if the value of 'veng' could never contain an apostrophe. If it could, you will need to write a function to double up the apostrophe inside the string so that VBA (and ADO) will interpret the double delimeter as a single delimeter.
Regards,
Bob
|
|

October 25th, 2003, 01:07 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Something like:
Public Function PadSingleQuotes(strIn As String) As String
Dim intI As Integer
Dim strTemp As String
Dim strCh As String
For intI = 1 To Len(strIn)
strCh = Mid(strIn, intI, 1)
If strCh = "'" Then
strTemp = strTemp & "''"
Else
strTemp = strTemp & strCh
End If
Next intI
PadSingleQuotes = strTemp
End Function
Called by:
' String criteria
strLastName = Me.txtLastName
intEmployeeID = Nz(DLookup("[EmployeeID]", "tblEmployees", _
"[LastName] = '" & PadSingleQuotes(strLastName) & "'"), 0)
Regards,
Bob
|
|

October 25th, 2003, 06:26 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Also wanted to mention that the declaration:
Dim vcontact, vsubject, vcc, veng As String
creates 3 Variant variables and 1 String ('veng') in VBA/ VB.
In VB.NET, the same syntax would give you 4 Strings.
If you want vcontact, vsubject, vcc to be declared as string variables, then the As String clause needs to immediately follow each in VBA/ VB:
Dim vcontact As String, vsubject As String
Dim vcc As String, veng As String
|
|

October 27th, 2003, 10:13 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Bob, Sal ... Thanks guys. You have saved my life and taught me a few things.
Ray
|
|

October 27th, 2003, 10:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Quote:
quote:Originally posted by RayL
vcontact = DLookup("[email]", "password", "[Name]=forms![frmProdApproval]![change by]")
|
Ray, I'm surprised this one didn't give you a strange result. It should be coded this way:
Code:
vcontact = DLookup("[email]", "password", "[Name]='" & _
forms![frmProdApproval]![change by] & "'")
You're passing a variable. The way you originally had it expressed it merely as a literal string, not an evaluated variable.
Oh, and I know nostalgic folks love the bang (!), but consider using only periods. They still work and you get the added benefit that Access XP autocompletes variables when you write code.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Parse error: syntax error, unexpected T_ELSE in /h |
vipin k varghese |
BOOK: XSLT Programmer's Reference, 2nd Edition |
4 |
September 29th, 2011 01:19 AM |
| Ch 4: Parse error: syntax error, unexpected T_SL |
hanizar77 |
BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 |
0 |
June 23rd, 2008 09:17 PM |
| Parse error: syntax error, unexpected T_STRING |
ginost7 |
Beginning PHP |
1 |
November 9th, 2007 02:51 AM |
| VB Error: Syntax Error or Access Violation |
codehappy |
VB How-To |
7 |
October 3rd, 2007 05:41 PM |
| Compile error: Syntax error: & Else without HELP |
Corey |
VB How-To |
2 |
April 21st, 2006 03:25 PM |
|
 |