Display message box if query returns no results

elmonko

New Member
Joined
Dec 18, 2012
Messages
25
Below is an example of multiple SQL queries that are run from a button on an excel sheet and are retrieved from an Access Database.

The WHERE clause in all the queries is the same and relates to a single cell reference.

I just have 2 small issues.....

If the query is to return no information than i would like a message box stating that's the case so the end user will need to enter the information manually. (once this is completed the user send back via email , the information is then collated into the DB and then when the next unit enters the value into the WHERE clause cell it will return data)

Secondly, if the query returns nothing then, the query that returns values into B18:F18, locks out these cells so the user can only enter from B19 downwards.

Any guidance would be appreciated

Thanks

Code:
sConn = "ODBC;CollatingSequence=ASCII;DBQ=" & Mailbox & DataB & ";DefaultDir=c:\temp;Deleted=1;Driver={Microsoft Access Driver (*.mdb)};DriverId=281;FIL=MS Access;"
sSql = "SELECT FORMAT(Total.FormReviewed, 'dd/mm/yy' ) AS DateRev FROM " & Mailbox & Totals & " WHERE(Total.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " )"
Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("C6"), Sql:=sSql)
oQt.AdjustColumnWidth = False
oQt.PreserveFormatting = True
oQt.RefreshStyle = xlOverwriteCells
oQt.FieldNames = False
oQt.Refresh
 
sConn = "ODBC;CollatingSequence=ASCII;DBQ=" & Mailbox & DataB & ";DefaultDir=c:\temp;Deleted=1;Driver={Microsoft Access Driver (*.mdb)};DriverId=281;FIL=MS Access;"
sSql = "SELECT FORMAT(Total.MeasuredClean, 'dd/mm/yy' ) AS DateRev FROM " & Mailbox & Totals & " WHERE(Total.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " )"
Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("F7"), Sql:=sSql)
oQt.AdjustColumnWidth = False
oQt.PreserveFormatting = True
oQt.RefreshStyle = xlOverwriteCells
oQt.FieldNames = False
oQt.Refresh
 
[B]'This query locks cells'[/B]
sConn = "ODBC;CollatingSequence=ASCII;DBQ=" & Mailbox & DataB & ";DefaultDir=c:\temp;Deleted=1;Driver={Microsoft Access Driver (*.mdb)};DriverId=281;FIL=MS Access;"
sSql = "SELECT Detail.BarName, Detail.ProdCode, Detail.ProdDesc, Detail.Price, Detail.Qty FROM " & Mailbox & Detail & " WHERE(Detail.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " )"
Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("B18"), Sql:=sSql)
oQt.AdjustColumnWidth = False
oQt.PreserveFormatting = True
oQt.RefreshStyle = xlOverwriteCells
oQt.FieldNames = False
oQt.Refresh
 
sConn = "ODBC;CollatingSequence=ASCII;DBQ=C:\Dropbox\capcon\queries\Clients.mdb;DefaultDir=c:\temp;Deleted=1;Driver={Microsoft Access Driver (*.mdb)};DriverId=281;FIL=MS Access;"
sSql = "SELECT Units.Name FROM C:\Dropbox\capcon\queries\Clients.Units Units WHERE(Units.Code=" & Sheets("Pipe Cleaning").Range("C4") & " )"
Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("C5"), Sql:=sSql)
oQt.AdjustColumnWidth = False
oQt.PreserveFormatting = True
oQt.RefreshStyle = xlOverwriteCells
oQt.BackgroundQuery = False
oQt.FieldNames = False
oQt.Refresh
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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