SQL result to msgbox

Ivan_a

New Member
Joined
Oct 7, 2016
Messages
7
Hello everyone,

I am trying to display the result of a SQL select query in a message box in access 2016. My initial thinking was that I need to use Openrecordset, but for some reason I can't manage to get it to work.

This is my code:


sqlstring = "Select [Current Period] from tblCHinfo"

Set Msgstring = CurrentDb.OpenRecordset(sqlstring)

MsgBox (Msgstring)


I also tried to search in the forum for an older topic on this, but couldn't fine one. Is anyone able to help out with this one ?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I'd probably use DLOOKUP
Code:
strMsg = DLOOKUP("[Current Period]","tblCHInfo")
MsgBox (strMsg)
 
Upvote 0
Why it didn't work: SET is for assigning objects to object variables (as in a recordset to a recordset object). You can't put an object into a message box.
Also, your recordset has no criteria, so likely you could get more than one record returned by your sql or query. When you don't provide criteria for a query/sql or domain function (such as DLookup) you will get whatever record Access considers to be the first one, and it may not be the first one you see in the table or query. Usually you could rely on the 1st record of a query if you do sorting, but you're not doing that either.
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,607
Members
452,660
Latest member
Zatman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top