Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / January 2005

Tip: Looking for answers? Try searching our database.

Acc2003/XP: DLookup to compare before update

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oz Skull - 29 Jan 2005 04:23 GMT
G'day ppl,

I am looking for a code that will lookup a string value before it updates
the field eg.

= DLookup("txtMyTitles", "tblMyTitles", "Criteria = '" &
forms!frmMyTitlesInput!txtMyTitles & "'")
If Me.txtMyTitles = ("txtMyTitles", "tblMyTitles") Then
response = msgBox("Title already Exists", vbOk)
DoCmd.Cancel
Else
Me.txtMyTitles.Update
End If

Effectively, I want to check the table for the value I enter to see if it is
already there, if so, then cancel the event.

Now I know the Syntax structure is all wrong, but it is the best was I can
explain what I am trying to achieve.

Look forward to your thoughts

TIA

Mark.
John Vinson - 29 Jan 2005 06:12 GMT
>G'day ppl,
>
[quoted text clipped - 15 lines]
>Now I know the Syntax structure is all wrong, but it is the best was I can
>explain what I am trying to achieve.

DLookUp is a function which returns a value; you want to compare that
value with the value which is in your textbox. But there's actually a
simpler way: just see if the DLookUp returns nothing at all. Try:

Private Sub txtMyTitles_BeforeUpdate(Cancel as Integer)
Dim response As Integer
If Not IsNull(DLookUp("txtMyTitles","tblMyTitles","txtMyTitles = '" _
 & Me!txtMyTitles & "'") Then
 response = MsgBox("Title already exists", vbOKOnly)
 Cancel= True ' the BeforeUpdate event's Cancel property
              ' cancels the update if it's true
End If
' if Cancel is left False it goes ahead and does the update, no code
needed
End Sub

One tweak: if your title might contain an apostrophe ("Lady
Chatterly's Lover" for example) you can't use ' to delimit it. Safer
is to use ", ASCII value 34, as a delimiter:

If Not IsNull(DLookUp("txtMyTitles", "tblMyTitles",
"txtMyTitles = " & Chr(34) & Me!txtMyTitles & Chr(34)) Then

This all assumes that you have a Field named txtMyTitles in your table
(the first argument to dlookup) and that the table is named
tblMyTitles. If it were me, I'd use a form control named txtMyTitle
bound to a table field named MyTitle (singular not plural and without
a prefix), but whatever naming conventions work for you are fine.

                 John W. Vinson[MVP]    
noodnutt - 29 Jan 2005 07:06 GMT
Thank you John

You have helped immensely; with regard to the naming, they actually are as
you suggest, I put them in this context to avoid any confusion.

Thx again

reg's

Mark

> >G'day ppl,
> >
[quoted text clipped - 46 lines]
>
>                   John W. Vinson[MVP]
noodnutt - 29 Jan 2005 08:01 GMT
One snag John, if the procedure is true, then a blank record is left at the
end of the event. What do you think I should add to the If statement to
delete the record in this event.

Thx again

Mark.
John Vinson - 30 Jan 2005 00:03 GMT
>One snag John, if the procedure is true, then a blank record is left at the
>end of the event. What do you think I should add to the If statement to
[quoted text clipped - 3 lines]
>
>Mark.

Move the code to the Form's BeforeUpdate event instead of the
control's. Setting Cancel to true will now cancel the entire record,
not just the one textbox.

                 John W. Vinson[MVP]    
noodnutt - 30 Jan 2005 01:04 GMT
Thx again John,

your a legend.

Regs

Mark.

> >One snag John, if the procedure is true, then a blank record is left at the
> >end of the event. What do you think I should add to the If statement to
[quoted text clipped - 9 lines]
>
>                   John W. Vinson[MVP]
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.