Solved

How to prevent DLookup / DSum displaying previous record values in a new record ?

Posted on 2015-01-03
5
299 Views
Last Modified: 2015-01-03
Hello, I have a form (F_ProjectInfo) on which some values are displayed in unbound text boxes using VBA Dlookup and DSum statements, as follows:

Private Sub Form_Current()

Textbox1 = DSum("Payment", "T_ProjectPayments", "ProjectID = " & [Forms]![F_ProjectInfo]![ProjectID])
Textbox2 = DLookup("ProjectType", "T_ProjectType", "ProjectID = " & [Forms]![F_ProjectInfo]![ProjectID])

End Sub

Two problems occur when the form moves onto a new record:
1) an error message appears due to the new record not yet having a ProjectID (an auto number), hence causing problems for the DSum and DLookup statements.
2) the unbound text boxes display the DSum / DLookup values from the previous record.

I solved 1) by adding “On Error Resume Next” at the top of the code, but I just cannot seem to find a solution for problem 2). Does anyone have any ideas ?

Thank you in advance for any help.
0
Comment
Question by:Paul McCabe
5 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 200 total points
ID: 40529149
The error essentially stops your code from moving forward, so you must take steps to prevent that error. IMO, you should do this:

'/ clear the values first:
Textbox1 = ""
Textbox2 = ""
'/ check for a valid ProjectID before moving forward:
If Nz(Me.ProjectID, 0) <> 0 Then
  Textbox1 = DSum("Payment", "T_ProjectPayments", "ProjectID = " & [Forms]![F_ProjectInfo]![ProjectID])
  Textbox2 = DLookup("ProjectType", "T_ProjectType", "ProjectID = " & [Forms]![F_ProjectInfo]![ProjectID])
End If
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
ID: 40529150
try

Textbox1 = DSum("Payment", "T_ProjectPayments", "ProjectID = " & Nz([Forms]![F_ProjectInfo]![ProjectID],0))
 Textbox2 = DLookup("ProjectType", "T_ProjectType", "ProjectID = " & Nz([Forms]![F_ProjectInfo]![ProjectID],0))
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 200 total points
ID: 40529179
An alternative method is to simply use the control source properties of the textboxes (and no code).

In the property sheet, set the control source as follows, including the = sign:

 = DSum("Payment", "T_ProjectPayments", "ProjectID = " & [Forms]![F_ProjectInfo]![ProjectID]

Open in new window


This will display the value associated with each record, without needing to navigate from record to record (which is required to trigger the Current Event).  This is particularly useful if you are using the Continuous Forms View.

The New record displays "#Error", which resolves as soon as you start entering data into the new record.  To avoid the #Error, you can use the NZ function shown in Scott's post:

 = DSum("Payment", "T_ProjectPayments", "ProjectID = " & NZ(ProjectID, 0)

Open in new window


(Also note that since the controls are on F_ProjectInfo, you do not need the full form reference - just the field name.)
0
 

Author Comment

by:Paul McCabe
ID: 40529195
Thank you all very much for your suggestions. I opted for Scott's VBA-based solution since I already had the VBA code, but I tried the solution described by Mbizup as well and it worked perfectly. Thank you so much !!
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40529201
did you try the post at http:#a40529150 ?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now