Solved

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

Posted on 2015-01-03
5
310 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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 120

Expert Comment

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

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

821 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