What is wrong with my Openform code? (1 Viewer)

jonathanchye

Registered User.
Local time
Today, 05:47
Joined
Mar 8, 2011
Messages
448
I have a button which opens a form referencing to the record selected in my main form's combobox (Combobox name is cboName)

The form I am opening is called frmCustomerDB and the field I am referencing to in that form is [CustomerName].

Here is my code:

Code:
Dim temp As String
temp = "[CustomerName]=" & cboCustomer.Value
DoCmd.OpenForm "frmCustomerDB", , , temp

I have the following error showing :

Run-time error '3075'

Syntax error (missing operator) in query expression '[CustomerName]=ACME CO LTD'

Is it because of the spaces?
 

jdraw

Super Moderator
Staff member
Local time
Today, 00:47
Joined
Jan 23, 2006
Messages
15,394
Since it's text you need to enclose with '.
Try
temp = "[CustomerName]='" & cboCustomer.Value & "'"
 

boblarson

Smeghead
Local time
Yesterday, 21:47
Joined
Jan 12, 2001
Messages
32,059
And to go with jdraw's response -

Since it is a name field, I would not use single quotes (in case of a name like O'Neil, O'Hare, etc.). Use Chr(34) instead.

temp = "CustomerName =" & Chr(34) & Me.cboCustomer & Chr(34)

And I would use the Me. part instead of just cboCustomer because sometimes it just doesn't quite work without the form reference.
 

jonathanchye

Registered User.
Local time
Today, 05:47
Joined
Mar 8, 2011
Messages
448
Thanks a lot for the quick response guys! Guess I am just a little bit confused switching between using the Macro builder and VBA...

Can anyone please refresh my memory why I need the extra single quote after the name tho?
 

boblarson

Smeghead
Local time
Yesterday, 21:47
Joined
Jan 12, 2001
Messages
32,059
Can anyone please refresh my memory why I need the extra single quote after the name tho?

For text you need to use quotes, for dates you need to use Octothorpes (#) and for numeric fields you don't need any.
 

Users who are viewing this thread

Top Bottom