Solved

Display contents of a field in a message box - MS Access

Posted on 2016-09-21
3
41 Views
Last Modified: 2016-09-21
I have an Access form for adding records.  One of the fields on the form requires a date to be entered.  When the user presses the command button on the form to add a record, the date entered in the textbox is checked against a table I have that lists invalid dates.

That table has two fields.  Field1 contains the invalid date value. Field2 contains a description of why it is invalid.

So, for example, a record in the table may look like this:

Field1              Field2
9/19/2016      Closed for renovations

To check if an invalid date is selected, I currently have the following code:

If CBool(DCount("*", "MyDatesTable", "[Invalid_Date] = #" & Format(Forms![MyForm]![MyDateTextbox].Value, "yyyy\/mm\/dd") & "#")) Then
     MsgBox "Invalid Date Selected"
End If

What I'd like to be able to is, in addition to the message box indicating an invalid date was selected, I would also like to display why it's invalid. Therefore, if a matching date is found in the table with what was entered in the textbox, I'd like to be able to display the contents of Field2 for that record.

So, if 9/19/2016 was selected (and presuming it's in the table as an invalid date due to business having been closed for renovations), I'd like to be able to display Field2's contents (i.e. "Closed for renovations") in the messagebox.  I don't care where in the messagebox it is displayed just as long as it is displayed.  Thank you.
0
Comment
Question by:dbfromnewjersey
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41809055
Then use DLookup() rather than DCount()
Dim ErrDesc as Variant
ErrDesc = DLookup("Field2", "MyDatesTable", "[Invalid_Date] = #" & Format(Forms![MyForm]![MyDateTextbox].Value, "yyyy\/mm\/dd") & "#")
If Not IsNull(ErrDesc) then
    Msgbox  "Invalid Date Selected - " & ErrDesc, vbOKOnly
End If

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 41809138
Thank you.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 41809320
You're welcome
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

24 Experts available now in Live!

Get 1:1 Help Now