Solved

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

Posted on 2015-01-03
5
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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