Solved

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

Posted on 2016-09-21
3
142 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
[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
  • 2
3 Comments
 
LVL 37

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 37

Expert Comment

by:PatHartman
ID: 41809320
You're welcome
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 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