Solved

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

Posted on 2016-09-21
3
26 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

759 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

20 Experts available now in Live!

Get 1:1 Help Now