Something like this (untested aircode):
Private Sub PhoneTypeID_BeforeUpdate(Cancel As Integer)
Dim varResult As Variant
Dim strWhere As String
If IsNull(Me.CompanyID) OR IsNull(Me.PhoneTypeID) OR _
(Me.CompanyID = Me.CompanyID.OldValue AND _
Me.PhoneTypeID = Me.PhoneTypeID.OldValue) Then
'do nothing
Else
strWhere = "(CompanyID = " & Me.CompanyID & _
") AND (PhoneTypeID = """ & Me.PhoneTypeID & """)"
varResult = DLookup("ID", "Table1", strWhere)
If Not IsNull(varResult) Then
Cancel = True
MsgBox "Oops! Same type as in ID " & varResult
End If
End If
End Sub
I've assumed CompanyID is Number, and PhoneTypeID is text. Remove the extra
quotes if both are numbers.
--
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" <(E-Mail Removed)> wrote in message
news:C2D47A05-D4B6-49AD-88A9-(E-Mail Removed)...
> Okay, I know how to do that in table design. I am trying to form
> validation.
>
> Thanks.
>
>
> "Allen Browne" wrote:
>
>> Presumably you have a separate table for companies (with a CompanyID
>> primary
>> key), and then this table with fields:
>> CompanyID relates to the key of the Companies table.
>> PhoneTypeID the field you are talking about.
>>
>> If so, you can make the combination unique at the table level. The
>> simplest
>> thing might be to make the 2 fields together the primary key.
>>
>> 1. Open the table in design view.
>>
>> 2. Select the 2 fields (by clicking on the "record selector" to the left
>> of
>> the first field, and dragging down.)
>>
>> 3. Click the Key icon (on the menu/toolbar.)
>>
>> Now you won't be able to enter 2 records of the same phone type for the
>> same
>> company.
>>
>> --
>> 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" <(E-Mail Removed)> wrote in message
>> news:14AF29F3-F362-46D5-A8E8-(E-Mail Removed)...
>> > First of all, I am not sure how to ask this question. But, this is
>> > what I
>> > am
>> > trying to do: I do not want the same phone type per company. If I
>> > choose,
>> > for example, Fax as the phone type twice, I want a message to display
>> > saying
>> > to choose another phone type. I entered the code below, but keep
>> > getting
>> > an
>> > error:
>> >
>> > Private Sub PhoneTypeID_BeforeUpdate(Cancel As Integer)
>> >
>> > If Me.CompanyID And Me.PhoneTypeID <> Me.PhoneTypeID Then
>> >
>> > Me.PhoneTypeID
>> >
>> > Else
>> >
>> > MsgBox "Choose another phone type.", vbCritical, "Phone Type"
>> >
>> > End If
>> >
>> > End Sub
>> >
>> >
>> > The error highlights Me.PhoneTypeID then give this error: Compile Error
>> > Invalid use of property.
>> >
>> > Then I tried putting the validation rule on a control in form design
>> > view.
>> > I tried this:
>> >
>> > =DLookUp("PhoneTypeID","Telephone","PhoneTypeID =
>> > Forms!frmTelephone!PhoneTypeID") Is Null
>> >
>> > The problem is it is not requiring it a unique PhoneTypeID per company.
>> > It
>> > is doing it for the entire table.
>> >
>> >
>> > I know I am missing something, but haven’t been able to figure it out.
>> >
>> >
>> >
>> > Thanks!
>>
>> .
>>