Hello
I've got a form which is based on a query
It's opened by a command button - when the command button is clicked a tect box prompts for the criteria "enter word"
The query searches a list of courses for any containing the "word" and then the form opens and displays those records
So far so good - BUT
if there are no matches for the "word" - the form opens but is just a grey screen - no boxes or anything - just blank
I've tried code on the command button and on the form itself - I'd just like a message box saying "no records to display" and then for the form to close and the previous screen (with the command button on it) to reappear
I've even tried putting some extra code on the SQL of the query - but having absolutely no luck
I can get a message box to display if there is a record with the following code:
Private Sub Form_Load()
If [course name] <> "" Then
MsgBox "is record"
End If
End Sub
and I tried
Else
MsgBox "no record"
but that didn't work
a message box on the "on error" of the form load does come up - but it comes up whether the query returns something or not
stumped - any help greatfully received
thanks
Shona
18 2155
I had this problem, what you can do is -
dim dbs as dao.database
-
dim results as dao.recordset
-
set dbs = currentdb
-
set results = dbs.OpenRecordset("SELECT * FROM query;")
-
if results.recordcount = 0 then
-
msgbox("There were no records to display")
-
else
-
'open form
-
end if
-
where query is the query that you form is based on.
If you put this in the Form_Load() sub it should work.
Sorry, one thing I for to say, if you use "SELECT COUNT(*) FROM query;" then recordcount will ALWAYS be 1 as you have a result - its a value of 0, but it's a result. If you use the count function, you can use result(0) instead of results.recordcount as results(0) will use the value returned by "SELECT COUNT(*) FROM query;"
thanks very much - I gave it a try but get
runtime error 3061
too few parameters. Expected 1
on the line:
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
not sure what it means
Shona
Can you do me a favour and post the whole sub in and I'll see if I can find the problem.
One other question - do you have Microsoft DAO ticked in your "Tools->References" in the VBA??
I think I know what the problem is - -
Dim dbs As DAO.Database
-
Dim results As DAO.Recordset
-
rather than -
Dim dbs As DOA.Database
-
Dim results As DOA.Recordset
-
My mistake
I noticed the doa / dao thing and changed it - and I checked that the dao is checked in references
the sub is:
Private Sub Form_Load()
Dim dbs As DAO.Database
Dim results As DAO.Recordset
Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
If results(0) Then
MsgBox ("There were no records to display")
Else
'open form
End If
should I have changed anything apart from the query name
(sorry - really rusty on Access and trying to re-learn)
thanks again
Shona
End Sub
you want to use results.recordcount rather than results(0) as you don't have count in the recordset sql string -
If results.recordcount = 0 Then
-
MsgBox ("There were no records to display")
-
Else
-
'open form
-
End If
-
Still getting a grey screen I'm afraid - code now says:
Private Sub Form_Load()
Dim dbs As DAO.Database
Dim results As DAO.Recordset
Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
If results.RecordCount = 0 Then
MsgBox ("There were no records to display")
Else
'open form
End If
End Sub
does it display the "There were no records to display" message or doesn't it get that far?
if there are records then the form opens and displays them properly - if there are no records I just get the grey screen - the message box doesn't appear
What I suggest you do then is the code for the command button rather than the form_load() sub -
Private Sub command_button_click()
-
Dim dbs As DAO.Database
-
Dim results As DAO.Recordset
-
Set dbs = CurrentDb
-
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
-
If results.recordcount = 0 Then
-
MsgBox ("There were no records to display")
-
Else
-
'open form
-
End If
-
This means that if there are no records to display it won't open the form. Alternatively, you can -
Private Sub Form_Load()
-
Dim dbs As DAO.Database
-
Dim results As DAO.Recordset
-
Set dbs = CurrentDb
-
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
-
If results.recordcount = 0 Then
-
MsgBox ("There were no records to display")
-
docmd.close
-
End If
-
which should close the form if there are no reocrds to display.
tried both of those but I'm afraid I get
runtime error 3061
too few parameters. Expected 1
sorry and thanks for helping - I'm way out of my depth here
post your code and I'll have a look. Also, let me know if you want to open the form and then check or check than open the form if required.
I just pasted in the code you suggested
tried both ways as I don't mind whether the opens or not really - either way is fine
If you do the working around the command button rather than form_load where formname is the name of the form that you are trying to open and command_button is the name of you button -
Private Sub command_button_click()
-
-
Dim dbs As DAO.Database
-
Dim results As DAO.Recordset
-
dim stdocname as string
-
dim Dim stLinkCriteria As String
-
-
Set dbs = CurrentDb
-
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
-
-
If results.recordcount = 0 Then
-
MsgBox ("There were no records to display")
-
Else
-
stDocName = "formname"
-
DoCmd.OpenForm stDocName, , , stLinkCriteria
-
End If
-
-
End Sub
-
If this still doesn't work, post your code and I'll see what else I've got wrong!!!
am getting the runtime error as soon as I press the command button - the text box which should prompt for the "word" doesn't appear
code is:
Private Sub viewtr_Click()
Dim dbs As DAO.Database
Dim results As DAO.Recordset
Dim stdocname As String
Dim stLinkCriteria As String
Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
If results.RecordCount = 0 Then
MsgBox ("There were no records to display")
Else
stdocname = "seltrain"
DoCmd.OpenForm stdocname, , , stLinkCriteria
End If
End Sub
the line:
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
is highlighted in the debug window
you are a very patient person - thank you
Hello again
I've been trawling the internet to see if anyone else has asked about this problem before and found the following code which goes on the Form Load event:
If Me.RecordSetClone.RecordCount = 0 Then
DoCmd.Close
End If
and it seems to work beautifully - even though I have no idea what it's doing
thank you so very much for all your help - I have learned a great deal this morning
very best wishes
Shona
Hi Shona
I cannot help you with you question, but would like to source some info from you - What you have described when opening your form seems to be exactly what I am trying to do (hence my posting on bound commands). but cannot get right.
How did you build and link the command button and the text box? - I would dearly like to do the same but have no idea how - And then maybe in my text box provide the option of a dropdown box to select from.
Can you PLEASE PLEASE tell me what you did?
Annette
Hello
I've got a form which is based on a query
It's opened by a command button - when the command button is clicked a tect box prompts for the criteria "enter word"
The query searches a list of courses for any containing the "word" and then the form opens and displays those records
So far so good - BUT
if there are no matches for the "word" - the form opens but is just a grey screen - no boxes or anything - just blank
I've tried code on the command button and on the form itself - I'd just like a message box saying "no records to display" and then for the form to close and the previous screen (with the command button on it) to reappear
I've even tried putting some extra code on the SQL of the query - but having absolutely no luck
I can get a message box to display if there is a record with the following code:
Private Sub Form_Load()
If [course name] <> "" Then
MsgBox "is record"
End If
End Sub
and I tried
Else
MsgBox "no record"
but that didn't work
a message box on the "on error" of the form load does come up - but it comes up whether the query returns something or not
stumped - any help greatfully received
thanks
Shona
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John |
last post by:
Just a general question...
I'm currently using a combobox that when updated, opens a form with
its recordset based on a query using the combo box value as the
criteria.
I'm I correct in...
|
by: neptune |
last post by:
I have a query where each customer has an or . Sometimes
both fields for a customer are populated, but if is null, then
will be populated and vice versa. I have a form,
, where I select a...
|
by: DebbieG |
last post by:
I have a form based on this query:
SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr,
OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, & ", " &
& " " & AS Name,...
|
by: dkintheuk |
last post by:
Hi all,
Using Access 2000 on XP Pro PC.
I have a form that is based on a presaved query - all fine with this.
I also have a refresh button that takes the values from various unbound...
|
by: meganrobertson22 |
last post by:
hi everybody-
what is the best way to add data from one form to another?
i have 2 tables: person and contract. here are some of the fields.
table: person
personid (autonumber and primary...
|
by: Robert |
last post by:
I am trying to give the user dynamic search capabilities to select
almost any record in the database from criteria they select. Everything
seems to work except when I open the display form to...
|
by: fstenoughsnoopy |
last post by:
I have a customer order database and I need to pull a customers
information, ie first name, last name, address, city, state, zip,
phone, etc, into the oder table. i don't know how to go about...
|
by: john |
last post by:
I created a form via the wizard with 1 main table and 2 one-on-one tables.
As i couldn't add a field to the form (a field that I added to the table
after creating the form), I googled out that the...
|
by: DWolff |
last post by:
I'm somewhat new to Access, and have no VB experience at all (except for modifying existing code where obvious).
I built a data entry form that works fine. Typically, a client will call in and...
|
by: tess |
last post by:
I have:
table 1 - tblLeadInfo which includes a salesman ID field
table 2 - tbllkpSalesman with all zips in the state and a Salesman
assigned to that area.
I have a form based on table #1 When...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |