Question From Expression builder to Code builder (1 Viewer)

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
I have some code in "Expression builder" which checks if Email is already in database:

Code:
=IIf(DCount("*";"[Contacts]";"[ID]<>" & Nz([ID];0) & " And [E-mail] = '" & Replace(Nz([E-mail]);"'";"''") & "'")>0;"Email was already used once!";"")

Can someone "translate" this code that I can use it in "Code builder"?
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
Or in any other way, as long as it checks for already existing fields (email and name) in database from my form.
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
Did you write that expression yourself?
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
Nop, I'm still just a noob :)

This expression is in one of the examples in access 2007.
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
Then maybe you need to "express" yourself properly? :)
 

DCrake

Remembered
Local time
Today, 04:18
Joined
Jun 8, 2005
Messages
8,632
=IIf(DCount("*";"[Contacts]";"[ID]<>" & Nz([ID];0) & " And [E-mail] = '" & Replace(Nz([E-mail]);"'";"''") & "'")>0;"Email was already used once!";"")

This code is syntactically incorrect and nonsensicle.
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
This code is syntactically incorrect and nonsensicle.
This code is placed on some label and whenever I open some record (contact) this label is empty if field value doesn't exist in some other record, but if it does it shows that warning.

This does work, but I would rather have code in VB where I could add some thing on my own.


I would have some button (Check fields) and on click event I should check if value in field named "E-mail Address" already exist in my table named "contacts" and field named "E-mail" at any of other record (contacts).
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
So you're using an Access template?

This is the syntax of an IIF statement in VBA:

Code:
IF [Something] > 0 Then 
    ....Do Something
Else
    ....Do Another Thing
End If

See if you can transform the expression to fit that structure. You know where the Click event of the button is right?
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
Yeah, I know the IF sentance and where/how to place the code, the problem for me is to check all records Email address in database...


Code:
IF [E-mail Address] <> *[Contacts][E-mail] Then ...
:eek:
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
Yeah, I know the IF sentance and where/how to place the code, the problem for me is to check all records Email address in database...


Code:
IF [E-mail Address] <> *[Contacts][E-mail] Then ...
:eek:
I'm not convinced. Maybe you could tell me where the code could go? In what event?
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
Yeah, I know the IF sentance and where/how to place the code, the problem for me is to check all records Email address in database...


Code:
IF [E-mail Address] <> *[Contacts][E-mail] Then ...
:eek:
I'm not convinced. Maybe you could tell me where the code could go? In what event?
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
On some button click. So when user enters data, he will click check fields and he will get some message if Email is already used by someone else...
 

vbaInet

AWF VIP
Local time
Today, 04:18
Joined
Jan 22, 2010
Messages
26,374
Since you say it works, I have just put it in vba code format:

Code:
    If DCount("*", "[Contacts]", "[ID]<>" & Nz([ID], 0) & " And [E-mail] = '" & Replace(Nz([E-mail]), "'", "''") & "'") > 0 Then
        MsgBox "Email was already used once!"
    Else
        MsgBox "Not in use"
    End If
 

MrNooby

Registered User.
Local time
Today, 05:18
Joined
Feb 21, 2010
Messages
58
Great, I think this is it :)

I'll test a bit more tomorrow when I'm home, but so far it works.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 13:18
Joined
Jan 20, 2009
Messages
12,851
I believe:
DCount("*", "[Contacts]", "[ID]<>" & Nz([ID], 0)

cound be replaced with:

DCount("[ID]", "[Contacts]", "[ID]<> 0")

because nulls are ignored when counting a field.
 

Users who are viewing this thread

Top Bottom