A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Database Design
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Syntax error when using DoCmd.OpenForm



 
 
Thread Tools Display Modes
  #1  
Old August 6th, 2009, 01:25 AM posted to microsoft.public.access.tablesdbdesign
lmcc007
external usenet poster
 
Posts: 63
Default Syntax error when using DoCmd.OpenForm

I have a command button that when clicked it will open a form called Check
Addresses. Below is the code:

Private Sub cmdEditAddress_Click()
Me.Requery

DoCmd.OpenForm "frmCheckAddresses", , , "CompanyID=" &
CompanyID

End Sub

The above code works fine when there is an address (or one of the address
fields is filled out), but when there is no address I get an error instead of
a blank Check Address form. The error message is:

Run-time error ‘3075’:

Syntax error (missing operator) in query expression ‘CompanyID=’.

I want a blank form so I can add an address if the address field is blank.

How shall I do that? Any help will be appreciated.

Thanks!



  #2  
Old August 6th, 2009, 03:03 AM posted to microsoft.public.access.tablesdbdesign
Allen Browne
external usenet poster
 
Posts: 11,706
Default Syntax error when using DoCmd.OpenForm

If the CompanyID is null (blank), there's no value to concatenate into the
WhereCondition, so it becomes just:
CompanyID =
which is clearly not valid, and so you get the syntax error.

To avoid that, test for Null, and then handle that case differently. Perhaps
you could filter the from so that there are no records to show. It would
then open the the new record. Like this:

Dim strWhere As String
If IsNull(Me.CompanyID) Then
strWhere = "(False)"
Else
strWhere = "CompanyID = " & Me.CompanyID
End If
DoCmd.OpenForm "frmCheckAddresses", _
WhereCondition:=strWhere

The WhereCondition string is an expression that evaluates to True or False
for each record. By setting it to the value False, no records match, and so
you end up with the new record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"lmcc007" wrote in message
...
I have a command button that when clicked it will open a form called Check
Addresses. Below is the code:

Private Sub cmdEditAddress_Click()
Me.Requery

DoCmd.OpenForm "frmCheckAddresses", , , "CompanyID=" &
CompanyID

End Sub

The above code works fine when there is an address (or one of the address
fields is filled out), but when there is no address I get an error instead
of
a blank Check Address form. The error message is:

Run-time error ‘3075’:

Syntax error (missing operator) in query expression ‘CompanyID=’.

I want a blank form so I can add an address if the address field is blank.


  #3  
Old August 6th, 2009, 05:49 AM posted to microsoft.public.access.tablesdbdesign
lmcc007
external usenet poster
 
Posts: 63
Default Syntax error when using DoCmd.OpenForm

Thanks a lot, Allen!

For some reason I keep getting confused about the WhereCondition. Oh well,
sometimes it take me a while to get something.

Thanks!

"Allen Browne" wrote:

If the CompanyID is null (blank), there's no value to concatenate into the
WhereCondition, so it becomes just:
CompanyID =
which is clearly not valid, and so you get the syntax error.

To avoid that, test for Null, and then handle that case differently. Perhaps
you could filter the from so that there are no records to show. It would
then open the the new record. Like this:

Dim strWhere As String
If IsNull(Me.CompanyID) Then
strWhere = "(False)"
Else
strWhere = "CompanyID = " & Me.CompanyID
End If
DoCmd.OpenForm "frmCheckAddresses", _
WhereCondition:=strWhere

The WhereCondition string is an expression that evaluates to True or False
for each record. By setting it to the value False, no records match, and so
you end up with the new record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"lmcc007" wrote in message
...
I have a command button that when clicked it will open a form called Check
Addresses. Below is the code:

Private Sub cmdEditAddress_Click()
Me.Requery

DoCmd.OpenForm "frmCheckAddresses", , , "CompanyID=" &
CompanyID

End Sub

The above code works fine when there is an address (or one of the address
fields is filled out), but when there is no address I get an error instead
of
a blank Check Address form. The error message is:

Run-time error ‘3075’:

Syntax error (missing operator) in query expression ‘CompanyID=’.

I want a blank form so I can add an address if the address field is blank.



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:50 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.