473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dlookup problem - Check to see if a date has been entered

beacon
579 512MB
Hi everybody,

I'm really hoping this will be the last problem I run into because I don't have much hair left to pull out! :)

I have a form that the user will enter a census date (indexed field) and a census into. I'm indexing the census date because I don't want duplicate entries.

When the user opens the frmCensus and tries to enter a date, I've implemented code that fires when the user selects a submit button that should check to see if the date has already been entered or not. If the answer is yes, msgbox pops up, clears all fields, and sets the focus on the date field. If the answer is no, the record is saved.

However, things aren't working out the way I had hoped and I think it's because there's an error with the Dlookup function I've used. Here's the current code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSubmit_Click()
  2.  
  3.     Dim varCensusDate
  4.  
  5.     varCensusDate = Me.CensusDate
  6.  
  7.     If IsNothing(varCensusDate) Then
  8.         MsgBox "Date field cannot be blank", vbExclamation, "Null value"
  9.         Exit Sub
  10.     End If
  11.  
  12.     If IsNothing(Me.Census) Or Me.Census = 0 Then
  13.         MsgBox "Census cannot be blank or have a zero value", vbExclamation, "Null Value"
  14.         Exit Sub
  15.     End If
  16.  
  17.     If Not IsNothing(DLookup("CensusDate", "tblCensus", varCensusDate)) Then
  18.         MsgBox "A census value has already been entered for this date", vbInformation, "Duplicate Entry"
  19.         If (Me.Dirty = True) Then
  20.             Me.Undo
  21.         End If
  22.         Me.CensusDate.SetFocus
  23.         Exit Sub
  24.     End If
  25.  
  26. End Sub
  27.  
I thought it was working when I originally tested it out, but know it's popping up the "census value has already been entered" msgbox for everything I try.

Any ideas?

Thanks,
beacon
Dec 2 '08 #1
4 2869
PianoMan64
374 Expert 256MB
@beacon
Hello Beacon,

The reason that you're having the issue is because you don't have the DLookup() function Criteria set correctly. You didn't include the field from the table tblCensus in the criteria.

The correct way to do what you're wanting is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DLookup("CensusDate","tblCensus","[CensusDate]=#" & varCensusDate & "#")
  3.  
  4.  
Hope that helps,

Joe P.
Dec 2 '08 #2
ADezii
8,834 Expert 8TB
Hope you don't mind, but I took the liberty of re-writing your code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSubmit_Click()
  2. If IsNull(Me![CensusDate]) Then
  3.   MsgBox "Date field cannot be blank", vbExclamation, "Null value"
  4.     Exit Sub
  5. Else
  6.   If IsNull(Me![Census]) Or Me![Census] = 0 Then
  7.     MsgBox "Census cannot be blank or have a zero value", vbExclamation, "Invalid Entry for Census"
  8.       Exit Sub
  9.   End If
  10. End If
  11.  
  12. If DCount("*", "tblCensus", "[CensusDate] = #" & Me![CensusDate] & "#") > 0 Then
  13.   MsgBox "A census value has already been entered for this date [" & Me![CensusDate] & "]", _
  14.           vbInformation, "Duplicate Entry"
  15.     If Me.Dirty Then
  16.       Me.Undo
  17.       Me![CensusDate].SetFocus
  18.     Else
  19.       'fall through
  20.     End If
  21. End If
  22. End Sub
  23.  
Dec 3 '08 #3
beacon
579 512MB
@ADezii
Hi Joe & ADezii,

Y'know, I read about using the '#' sign when using dates and I think I used it in another Dlookup function I wrote, but for some reason I didn't even realize in this case. Thank you for pointing that out.

ADezii...I don't mind you rewriting the code at all, but I would like to know your reasoning for writing it the way you did just so I can gain a better understanding of the functions, in case I run across another instance when I may need to implement it in this way.
Dec 3 '08 #4
ADezii
8,834 Expert 8TB
@beacon
Mostly a matter of personal preference, since there are many ways in VBA to accomplish the same task. Simply another approach for you to look at.
Dec 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Rey | last post by:
Howdy all. Appreciate your help with several problems I'm having: I'm trying to determine if the Visit subform (subformVisits) has a new record or been changed, i.e. dirty. The form that...
2
by: sherah | last post by:
I am trying to create a procedure that has values are entered into a table that based on the value entered will programatically decide weather or not the Yes/No should be checked. I am trying to...
3
by: Rich Bernat | last post by:
We have vending machines which are serviced by emptying the money inside the machine and placing it into numbered bags. Each bag is numbered independently of the machines. Each machine has a...
4
by: Beejer | last post by:
I'm running Access 2003 on WinXP Here is my problem: I'm trying to lookup a date in a table from a query data. The query include the following: Query = Qry_Invoice and Table = Tbl_Friday ...
2
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
19
by: Kosmos | last post by:
Hey guys...I've never used DLookup before... I'm just simply wondering how to use it. I have to check a field to see if it has been entered before. The field is in...
2
by: Isaac | last post by:
I am still working on my database of work performance for the company I work for. I have a form (frmtblOccurrences) based on a table (tblOccurrences). I also have tblAgent that contains personal...
1
by: jamesjohn | last post by:
i have one more doubt can i use the dlookup () function to check out three fields if the same account information and amount are entered within the past seven days of the current date an error...
2
by: tasawer | last post by:
I am using DLOOKUP to bring up the value of VAT rate applicable in a particular date period. but it is not bringing expected values. Can someone help me resolve the issue. Thx I have a table:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.